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
type Settings struct {
Autostart []string `"json:autostart"`
Crawl bool `"json:crawl"`
Banned []string `"json:banned"`
Alwaysbot []string `"json:alwaysbot"`

View File

@ -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,

View File

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

View File

@ -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)