alternate way to check server + authorization failure checks

This commit is contained in:
farhan 2020-11-19 00:38:40 -05:00
parent 69a555a144
commit 27cc6a5d9a
2 changed files with 65 additions and 23 deletions

View File

@ -3,6 +3,8 @@ package main
const ( const (
NEW_INSTANCE = 0 NEW_INSTANCE = 0
RUNNING = 200 RUNNING = 200
UNAUTHORIZED = 401
UNPROCESSABLE_ENTITY = 422
TOOMANYREQUESTS = 429 TOOMANYREQUESTS = 429
CLIENT_ISSUE = 600 CLIENT_ISSUE = 600
ONION_PROTOCOL = 601 ONION_PROTOCOL = 601

View File

@ -38,7 +38,16 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) {
ri_mutex.Unlock() ri_mutex.Unlock()
return 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 // Apparently you just need to do this to throw away the body
_, _ = ioutil.ReadAll(resp.Body) _, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close() // Release as soon as done resp.Body.Close() // Release as soon as done
@ -49,23 +58,15 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) {
time.Sleep(time.Second * 30) time.Sleep(time.Second * 30)
continue continue
} }
body, err := ioutil.ReadAll(resp.Body) err = json.NewDecoder(resp.Body).Decode(&newposts)
if err != nil { if err != nil {
fmt.Println("Error Message 1:", resp.StatusCode, err, endpoint, resp.Status)
ri_mutex.Lock() ri_mutex.Lock()
m.Status = BAD_RESPONSE m.Status = BAD_RESPONSE
runninginstances[endpoint] = m runninginstances[endpoint] = m
ri_mutex.Unlock() ri_mutex.Unlock()
return 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 resp.Body.Close() // Release as soon as done
ri_mutex.Lock() ri_mutex.Lock()
@ -119,22 +120,61 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) {
// Change this to return a proper "err" // Change this to return a proper "err"
func GetNodeInfo(endpoint string) (NodeInfo) { func GetNodeInfo(endpoint string) (NodeInfo) {
var nodeinfo 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) /* Not everyone implements this */
err = json.Unmarshal(body, &nodeinfo) 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 { if err != nil {
fmt.Println("Make a legit error here") fmt.Println("Make a legit error here", err)
fmt.Println("Unmarshal 2");
return NodeInfo{} 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 return nodeinfo
} }