fedilogue/engine/instance.go

96 lines
2.6 KiB
Go
Raw Normal View History

package main
import (
"github.com/microcosm-cc/bluemonday"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
2020-12-06 18:05:17 -05:00
"regexp"
"time"
2020-11-24 20:36:47 -05:00
"log"
)
2020-11-17 22:35:59 -05:00
var p *bluemonday.Policy
2020-12-06 18:05:17 -05:00
var spaceReg *regexp.Regexp
2020-11-17 22:35:59 -05:00
// Change this to return a proper "err"
func GetNodeInfo(endpoint string) (NodeInfo) {
/* Checking order
* Mastodon/Pleroma
* Um..nothing else yet
*/
2020-12-07 15:47:44 -05:00
pleromastodon_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.0.json"
//http_client := http.Client{Timeout: 10 * time.Second}
http_client := http.Client{}
2020-12-07 15:47:44 -05:00
pleromastodon_api_resp, err := http_client.Get(pleromastodon_nodeinfo_uri)
if err != nil {
return NodeInfo{}
} else {
defer pleromastodon_api_resp.Body.Close()
}
if pleromastodon_api_resp.StatusCode == 200 {
var nodeinfo NodeInfo
err = json.NewDecoder(pleromastodon_api_resp.Body).Decode(&nodeinfo)
if err == nil {
defer pleromastodon_api_resp.Body.Close()
return nodeinfo
}
}
// Check the front page
2020-12-07 15:47:44 -05:00
index_uri := "https://" + endpoint + "/"
resp_index, err := http_client.Get(index_uri)
if err != nil {
log.Print("Unable to connect to " + endpoint + ", giving up")
return NodeInfo{}
}
defer resp_index.Body.Close()
indexbin, err := ioutil.ReadAll(resp_index.Body)
if err != nil {
log.Print("Unable to read index of " + endpoint + ", giving up")
return NodeInfo{}
}
indexstr := string(indexbin)
nodeinfo := NodeInfo{}
if strings.Contains(indexstr, "Pleroma") || strings.Contains(indexstr, "Soapbox") {
log.Print("Manual view: Pleroma" + endpoint)
nodeinfo.Software.Name = "pleroma"
nodeinfo.Software.Version = "guess"
} else if strings.Contains(indexstr, "Mastodon") {
log.Print("Manual view: Mastodon" + endpoint)
nodeinfo.Software.Name = "mastodon"
nodeinfo.Software.Version = "guess"
} else if strings.Contains(indexstr, "Gab") {
log.Print("Manual view: Gab" + endpoint)
nodeinfo.Software.Name = "gab"
nodeinfo.Software.Version = "guess"
}
return nodeinfo
}
func StartInstance(endpoint string, reportPostChan chan ReportPost) {
nodeinfo := GetNodeInfo(endpoint)
if nodeinfo.Software.Name == "" {
var m = runninginstances[endpoint]
m.Software = ""
m.LastRun = time.Now().Format(time.RFC3339)
2020-11-17 19:57:39 -05:00
m.Status = UNSUPPORTED_INSTANCE
ri_mutex.Lock()
runninginstances[endpoint] = m
ri_mutex.Unlock()
return
}
if nodeinfo.Software.Name == "pleroma" {
log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name)
PollMastodonPleroma(endpoint, reportPostChan)
} else if nodeinfo.Software.Name == "mastodon" {
log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name)
StreamMastodon(endpoint, reportPostChan)
}
}