Entirely redoing instance identification process

This commit is contained in:
farhan 2025-01-28 15:41:34 +00:00
parent b783f21ccd
commit 12cf21c33a
2 changed files with 69 additions and 76 deletions

View File

@ -7,6 +7,9 @@ import (
"net/http" "net/http"
"time" "time"
"os"
"fmt"
"git.farhan.codes/farhan/fedilogue/shared" "git.farhan.codes/farhan/fedilogue/shared"
) )
@ -82,90 +85,77 @@ func UpdateRunner(endpoint string, o shared.RunningInstance) {
ri_mutex.Unlock() ri_mutex.Unlock()
} }
func GetInstanceInfo(endpoint string, o shared.RunningInstance) shared.RunningInstance { func GetInstanceInfo(endpoint string, o shared.RunningInstance) shared.RunningInstance {
/* Checking order
* Mastodon/Pleroma/Misskey
* Um..nothing else yet
*/
logDebug("GetInstanceInfo for ", endpoint) logDebug("GetInstanceInfo for ", endpoint)
var nodeinfo shared.NodeInfo wellknown_nodeinfo_uri := "https://" + endpoint + "/.well-known/nodeinfo"
pleromastodon_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.0.json"
// Checking Mastodon and Pleroma (with .json) req, _ := http.NewRequest("GET", wellknown_nodeinfo_uri, nil)
reqjson, _ := http.NewRequest("GET", pleromastodon_nodeinfo_uri, nil)
reqjson.Header.Set("User-Agent", "Tusky")
pleromastodon_api_resp, err := DoTries(&o, reqjson)
if err != nil {
o.Software = "Unsupported"
return o
} else {
defer pleromastodon_api_resp.Body.Close()
}
if pleromastodon_api_resp.StatusCode == 200 {
err = json.NewDecoder(pleromastodon_api_resp.Body).Decode(&nodeinfo)
if err == nil {
o.Software = nodeinfo.Software.Name
o.Version = nodeinfo.Software.Version
o.LastRun = time.Now().Format(time.RFC3339)
defer pleromastodon_api_resp.Body.Close()
return o
}
}
// Checking for Misskey (without .json)
misskey_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.0"
req, _ := http.NewRequest("GET", misskey_nodeinfo_uri, nil)
req.Header.Set("User-Agent", "Tusky") req.Header.Set("User-Agent", "Tusky")
misskey_api_resp, err := DoTries(&o, req) resp, err := DoTries(&o, req)
if err != nil { if err != nil {
fmt.Println("Exit 1")
o.Software = "Unsupported" o.Software = "Unsupported"
return o return o
} else { } else {
defer misskey_api_resp.Body.Close() defer resp.Body.Close()
} }
if misskey_api_resp.StatusCode == 200 { if resp.StatusCode != 200 {
err = json.NewDecoder(misskey_api_resp.Body).Decode(&nodeinfo) fmt.Println("Exit 2")
if err == nil { o.Software = "Unsupported"
o.Software = nodeinfo.Software.Name return o
o.Version = nodeinfo.Software.Version
o.LastRun = time.Now().Format(time.RFC3339)
defer misskey_api_resp.Body.Close()
return o
}
} }
// Check for Lemmy (With Json) var wellknownnodeinfo shared.WellKnownNodeInfo
lemmy_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.1" err = json.NewDecoder(resp.Body).Decode(&wellknownnodeinfo)
req, _ = http.NewRequest("GET", lemmy_nodeinfo_uri, nil) if (err != nil) {
fmt.Println("Exit 3")
o.Software = "Unsupported"
return o
}
// There can be multiple entries of /.well-known/nodeinfo entries, we want the last
length := len(wellknownnodeinfo.Links)
if length == 0 {
fmt.Println("Exit 4")
o.Software = "Unsupported"
return o
}
actual_nodeinfo_uri := wellknownnodeinfo.Links[length-1].Href
req, _ = http.NewRequest("GET", actual_nodeinfo_uri, nil)
req.Header.Set("User-Agent", "Tusky") req.Header.Set("User-Agent", "Tusky")
lemmy_api_resp, err := DoTries(&o, req) respActual, err := DoTries(&o, req)
if err != nil { if err != nil {
fmt.Println("Exit 5")
o.Software = "Unsupported" o.Software = "Unsupported"
return o return o
} else { } else {
defer lemmy_api_resp.Body.Close() defer respActual.Body.Close()
} }
if lemmy_api_resp.StatusCode == 200 { if respActual.StatusCode != 200 {
err = json.NewDecoder(lemmy_api_resp.Body).Decode(&nodeinfo) fmt.Println("Exit 6")
if err == nil { o.Software = "Unsupported"
logDebug("Found a new Lemmy instance: " + endpoint) return o
o.Software = nodeinfo.Software.Name
o.Version = nodeinfo.Software.Version
o.LastRun = time.Now().Format(time.RFC3339)
defer lemmy_api_resp.Body.Close()
return o
}
} }
// Unsupported Software var actualnodeinfo shared.ActualNodeInfo
o.Software = "Unsupported" err = json.NewDecoder(respActual.Body).Decode(&actualnodeinfo)
o.Version = "Unknown" if (err != nil) {
fmt.Println("Exit 7")
fmt.Println(err)
o.Software = "Unsupported"
return o
}
fmt.Println(actualnodeinfo)
fmt.Println("Final exit")
os.Exit(1)
return o return o
} }

View File

@ -70,21 +70,6 @@ type RunningInstance struct {
Recentactors *UniqueFifo `json:"-"` Recentactors *UniqueFifo `json:"-"`
} }
type NodeInfoSoftware struct {
Name string `json:"name"`
Version string `json:"version"`
}
type NodeInfo struct {
Software NodeInfoSoftware `json:"software"`
}
//type ResponseBack struct {
///Type string `json:"Type"`
//Message string `json:"Message"`
// RunningInstances map[string]RunningInstance `json:"RunningInstances"`
///}
type Userinfo struct { type Userinfo struct {
Id string `"json:id"` Id string `"json:id"`
Type string `"json:type"` Type string `"json:type"`
@ -100,3 +85,21 @@ type Userinfo struct {
ManuallyApprovesFollowers string `"json:manuallyApprovesFollowers"` ManuallyApprovesFollowers string `"json:manuallyApprovesFollowers"`
Discoverable string `"json:discoverable"` Discoverable string `"json:discoverable"`
} }
type SoftwareActualNodeInfo struct {
Name string `json:"name"`
Version string `json:"version"`
}
type ActualNodeInfo struct {
Software SoftwareActualNodeInfo `json:"software"`
}
type WellKnownNodeInfoLinks struct {
Rel string `json:"rel"`
Href string `json:"href"`
}
type WellKnownNodeInfo struct {
Links []WellKnownNodeInfoLinks `json:"links"`
}