2020-11-10 21:53:46 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-12-06 18:05:17 -05:00
|
|
|
"regexp"
|
2020-11-10 21:53:46 -05:00
|
|
|
"time"
|
2020-11-24 20:36:47 -05:00
|
|
|
"log"
|
2020-11-10 21:53:46 -05:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2020-11-10 21:53:46 -05:00
|
|
|
// Change this to return a proper "err"
|
2020-11-17 18:28:59 -05:00
|
|
|
func GetNodeInfo(endpoint string) (NodeInfo) {
|
2020-11-25 18:24:56 -05:00
|
|
|
/* 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"
|
2020-11-25 18:24:56 -05:00
|
|
|
//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)
|
2020-11-10 21:53:46 -05:00
|
|
|
if err != nil {
|
2020-11-17 18:28:59 -05:00
|
|
|
return NodeInfo{}
|
2020-11-25 18:24:56 -05:00
|
|
|
} else {
|
|
|
|
defer pleromastodon_api_resp.Body.Close()
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
2020-11-19 00:38:40 -05:00
|
|
|
|
2020-11-25 18:24:56 -05:00
|
|
|
if pleromastodon_api_resp.StatusCode == 200 {
|
|
|
|
var nodeinfo NodeInfo
|
|
|
|
err = json.NewDecoder(pleromastodon_api_resp.Body).Decode(&nodeinfo)
|
2020-11-28 11:42:31 -05:00
|
|
|
if err == nil {
|
|
|
|
defer pleromastodon_api_resp.Body.Close()
|
|
|
|
return nodeinfo
|
2020-11-19 00:38:40 -05:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 18:24:56 -05:00
|
|
|
|
|
|
|
// Check the front page
|
2020-12-07 15:47:44 -05:00
|
|
|
index_uri := "https://" + endpoint + "/"
|
|
|
|
resp_index, err := http_client.Get(index_uri)
|
2020-11-25 18:24:56 -05:00
|
|
|
if err != nil {
|
2020-11-28 11:42:31 -05:00
|
|
|
log.Print("Unable to connect to " + endpoint + ", giving up")
|
|
|
|
return NodeInfo{}
|
2020-11-25 18:24:56 -05:00
|
|
|
}
|
|
|
|
defer resp_index.Body.Close()
|
|
|
|
indexbin, err := ioutil.ReadAll(resp_index.Body)
|
|
|
|
if err != nil {
|
2020-11-28 11:42:31 -05:00
|
|
|
log.Print("Unable to read index of " + endpoint + ", giving up")
|
|
|
|
return NodeInfo{}
|
2020-11-25 18:24:56 -05:00
|
|
|
}
|
|
|
|
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"
|
2020-11-19 00:38:40 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
|
2020-11-17 18:28:59 -05:00
|
|
|
return nodeinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartInstance(endpoint string, reportPostChan chan ReportPost) {
|
|
|
|
nodeinfo := GetNodeInfo(endpoint)
|
|
|
|
if nodeinfo.Software.Name == "" {
|
|
|
|
var m = runninginstances[endpoint]
|
|
|
|
m.Software = ""
|
2020-11-25 18:24:56 -05:00
|
|
|
m.LastRun = time.Now().Format(time.RFC3339)
|
2020-11-17 19:57:39 -05:00
|
|
|
m.Status = UNSUPPORTED_INSTANCE
|
|
|
|
ri_mutex.Lock()
|
2020-11-17 18:28:59 -05:00
|
|
|
runninginstances[endpoint] = m
|
|
|
|
ri_mutex.Unlock()
|
2020-11-10 21:53:46 -05:00
|
|
|
return
|
|
|
|
}
|
2020-11-17 18:28:59 -05:00
|
|
|
|
2020-12-11 17:20:44 +00:00
|
|
|
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)
|
2020-11-17 18:28:59 -05:00
|
|
|
}
|
|
|
|
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|