Logging instance to database

This commit is contained in:
Farhan Khan 2021-12-10 22:59:21 -05:00
parent ea72c19b37
commit 07c11d360d

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)