2020-12-11 17:20:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-12-17 04:23:25 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-01-14 19:51:42 +00:00
|
|
|
"net"
|
2020-12-11 17:20:44 +00:00
|
|
|
)
|
|
|
|
|
2020-12-25 05:18:42 +00:00
|
|
|
func StreamMastodon(endpoint string, o *RunningInstance) {
|
2021-01-14 19:51:42 +00:00
|
|
|
tr := &http.Transport{
|
|
|
|
MaxIdleConns: 10,
|
|
|
|
IdleConnTimeout: 7200 * time.Second,
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext,
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_client := http.Client{Transport: tr}
|
2020-12-11 17:20:44 +00:00
|
|
|
|
|
|
|
var oauthData OAuth
|
2020-12-29 16:41:43 +00:00
|
|
|
var retry bool
|
2020-12-11 17:20:44 +00:00
|
|
|
|
2020-12-29 16:38:14 +00:00
|
|
|
for {
|
2020-12-11 17:20:44 +00:00
|
|
|
|
2020-12-29 16:38:14 +00:00
|
|
|
api_timeline := "https://" + endpoint + "/api/v1/streaming/public"
|
|
|
|
req, err := http.NewRequest("GET", api_timeline, nil)
|
2021-01-25 21:06:47 -05:00
|
|
|
req.Header.Set("User-Agent", "Tusky")
|
2020-12-29 16:38:14 +00:00
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Unable to create new request for " + endpoint + ", exiting.")
|
2020-12-29 16:38:14 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-11 17:20:44 +00:00
|
|
|
|
2020-12-29 16:38:14 +00:00
|
|
|
for _, extaccount := range settings.Externalaccounts {
|
|
|
|
if extaccount.Endpoint == endpoint {
|
|
|
|
get_client(endpoint, o)
|
2020-12-11 17:20:44 +00:00
|
|
|
|
2020-12-29 16:38:14 +00:00
|
|
|
err = get_client(endpoint, o)
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logWarn.Print("Unable to register client: ", err)
|
2020-12-29 16:38:14 +00:00
|
|
|
}
|
2020-12-11 17:20:44 +00:00
|
|
|
|
2020-12-29 16:38:14 +00:00
|
|
|
oauthData, err = oauth_login(endpoint, o, extaccount.Username, extaccount.Password)
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logWarn.Print("Unable to login: ", err)
|
2020-12-29 16:38:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Authorization", oauthData.Access_token)
|
2020-12-11 17:20:44 +00:00
|
|
|
|
2020-12-29 16:38:14 +00:00
|
|
|
}
|
2020-12-11 17:20:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 19:51:42 +00:00
|
|
|
var resp *http.Response
|
|
|
|
|
|
|
|
for tries := 0; tries < 10; tries++ {
|
|
|
|
resp, err = stream_client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(time.Minute * 5)
|
|
|
|
logWarn.Print("Failure connecting to " + req.URL.Scheme + "://" + req.URL.Host + req.URL.Path + ", attempt ", tries + 1, ", sleeping for 5 minutes, ", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-12-29 16:38:14 +00:00
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logErr.Print("Unable to stream " + api_timeline + ": ", err)
|
2020-12-29 16:38:14 +00:00
|
|
|
return
|
2020-12-11 17:20:44 +00:00
|
|
|
}
|
2020-12-29 16:38:14 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
ri_mutex.Lock()
|
|
|
|
m := runninginstances[endpoint]
|
|
|
|
m.Status = RUNNING
|
|
|
|
m.LastRun = "Streaming"
|
|
|
|
runninginstances[endpoint] = m
|
|
|
|
ri_mutex.Unlock()
|
|
|
|
|
|
|
|
s := bufio.NewScanner(resp.Body)
|
|
|
|
var name string
|
|
|
|
for s.Scan() {
|
|
|
|
line := s.Text()
|
|
|
|
token := strings.SplitN(line, ":", 2)
|
|
|
|
var newpost ReportPost
|
|
|
|
if len(token) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.TrimSpace(token[0]) {
|
|
|
|
case "event":
|
|
|
|
name = strings.TrimSpace(token[1])
|
|
|
|
continue
|
|
|
|
case "data":
|
|
|
|
switch name {
|
|
|
|
case "update":
|
2020-12-29 16:41:43 +00:00
|
|
|
jsondata := token[1][1:]
|
|
|
|
err := json.Unmarshal([]byte(jsondata), &newpost)
|
2020-12-29 16:38:14 +00:00
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logDebug.Print("Unable to parse data from "+endpoint+", but still connected.")
|
2020-12-29 16:38:14 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-12-29 16:41:43 +00:00
|
|
|
retry = true
|
2020-12-29 16:38:14 +00:00
|
|
|
default:
|
|
|
|
continue
|
2020-12-11 17:20:44 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-29 16:38:14 +00:00
|
|
|
go check_post(newpost.Uri)
|
2020-12-11 17:20:44 +00:00
|
|
|
|
2021-01-10 05:31:51 +00:00
|
|
|
matchset := re.FindStringSubmatch(newpost.Uri)
|
2021-01-10 21:47:08 +00:00
|
|
|
if matchset != nil {
|
|
|
|
newinstance := matchset[1]
|
|
|
|
go CheckInstance(newinstance, endpoint)
|
|
|
|
}
|
2020-12-11 17:20:44 +00:00
|
|
|
}
|
2020-12-29 16:41:43 +00:00
|
|
|
if retry == true {
|
|
|
|
time.Sleep(time.Minute * 30)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2020-12-11 17:20:44 +00:00
|
|
|
}
|
|
|
|
}
|