diff --git a/fedilogue/instance.go b/fedilogue/instance.go index 83dec17..1b69116 100644 --- a/fedilogue/instance.go +++ b/fedilogue/instance.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "math/rand" + "context" "strings" "time" "net" @@ -83,7 +84,7 @@ func UpdateRunner(endpoint string, o RunningInstance) { ri_mutex.Unlock() } -func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance { +func GetInstanceInfo(endpoint string, o RunningInstance) RunningInstance { /* Checking order * Mastodon/Pleroma * Um..nothing else yet @@ -96,6 +97,7 @@ func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance { pleromastodon_api_resp, err := DoTries(&o, req) if err != nil { + o.Software = "Unsupported" return o } else { defer pleromastodon_api_resp.Body.Close() @@ -120,6 +122,7 @@ func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance { o.LastRun = time.Now().Format(time.RFC3339) if err != nil { o.Status = UNSUPPORTED_INSTANCE + o.Software = "Unsupported" logWarn("Unable to connect to " + endpoint + ", giving up") return o } @@ -128,6 +131,7 @@ func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance { indexbin, err := ioutil.ReadAll(resp_index.Body) if err != nil { o.Status = UNSUPPORTED_INSTANCE + o.Software = "Unsupported" logWarn("Unable to read index of " + endpoint + ", giving up") return o } @@ -142,11 +146,30 @@ func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance { } else if strings.Contains(indexstr, "Gab") { o.Software = "gab" o.Version = "guess" + } else { + o.Software = "Unsupported" + o.Version = "Unknown" } return o } +func LogInstance(endpoint string, o RunningInstance) bool { + selectRet := pool.QueryRow(context.Background(), "SELECT FROM instances WHERE endpoint = $1", endpoint) + err := selectRet.Scan() + if err == nil { + return true // Endpoint already in database, continuing + } + + _, err = pool.Exec(context.Background(), "INSERT INTO instances (endpoint, autostart, state, software) VALUES($1, $2, $3, $4)", endpoint, true, "", o.Software) + if err != nil { + logWarn("Error inserting %s into `instances`: "+endpoint, err) + return true + } + + return false +} + func CheckInstance(newinstance string, callerEndpoint string) { if settings.Crawl == true && stringexists(newinstance, settings.Banned) == false { // Skip over this if its the same as the endpoint or empty @@ -194,8 +217,9 @@ func StartInstance(endpoint string) { // Check if exists. If so, get the object. If not, create it o, _ := GetRunner(endpoint) - o = GetNodeInfo(endpoint, o) + o = GetInstanceInfo(endpoint, o) UpdateRunner(endpoint, o) + LogInstance(endpoint, o) if o.Software == "pleroma" { logConn("Starting " + endpoint + " as " + o.Software + " " + o.Version)