using the same http client, adding it to running instances

some web work
lost track of what's going on and I'm not accountable to anyone...so screw it
This commit is contained in:
farhan 2020-12-16 02:41:45 +00:00
parent 3d196ccf41
commit a50916eb36
6 changed files with 98 additions and 58 deletions

View File

@ -41,12 +41,12 @@ func writePost(pool *pgxpool.Pool, reportpost ReportPost) {
} }
// Insert new post if new // Insert new post if new
_, err = conn.Exec(context.Background(), "INSERT INTO posts (uri, content, created_at, normalized, account_id, posthash) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (posthash) DO NOTHING", reportpost.Url, reportpost.Content, reportpost.Created_at, reportpost.normalized, accountid, reportpost.posthash) _, err = conn.Exec(context.Background(), "INSERT INTO posts (uri, content, created_at, normalized, account_id, posthash) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (posthash) DO NOTHING", reportpost.Uri, reportpost.Content, reportpost.Created_at, reportpost.normalized, accountid, reportpost.posthash)
if err != nil { // For now I want to know why this failed. if err != nil { // For now I want to know why this failed.
log.Print("Second ", err) log.Print("Second ", err)
log.Print("--------------------------") log.Print("--------------------------")
log.Print("Reported error: ", err) log.Print("Reported error: ", err)
log.Print("Url: ", reportpost.Url) log.Print("Uri: ", reportpost.Uri)
log.Print("Content: ", reportpost.Content) log.Print("Content: ", reportpost.Content)
log.Print("Created_at: ", reportpost.Created_at) log.Print("Created_at: ", reportpost.Created_at)
log.Print("normalized: ", reportpost.normalized) log.Print("normalized: ", reportpost.normalized)

View File

@ -1,5 +1,9 @@
package main package main
import (
"net/http"
)
const ( const (
NEW_INSTANCE = 0 NEW_INSTANCE = 0
RUNNING = 200 RUNNING = 200
@ -22,7 +26,7 @@ type ReportPost struct {
// Retrieved values // Retrieved values
Id string `json:"id"` Id string `json:"id"`
Url string `json:"uri"` Uri string `json:"uri"`
Account AccountType Account AccountType
Content string `json:"content"` Content string `json:"content"`
Created_at string `json:"created_at"` Created_at string `json:"created_at"`
@ -38,7 +42,7 @@ type AccountType struct {
Bot bool `json:"bot"` Bot bool `json:"bot"`
Created_at string `json:"created_at"` Created_at string `json:"created_at"`
Display_name string `json:"display_name"` Display_name string `json:"display_name"`
Url string `json:"uri"` Url string `json:"url"`
} }
// Instance's new min_id value // Instance's new min_id value
@ -47,6 +51,7 @@ type RunningInstance struct {
Status int `json:"status"` Status int `json:"status"`
LastRun string `json:"lastrun"` LastRun string `json:"lastrun"`
CaptureType string `json:"capturetype"` CaptureType string `json:"capturetype"`
client *http.Client
} }
type NodeInfoSoftware struct { type NodeInfoSoftware struct {
@ -68,3 +73,20 @@ type ResponseBack struct {
Message string `json:"Message"` Message string `json:"Message"`
RunningInstances map[string]RunningInstance `json:"RunningInstances"` RunningInstances map[string]RunningInstance `json:"RunningInstances"`
} }
type Userinfo struct {
Id string `"json:id"`
Type string `"json:type"`
Following string `"json:following"`
Followers string `"json:followers"`
Inbox string `"json:inbox"`
Outbox string `"json:outbox"`
Featured string `"json:featured"`
PreferredUsername string `"json:preferredUsername"`
Name string `"json:name"`
Summary string `"json:summary"`
Url string `"json:Url"`
ManuallyApprovesFollowers string `"json:manuallyApprovesFollowers"`
Discoverable string `"json:discoverable"`
}

View File

@ -15,17 +15,16 @@ var p *bluemonday.Policy
var spaceReg *regexp.Regexp var spaceReg *regexp.Regexp
// Change this to return a proper "err" // Change this to return a proper "err"
func GetNodeInfo(endpoint string) (NodeInfo) { func GetNodeInfo(endpoint string) (http.Client, NodeInfo) {
/* Checking order /* Checking order
* Mastodon/Pleroma * Mastodon/Pleroma
* Um..nothing else yet * Um..nothing else yet
*/ */
pleromastodon_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.0.json" pleromastodon_nodeinfo_uri := "https://" + endpoint + "/nodeinfo/2.0.json"
//http_client := http.Client{Timeout: 10 * time.Second}
http_client := http.Client{} http_client := http.Client{}
pleromastodon_api_resp, err := http_client.Get(pleromastodon_nodeinfo_uri) pleromastodon_api_resp, err := http_client.Get(pleromastodon_nodeinfo_uri)
if err != nil { if err != nil {
return NodeInfo{} return http_client, NodeInfo{}
} else { } else {
defer pleromastodon_api_resp.Body.Close() defer pleromastodon_api_resp.Body.Close()
} }
@ -35,7 +34,7 @@ func GetNodeInfo(endpoint string) (NodeInfo) {
err = json.NewDecoder(pleromastodon_api_resp.Body).Decode(&nodeinfo) err = json.NewDecoder(pleromastodon_api_resp.Body).Decode(&nodeinfo)
if err == nil { if err == nil {
defer pleromastodon_api_resp.Body.Close() defer pleromastodon_api_resp.Body.Close()
return nodeinfo return http_client, nodeinfo
} }
} }
@ -44,13 +43,13 @@ func GetNodeInfo(endpoint string) (NodeInfo) {
resp_index, err := http_client.Get(index_uri) resp_index, err := http_client.Get(index_uri)
if err != nil { if err != nil {
log.Print("Unable to connect to " + endpoint + ", giving up") log.Print("Unable to connect to " + endpoint + ", giving up")
return NodeInfo{} return http_client, NodeInfo{}
} }
defer resp_index.Body.Close() defer resp_index.Body.Close()
indexbin, err := ioutil.ReadAll(resp_index.Body) indexbin, err := ioutil.ReadAll(resp_index.Body)
if err != nil { if err != nil {
log.Print("Unable to read index of " + endpoint + ", giving up") log.Print("Unable to read index of " + endpoint + ", giving up")
return NodeInfo{} return http_client, NodeInfo{}
} }
indexstr := string(indexbin) indexstr := string(indexbin)
nodeinfo := NodeInfo{} nodeinfo := NodeInfo{}
@ -68,30 +67,29 @@ func GetNodeInfo(endpoint string) (NodeInfo) {
nodeinfo.Software.Version = "guess" nodeinfo.Software.Version = "guess"
} }
return nodeinfo return http_client, nodeinfo
} }
func StartInstance(endpoint string, reportPostChan chan ReportPost) { func StartInstance(endpoint string, reportPostChan chan ReportPost) {
nodeinfo := GetNodeInfo(endpoint) http_client, nodeinfo := GetNodeInfo(endpoint)
if nodeinfo.Software.Name == "" { ri_mutex.Lock()
m := runninginstances[endpoint] m := runninginstances[endpoint]
if nodeinfo.Software.Name == "" {
m.Software = "" m.Software = ""
m.LastRun = time.Now().Format(time.RFC3339) m.LastRun = time.Now().Format(time.RFC3339)
m.Status = UNSUPPORTED_INSTANCE m.Status = UNSUPPORTED_INSTANCE
ri_mutex.Lock()
runninginstances[endpoint] = m runninginstances[endpoint] = m
ri_mutex.Unlock() ri_mutex.Unlock()
return return
} }
ri_mutex.Lock() m.client = &http_client
m := runninginstances[endpoint]
if nodeinfo.Software.Name == "pleroma" { if nodeinfo.Software.Name == "pleroma" {
log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name) log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name)
m.CaptureType = "Poll" m.CaptureType = "Poll"
runninginstances[endpoint] = m runninginstances[endpoint] = m
ri_mutex.Unlock() ri_mutex.Unlock()
PollMastodonPleroma(endpoint, reportPostChan) PollMastodonPleroma(endpoint, reportPostChan, http_client)
} else if nodeinfo.Software.Name == "mastodon" { } else if nodeinfo.Software.Name == "mastodon" {
log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name) log.Print("Starting " + endpoint + " as " + nodeinfo.Software.Name)
m.CaptureType = "Stream" m.CaptureType = "Stream"

View File

@ -12,12 +12,12 @@ import (
"log" "log"
) )
func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) { func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost, http_client http.Client) {
newposts := make([]ReportPost, 0) newposts := make([]ReportPost, 0)
min_id := "" min_id := ""
http_client := http.Client{} // http_client := http.Client{}
parsing_error := 0 parsing_error := 0
unprocess_error := 0 unprocess_error := 0
use_auth := false use_auth := false
@ -46,7 +46,6 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) {
} }
} }
for { for {
ri_mutex.Lock() ri_mutex.Lock()
m := runninginstances[endpoint] m := runninginstances[endpoint]
@ -151,7 +150,7 @@ func PollMastodonPleroma(endpoint string, reportPostChan chan ReportPost) {
} }
// Calculate the post hash // Calculate the post hash
fmt.Fprint(posthash, newpost.Url) fmt.Fprint(posthash, newpost.Uri)
fmt.Fprint(posthash, newpost.normalized) fmt.Fprint(posthash, newpost.normalized)
fmt.Fprint(posthash, newpost.Account.Acct) fmt.Fprint(posthash, newpost.Account.Acct)
fmt.Fprint(posthash, newpost.Account.Display_name) fmt.Fprint(posthash, newpost.Account.Display_name)

View File

@ -106,7 +106,7 @@ func StreamMastodon(endpoint string, reportPostChan chan ReportPost) {
} }
// Calculate the post hash // Calculate the post hash
fmt.Fprint(posthash, newpost.Url) fmt.Fprint(posthash, newpost.Uri)
fmt.Fprint(posthash, newpost.normalized) fmt.Fprint(posthash, newpost.normalized)
fmt.Fprint(posthash, newpost.Account.Acct) fmt.Fprint(posthash, newpost.Account.Acct)
fmt.Fprint(posthash, newpost.Account.Display_name) fmt.Fprint(posthash, newpost.Account.Display_name)

