diff --git a/poll/headers.go b/poll/headers.go index 0ec69e7..6c9e920 100644 --- a/poll/headers.go +++ b/poll/headers.go @@ -3,6 +3,8 @@ package main const ( NEW_INSTANCE = 0 RUNNING = 200 + UNAUTHORIZED = 401 + UNPROCESSABLE_ENTITY = 422 TOOMANYREQUESTS = 429 CLIENT_ISSUE = 600 ONION_PROTOCOL = 601 diff --git a/poll/instance.go b/poll/instance.go index 17a2654..8fac2b7 100644 --- a/poll/instance.go +++ b/poll/instance.go @@ -38,7 +38,16 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) { ri_mutex.Unlock() return } - if resp.StatusCode == 429 { + + if resp.StatusCode == UNAUTHORIZED || resp.StatusCode == UNPROCESSABLE_ENTITY { + _, _ = ioutil.ReadAll(resp.Body) + resp.Body.Close() // Release as soon as done + ri_mutex.Lock() + m.Status = UNAUTHORIZED + runninginstances[endpoint] = m + ri_mutex.Unlock() + return + } else if resp.StatusCode == TOOMANYREQUESTS { // Apparently you just need to do this to throw away the body _, _ = ioutil.ReadAll(resp.Body) resp.Body.Close() // Release as soon as done @@ -49,23 +58,15 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) { time.Sleep(time.Second * 30) continue } - body, err := ioutil.ReadAll(resp.Body) + err = json.NewDecoder(resp.Body).Decode(&newposts) if err != nil { + fmt.Println("Error Message 1:", resp.StatusCode, err, endpoint, resp.Status) ri_mutex.Lock() m.Status = BAD_RESPONSE runninginstances[endpoint] = m ri_mutex.Unlock() return } - err = json.Unmarshal(body, &newposts) - if err != nil { - ri_mutex.Lock() - m.Status = BAD_RESPONSE - runninginstances[endpoint] = m - ri_mutex.Unlock() - return - } - resp.Body.Close() // Release as soon as done ri_mutex.Lock() @@ -119,22 +120,61 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) { // Change this to return a proper "err" func GetNodeInfo(endpoint string) (NodeInfo) { var nodeinfo NodeInfo - api_nodeinfo := "https://" + endpoint + "/nodeinfo/2.0.json" - http_client := http.Client{Timeout: 5 * time.Second} - resp, err := http_client.Get(api_nodeinfo) - if err != nil { - fmt.Println("Make a legit error here") - return NodeInfo{} - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - err = json.Unmarshal(body, &nodeinfo) + /* Not everyone implements this */ + api_nodeinfo := "https://" + endpoint + "/nodeinfo/2.0.json" + http_client := http.Client{Timeout: 10 * time.Second} + resp_node, err := http_client.Get(api_nodeinfo) if err != nil { - fmt.Println("Make a legit error here") - fmt.Println("Unmarshal 2"); + fmt.Println("Make a legit error here", err) return NodeInfo{} } + defer resp_node.Body.Close() + + /* + Some Pleroma and Mastodon instances hide their /nodeinfo/2.0.json + So this attempts to check the index URL for references to Mastodon + or Pleroma. + */ + if resp_node.StatusCode == 200 { + err = json.NewDecoder(resp_node.Body).Decode(&nodeinfo) + if err != nil { + fmt.Println("Error Message 2:", resp_node.StatusCode, err, endpoint, resp_node.Status, api_nodeinfo) + return NodeInfo{} + } + } + if resp_node.StatusCode == 404 { + indexurl := "https://" + endpoint + "/" + resp_index, err := http_client.Get(indexurl) + defer resp_index.Body.Close() + if err != nil { + fmt.Println("Error Message 2:", resp_index.StatusCode, err, endpoint, resp_index.Status, api_nodeinfo) + return NodeInfo{} + } + indexbin, err := ioutil.ReadAll(resp_index.Body) + if err != nil { + fmt.Println("Error Message 2:", resp_index.StatusCode, err, endpoint, resp_index.Status, api_nodeinfo) + return NodeInfo{} + } + indexstr := string(indexbin) + if strings.Contains(indexstr, "Pleroma") { + nodeinfo.Software.Name = "pleroma" + fmt.Println("It is Pleroma: " + endpoint) + } else if strings.Contains(indexstr, "Mastodon") { + nodeinfo.Software.Name = "mastodon" + fmt.Println("It is Mastodon: " + endpoint) + } else { + return NodeInfo{} + } + } + + /* + err = json.NewDecoder(resp.Body).Decode(&nodeinfo) + if err != nil { + fmt.Println("Error Message 2:", resp.StatusCode, err, endpoint, resp.Status, api_nodeinfo) + return NodeInfo{} + } + */ return nodeinfo }