Merge branch 'LogNodesInDatabase' into 'master'

Log instances in database

See merge request khanzf/fedilogue!12
This commit is contained in:
Fikrān Mutasā'il 2021-12-11 05:57:40 +00:00
commit b1e8f08488
4 changed files with 44 additions and 16 deletions

View File

@ -33,7 +33,6 @@ type Proxy struct {
// Settings - Configuration file structure // Settings - Configuration file structure
type Settings struct { type Settings struct {
Autostart []string `"json:autostart"`
Crawl bool `"json:crawl"` Crawl bool `"json:crawl"`
Banned []string `"json:banned"` Banned []string `"json:banned"`
Alwaysbot []string `"json:alwaysbot"` Alwaysbot []string `"json:alwaysbot"`

View File

@ -4,16 +4,6 @@
* https://github.com/neoclide/jsonc.vim * 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 // Should pollers automatically crawl
"crawl": true, "crawl": true,

View File

@ -3,8 +3,9 @@ package main
import ( import (
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"regexp" "context"
"runtime" "runtime"
"regexp"
"sync" "sync"
"time" "time"
@ -82,10 +83,24 @@ func main() {
re = regexp.MustCompile("^https?://([^/]*)/(.*)$") re = regexp.MustCompile("^https?://([^/]*)/(.*)$")
matchurl = regexp.MustCompile("http?s://[\\w\\-]+\\.[\\w\\-]+\\S*") matchurl = regexp.MustCompile("http?s://[\\w\\-]+\\.[\\w\\-]+\\S*")
for _, endpoint := range settings.Autostart { // Start instances located in database
logInfo("Autostarting " + endpoint) 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 database, exiting.")
return
}
_, exists := GetRunner(endpoint) _, exists := GetRunner(endpoint)
if exists == false { if exists == false {
logInfo("Autostarting " + endpoint)
go StartInstance(endpoint) go StartInstance(endpoint)
} }
} }

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"math/rand" "math/rand"
"context"
"strings" "strings"
"time" "time"
"net" "net"
@ -83,7 +84,7 @@ func UpdateRunner(endpoint string, o RunningInstance) {
ri_mutex.Unlock() ri_mutex.Unlock()
} }
func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance { func GetInstanceInfo(endpoint string, o RunningInstance) RunningInstance {
/* Checking order /* Checking order
* Mastodon/Pleroma * Mastodon/Pleroma
* Um..nothing else yet * Um..nothing else yet
@ -96,6 +97,7 @@ func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance {
pleromastodon_api_resp, err := DoTries(&o, req) pleromastodon_api_resp, err := DoTries(&o, req)
if err != nil { if err != nil {
o.Software = "Unsupported"
return o return o
} else { } else {
defer pleromastodon_api_resp.Body.Close() defer pleromastodon_api_resp.Body.Close()
@ -120,6 +122,7 @@ func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance {
o.LastRun = time.Now().Format(time.RFC3339) o.LastRun = time.Now().Format(time.RFC3339)
if err != nil { if err != nil {
o.Status = UNSUPPORTED_INSTANCE o.Status = UNSUPPORTED_INSTANCE
o.Software = "Unsupported"
logWarn("Unable to connect to " + endpoint + ", giving up") logWarn("Unable to connect to " + endpoint + ", giving up")
return o return o
} }
@ -128,6 +131,7 @@ func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance {
indexbin, err := ioutil.ReadAll(resp_index.Body) indexbin, err := ioutil.ReadAll(resp_index.Body)
if err != nil { if err != nil {
o.Status = UNSUPPORTED_INSTANCE o.Status = UNSUPPORTED_INSTANCE
o.Software = "Unsupported"
logWarn("Unable to read index of " + endpoint + ", giving up") logWarn("Unable to read index of " + endpoint + ", giving up")
return o return o
} }
@ -142,11 +146,30 @@ func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance {
} else if strings.Contains(indexstr, "Gab") { } else if strings.Contains(indexstr, "Gab") {
o.Software = "gab" o.Software = "gab"
o.Version = "guess" o.Version = "guess"
} else {
o.Software = "Unsupported"
o.Version = "Unknown"
} }
return o 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) { func CheckInstance(newinstance string, callerEndpoint string) {
if settings.Crawl == true && stringexists(newinstance, settings.Banned) == false { if settings.Crawl == true && stringexists(newinstance, settings.Banned) == false {
// Skip over this if its the same as the endpoint or empty // 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 // Check if exists. If so, get the object. If not, create it
o, _ := GetRunner(endpoint) o, _ := GetRunner(endpoint)
o = GetNodeInfo(endpoint, o) o = GetInstanceInfo(endpoint, o)
UpdateRunner(endpoint, o) UpdateRunner(endpoint, o)
LogInstance(endpoint, o)
if o.Software == "pleroma" { if o.Software == "pleroma" {
logConn("Starting " + endpoint + " as " + o.Software + " " + o.Version) logConn("Starting " + endpoint + " as " + o.Software + " " + o.Version)