View File

@ -6,6 +6,9 @@ import (
"net/http" "net/http"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"html"
"time"
"strings"
"os" "os"
) )
@ -91,7 +94,8 @@ func webfinger(w http.ResponseWriter, r *http.Request) {
} }
} }
func inbox(w http.ResponseWriter, r *http.Request) { func inboxHandler(reportPostChan chan ReportPost) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println("PATH --> ", r.URL.Path) fmt.Println("PATH --> ", r.URL.Path)
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
@ -103,7 +107,7 @@ func inbox(w http.ResponseWriter, r *http.Request) {
var findtype FindType var findtype FindType
err = json.Unmarshal(body, &findtype) err = json.Unmarshal(body, &findtype)
fmt.Println(findtype.Type) fmt.Println(string(body))
switch findtype.Type { switch findtype.Type {
case "Create": case "Create":
@ -114,6 +118,22 @@ func inbox(w http.ResponseWriter, r *http.Request) {
fmt.Println("Ignore the post here") fmt.Println("Ignore the post here")
} }
fmt.Println("Content: ", createobject.Content) fmt.Println("Content: ", createobject.Content)
var newpost ReportPost
newpost.Uri = findtype.Id
newpost.Content = createobject.Content
newpost.Created_at = findtype.Published
newpost.normalized = html.UnescapeString(strings.ToLower(p.Sanitize(newpost.Content)))
newpost.normalized = strings.ReplaceAll(newpost.normalized, "\t", " ")
newpost.normalized = spaceReg.ReplaceAllString(newpost.normalized, " ")
newpost.Account.Acct = "aaa@bbb"
newpost.Account.Avatar = "ttt"
newpost.Account.Bot = false
newpost.Account.Created_at = time.Now().Format(time.RFC3339)
newpost.Account.Display_name = "Fqrhqn"
newpost.Account.Url = "qwerty"
reportPostChan <- newpost
case "Like": case "Like":
case "Announcement": case "Announcement":
case "Delete": case "Delete":
@ -130,6 +150,7 @@ func inbox(w http.ResponseWriter, r *http.Request) {
fmt.Println("Type: ", findtype.Type) fmt.Println("Type: ", findtype.Type)
fmt.Println("Object: ", string(findtype.Object)) fmt.Println("Object: ", string(findtype.Object))
}
} }
func users_fedilogue_followers(w http.ResponseWriter, r *http.Request) { func users_fedilogue_followers(w http.ResponseWriter, r *http.Request) {
@ -218,7 +239,7 @@ func errorHandler(w http.ResponseWriter, r *http.Request) {
func webmain(reportPostChan chan ReportPost) { func webmain(reportPostChan chan ReportPost) {
http.HandleFunc("/.well-known/webfinger", webfinger) http.HandleFunc("/.well-known/webfinger", webfinger)
http.HandleFunc("/.well-known/host-meta", hostmeta) http.HandleFunc("/.well-known/host-meta", hostmeta)
http.HandleFunc("/inbox", inbox) http.HandleFunc("/inbox", inboxHandler(reportPostChan))
http.HandleFunc("/users/fedilogue", users_fedilogue) http.HandleFunc("/users/fedilogue", users_fedilogue)
http.HandleFunc("/users/fedilogue/followers", users_fedilogue_followers) http.HandleFunc("/users/fedilogue/followers", users_fedilogue_followers)
http.HandleFunc("/users/fedilogue/following", users_fedilogue_following) http.HandleFunc("/users/fedilogue/following", users_fedilogue_following)