104 lines
2.9 KiB
Go
104 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/microcosm-cc/bluemonday"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
"github.com/jackc/pgx/pgxpool"
|
|
)
|
|
|
|
var p *bluemonday.Policy
|
|
var spaceReg *regexp.Regexp
|
|
|
|
// Change this to return a proper "err"
|
|
func GetNodeInfo(endpoint string) (http.Client, NodeInfo) {
|
|
/* Checking order
|
|
* Mastodon/Pleroma
|
|
* Um..nothing else yet
|
|
*/
|
|
pleromastodon_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.0.json"
|
|
http_client := http.Client{}
|
|
pleromastodon_api_resp, err := http_client.Get(pleromastodon_nodeinfo_uri)
|
|
if err != nil {
|
|
return http_client, 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 http_client, nodeinfo
|
|
}
|
|
}
|
|
|
|
// Check the front page
|
|
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 http_client, 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 http_client, 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 http_client, nodeinfo
|
|
}
|
|
|
|
func StartInstance(endpoint string, reportPostChan chan ReportPost, pool *pgxpool.Pool) {
|
|
http_client, nodeinfo := GetNodeInfo(endpoint)
|
|
ri_mutex.Lock()
|
|
m := runninginstances[endpoint]
|
|
if nodeinfo.Software.Name == "" {
|
|
m.Software = ""
|
|
m.LastRun = time.Now().Format(time.RFC3339)
|
|
m.Status = UNSUPPORTED_INSTANCE
|
|
runninginstances[endpoint] = m
|
|
ri_mutex.Unlock()
|
|
return
|
|
}
|
|
|
|
m.client = http_client
|
|
if nodeinfo.Software.Name == "pleroma" {
|
|
log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name)
|
|
m.CaptureType = "Poll"
|
|
runninginstances[endpoint] = m
|
|
ri_mutex.Unlock()
|
|
PollMastodonPleroma(endpoint, reportPostChan, http_client, pool)
|
|
} else if nodeinfo.Software.Name == "mastodon" {
|
|
log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name)
|
|
m.CaptureType = "Stream"
|
|
runninginstances[endpoint] = m
|
|
ri_mutex.Unlock()
|
|
// PollMastodonPleroma(endpoint, reportPostChan, http_client, pool)
|
|
StreamMastodon(endpoint, reportPostChan, pool)
|
|
}
|
|
|
|
}
|