From 07c11d360d0d8d74d497d203d844baa8643623b2 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Fri, 10 Dec 2021 22:59:21 -0500 Subject: [PATCH 1/3] Logging instance to database --- fedilogue/instance.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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) From b42701e4efffadf80d6877c183e1935065dad9e8 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Sat, 11 Dec 2021 00:32:32 -0500 Subject: [PATCH 2/3] Adding autostart from database --- fedilogue/fedilogue.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/fedilogue/fedilogue.go b/fedilogue/fedilogue.go index 863f11c..df97bfb 100644 --- a/fedilogue/fedilogue.go +++ b/fedilogue/fedilogue.go @@ -3,8 +3,9 @@ package main import ( "net/http" _ "net/http/pprof" - "regexp" + "context" "runtime" + "regexp" "sync" "time" @@ -82,6 +83,28 @@ func main() { re = regexp.MustCompile("^https?://([^/]*)/(.*)$") matchurl = regexp.MustCompile("http?s://[\\w\\-]+\\.[\\w\\-]+\\S*") + // Start instances located in database + rows, err := pool.Query(context.Background(), "SELECT endpoint FROM instances") + if err != nil { + logErr("Unable to select from instances") + return + } + defer rows.Close() + + for rows.Next() { + var endpoint string + err = rows.Scan(&endpoint) + if err != nil { + logErr("Unable to iterate") + return + } + _, exists := GetRunner(endpoint) + if exists == false { + go StartInstance(endpoint) + } + } + + // Start from Autostart list for _, endpoint := range settings.Autostart { logInfo("Autostarting " + endpoint) _, exists := GetRunner(endpoint) From a9a55719aab2d1a67e78015becd87be5fbd656fe Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Sat, 11 Dec 2021 00:53:43 -0500 Subject: [PATCH 3/3] Removing autostart from configuration --- fedilogue/config.go | 1 - fedilogue/config.jsonc.sample | 10 ---------- fedilogue/fedilogue.go | 12 ++---------- 3 files changed, 2 insertions(+), 21 deletions(-) diff --git a/fedilogue/config.go b/fedilogue/config.go index 611250e..9556873 100644 --- a/fedilogue/config.go +++ b/fedilogue/config.go @@ -33,7 +33,6 @@ type Proxy struct { // Settings - Configuration file structure type Settings struct { - Autostart []string `"json:autostart"` Crawl bool `"json:crawl"` Banned []string `"json:banned"` Alwaysbot []string `"json:alwaysbot"` diff --git a/fedilogue/config.jsonc.sample b/fedilogue/config.jsonc.sample index 88b946e..d83520f 100644 --- a/fedilogue/config.jsonc.sample +++ b/fedilogue/config.jsonc.sample @@ -4,16 +4,6 @@ * https://github.com/neoclide/jsonc.vim */ { - /* - * Automatically start following the following instances's public timeline - * If an external account with the same endpoint exists, the autostart - * will use the authenticated to follow - */ - "autostart": [ - "mastodon.social", - "pleroma.site" - ], - // Should pollers automatically crawl "crawl": true, diff --git a/fedilogue/fedilogue.go b/fedilogue/fedilogue.go index df97bfb..3baf6af 100644 --- a/fedilogue/fedilogue.go +++ b/fedilogue/fedilogue.go @@ -95,20 +95,12 @@ func main() { var endpoint string err = rows.Scan(&endpoint) if err != nil { - logErr("Unable to iterate") + logErr("Unable to iterate database, exiting.") return } _, exists := GetRunner(endpoint) if exists == false { - go StartInstance(endpoint) - } - } - - // Start from Autostart list - for _, endpoint := range settings.Autostart { - logInfo("Autostarting " + endpoint) - _, exists := GetRunner(endpoint) - if exists == false { + logInfo("Autostarting " + endpoint) go StartInstance(endpoint) } }