From 12cf21c33ae47e4f79fbd2c1eda0018274f5ccad Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Tue, 28 Jan 2025 15:41:34 +0000 Subject: [PATCH] Entirely redoing instance identification process --- fedilogue/instance.go | 112 +++++++++++++++++++----------------------- shared/headers.go | 33 +++++++------ 2 files changed, 69 insertions(+), 76 deletions(-) diff --git a/fedilogue/instance.go b/fedilogue/instance.go index d7eb6c8..c4c524d 100644 --- a/fedilogue/instance.go +++ b/fedilogue/instance.go @@ -7,6 +7,9 @@ import ( "net/http" "time" + "os" + "fmt" + "git.farhan.codes/farhan/fedilogue/shared" ) @@ -82,90 +85,77 @@ func UpdateRunner(endpoint string, o shared.RunningInstance) { ri_mutex.Unlock() } + func GetInstanceInfo(endpoint string, o shared.RunningInstance) shared.RunningInstance { - /* Checking order - * Mastodon/Pleroma/Misskey - * Um..nothing else yet - */ logDebug("GetInstanceInfo for ", endpoint) - var nodeinfo shared.NodeInfo - pleromastodon_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.0.json" + wellknown_nodeinfo_uri := "https://" + endpoint + "/.well-known/nodeinfo" - // Checking Mastodon and Pleroma (with .json) - 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, _ := http.NewRequest("GET", wellknown_nodeinfo_uri, nil) req.Header.Set("User-Agent", "Tusky") - misskey_api_resp, err := DoTries(&o, req) + resp, err := DoTries(&o, req) if err != nil { + fmt.Println("Exit 1") o.Software = "Unsupported" return o } else { - defer misskey_api_resp.Body.Close() + defer resp.Body.Close() } - if misskey_api_resp.StatusCode == 200 { - err = json.NewDecoder(misskey_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 misskey_api_resp.Body.Close() - return o - } + if resp.StatusCode != 200 { + fmt.Println("Exit 2") + o.Software = "Unsupported" + return o } - // Check for Lemmy (With Json) - lemmy_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.1" - req, _ = http.NewRequest("GET", lemmy_nodeinfo_uri, nil) + var wellknownnodeinfo shared.WellKnownNodeInfo + err = json.NewDecoder(resp.Body).Decode(&wellknownnodeinfo) + 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") - lemmy_api_resp, err := DoTries(&o, req) + respActual, err := DoTries(&o, req) if err != nil { + fmt.Println("Exit 5") o.Software = "Unsupported" return o } else { - defer lemmy_api_resp.Body.Close() + defer respActual.Body.Close() } - if lemmy_api_resp.StatusCode == 200 { - err = json.NewDecoder(lemmy_api_resp.Body).Decode(&nodeinfo) - if err == nil { - logDebug("Found a new Lemmy instance: " + endpoint) - 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 - } + if respActual.StatusCode != 200 { + fmt.Println("Exit 6") + o.Software = "Unsupported" + return o } - // Unsupported Software - o.Software = "Unsupported" - o.Version = "Unknown" + var actualnodeinfo shared.ActualNodeInfo + err = json.NewDecoder(respActual.Body).Decode(&actualnodeinfo) + 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 } diff --git a/shared/headers.go b/shared/headers.go index 305c463..30ebf36 100644 --- a/shared/headers.go +++ b/shared/headers.go @@ -70,21 +70,6 @@ type RunningInstance struct { 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 { Id string `"json:id"` Type string `"json:type"` @@ -100,3 +85,21 @@ type Userinfo struct { ManuallyApprovesFollowers string `"json:manuallyApprovesFollowers"` 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"` +}