2020-11-10 21:53:46 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-12-17 04:23:25 +00:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
2020-11-10 21:53:46 -05:00
|
|
|
"io/ioutil"
|
2020-12-17 04:23:25 +00:00
|
|
|
"log"
|
2020-11-10 21:53:46 -05:00
|
|
|
"net/http"
|
2020-12-06 18:05:17 -05:00
|
|
|
"regexp"
|
2020-12-17 04:23:25 +00:00
|
|
|
"strings"
|
2020-11-10 21:53:46 -05:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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-12-29 20:20:02 +00:00
|
|
|
func GetRunner(endpoint string) RunningInstance {
|
2020-12-25 05:18:42 +00:00
|
|
|
ri_mutex.Lock()
|
|
|
|
o, exists := runninginstances[endpoint]
|
|
|
|
if exists == false {
|
|
|
|
o := RunningInstance{}
|
|
|
|
|
2021-01-02 07:00:21 +00:00
|
|
|
//tr := &http.Transport{MaxIdleConns: 10, IdleConnTimeout: 7200 * time.Second}
|
2020-12-29 20:20:02 +00:00
|
|
|
tr := &http.Transport{MaxIdleConns: 10, IdleConnTimeout: 7200 * time.Second}
|
2020-12-25 05:18:42 +00:00
|
|
|
|
|
|
|
o.client = http.Client{Transport: tr}
|
2021-01-02 07:00:21 +00:00
|
|
|
//o.client = http.Client{}
|
2020-12-25 05:18:42 +00:00
|
|
|
o.Status = KEEPALIVE
|
|
|
|
runninginstances[endpoint] = o
|
|
|
|
}
|
|
|
|
ri_mutex.Unlock()
|
|
|
|
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateRunner(endpoint string, o RunningInstance) {
|
|
|
|
ri_mutex.Lock()
|
|
|
|
runninginstances[endpoint] = o
|
|
|
|
ri_mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-12-29 20:20:02 +00:00
|
|
|
func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance {
|
2020-11-25 18:24:56 -05:00
|
|
|
/* Checking order
|
|
|
|
* Mastodon/Pleroma
|
|
|
|
* Um..nothing else yet
|
2020-12-17 04:23:25 +00:00
|
|
|
*/
|
2020-12-25 05:18:42 +00:00
|
|
|
var nodeinfo NodeInfo
|
2020-12-07 15:47:44 -05:00
|
|
|
pleromastodon_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.0.json"
|
2020-12-25 05:18:42 +00:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", pleromastodon_nodeinfo_uri, nil)
|
|
|
|
|
|
|
|
pleromastodon_api_resp, err := o.client.Do(req)
|
2020-11-10 21:53:46 -05:00
|
|
|
if err != nil {
|
2020-12-25 05:18:42 +00:00
|
|
|
return o
|
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 {
|
|
|
|
err = json.NewDecoder(pleromastodon_api_resp.Body).Decode(&nodeinfo)
|
2020-11-28 11:42:31 -05:00
|
|
|
if err == nil {
|
2020-12-25 05:18:42 +00:00
|
|
|
o.Software = nodeinfo.Software.Name
|
2020-12-29 16:38:14 +00:00
|
|
|
o.Version = nodeinfo.Software.Version
|
2020-12-25 05:18:42 +00:00
|
|
|
o.LastRun = time.Now().Format(time.RFC3339)
|
2020-11-28 11:42:31 -05:00
|
|
|
defer pleromastodon_api_resp.Body.Close()
|
2020-12-25 05:18:42 +00:00
|
|
|
return o
|
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 + "/"
|
2020-12-25 05:18:42 +00:00
|
|
|
req, _ = http.NewRequest("GET", index_uri, nil)
|
|
|
|
resp_index, err := o.client.Do(req)
|
|
|
|
o.LastRun = time.Now().Format(time.RFC3339)
|
2020-11-25 18:24:56 -05:00
|
|
|
if err != nil {
|
2020-12-25 05:18:42 +00:00
|
|
|
o.Status = UNSUPPORTED_INSTANCE
|
2020-11-28 11:42:31 -05:00
|
|
|
log.Print("Unable to connect to " + endpoint + ", giving up")
|
2020-12-25 05:18:42 +00:00
|
|
|
return o
|
2020-11-25 18:24:56 -05:00
|
|
|
}
|
|
|
|
defer resp_index.Body.Close()
|
2020-12-25 05:18:42 +00:00
|
|
|
|
2020-11-25 18:24:56 -05:00
|
|
|
indexbin, err := ioutil.ReadAll(resp_index.Body)
|
|
|
|
if err != nil {
|
2020-12-25 05:18:42 +00:00
|
|
|
o.Status = UNSUPPORTED_INSTANCE
|
2020-11-28 11:42:31 -05:00
|
|
|
log.Print("Unable to read index of " + endpoint + ", giving up")
|
2020-12-25 05:18:42 +00:00
|
|
|
return o
|
2020-11-25 18:24:56 -05:00
|
|
|
}
|
|
|
|
indexstr := string(indexbin)
|
2020-12-25 05:18:42 +00:00
|
|
|
|
2020-11-25 18:24:56 -05:00
|
|
|
if strings.Contains(indexstr, "Pleroma") || strings.Contains(indexstr, "Soapbox") {
|
2020-12-25 05:18:42 +00:00
|
|
|
o.Software = "pleroma"
|
|
|
|
o.Version = "guess"
|
2020-11-25 18:24:56 -05:00
|
|
|
} else if strings.Contains(indexstr, "Mastodon") {
|
2020-12-25 05:18:42 +00:00
|
|
|
o.Software = "mastodon"
|
|
|
|
o.Version = "guess"
|
2020-11-25 18:24:56 -05:00
|
|
|
} else if strings.Contains(indexstr, "Gab") {
|
2020-12-25 05:18:42 +00:00
|
|
|
o.Software = "gab"
|
|
|
|
o.Version = "guess"
|
2020-11-19 00:38:40 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
|
2020-12-25 05:18:42 +00:00
|
|
|
return o
|
2020-11-17 18:28:59 -05:00
|
|
|
}
|
|
|
|
|
2020-12-22 20:36:37 +00:00
|
|
|
func StartInstance(endpoint string) {
|
2020-12-25 05:18:42 +00:00
|
|
|
// Check if exists. If so, get the object. If not, create it
|
|
|
|
o := GetRunner(endpoint)
|
|
|
|
|
|
|
|
o = GetNodeInfo(endpoint, o)
|
|
|
|
UpdateRunner(endpoint, o)
|
|
|
|
|
|
|
|
if o.Software == "" {
|
2020-11-10 21:53:46 -05:00
|
|
|
return
|
|
|
|
}
|
2020-11-17 18:28:59 -05:00
|
|
|
|
2020-12-25 05:18:42 +00:00
|
|
|
if o.Software == "pleroma" {
|
2020-12-29 16:38:14 +00:00
|
|
|
log.Print("Starting " + endpoint + " as " + o.Software + " " + o.Version)
|
2020-12-25 05:18:42 +00:00
|
|
|
o.CaptureType = "Poll"
|
|
|
|
UpdateRunner(endpoint, o)
|
|
|
|
PollMastodonPleroma(endpoint, &o)
|
|
|
|
} else if o.Software == "mastodon" {
|
2020-12-29 16:38:14 +00:00
|
|
|
log.Print("Starting " + endpoint + " as " + o.Software + " " + o.Version)
|
2020-12-25 05:18:42 +00:00
|
|
|
o.CaptureType = "Stream"
|
|
|
|
UpdateRunner(endpoint, o)
|
|
|
|
StreamMastodon(endpoint, &o)
|
2020-11-17 18:28:59 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|