validating hostname before starting instance

changed hostname identification from account to URI
This commit is contained in:
farhan 2021-01-10 05:31:51 +00:00
parent 48acaa5b0b
commit ec1743905a
6 changed files with 42 additions and 67 deletions

19
ctl.go
View File

@ -7,7 +7,6 @@ import (
"io"
"log"
"net"
"os"
)
func startctl() {
@ -44,12 +43,10 @@ func handleClient(commandClient net.Conn) {
var responseback ResponseBack
n, err := io.ReadFull(commandClient, sizebyte)
if err != nil {
fmt.Println("Read error", err)
os.Exit(1)
log.Fatal("Read error: ", err)
}
if n != 4 {
fmt.Println("Did not read 4 bytes, failure.")
os.Exit(1)
log.Fatal("Did not read 4 bytes, failure.")
}
jsonsize := int(binary.LittleEndian.Uint32(sizebyte))
jsonbyte := make([]byte, jsonsize)
@ -95,8 +92,7 @@ func handleClient(commandClient net.Conn) {
responsebytes, err := json.Marshal(responseback)
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
log.Fatal("Error: ", err)
}
n = len(responsebytes)
@ -104,20 +100,17 @@ func handleClient(commandClient net.Conn) {
_, err = commandClient.Write(sizebyte)
if err != nil {
fmt.Println("Error on write:", err)
os.Exit(1)
log.Fatal("Error on write:", err)
}
responsebyte, err := json.Marshal(responseback)
if err != nil {
fmt.Println("Error response back: ", err)
os.Exit(1)
log.Fatal("Error response back: ", err)
}
_, err = commandClient.Write(responsebyte)
if err != nil {
fmt.Println("Error on write:", err)
os.Exit(1)
log.Fatal("Error on write:", err)
}
commandClient.Close()
}

View File

@ -32,6 +32,8 @@ func main() {
p = bluemonday.NewPolicy()
spaceReg = regexp.MustCompile(`\s+`)
// re = regexp.MustCompile("^https?://(.*)/.*$")
re = regexp.MustCompile("^https?://([^/]*)/(.*)$")
for _, endpoint := range settings.Autostart {
log.Print("Autostarting " + endpoint)

View File

@ -9,10 +9,12 @@ import (
"regexp"
"strings"
"time"
"net"
)
var p *bluemonday.Policy
var spaceReg *regexp.Regexp
var re *regexp.Regexp
func GetRunner(endpoint string) RunningInstance {
ri_mutex.Lock()
@ -101,6 +103,29 @@ func GetNodeInfo(endpoint string, o RunningInstance) RunningInstance {
return o
}
func CheckInstance(newinstance string, callerEndpoint string) {
if settings.Crawl == true && stringexists(newinstance, settings.Banned) == false {
_, err := net.LookupHost(newinstance)
if err != nil { // Bad hostname
return
}
// Skip over this if its the same as the endpoint
if newinstance == callerEndpoint {
return
}
ri_mutex.Lock()
o, exists := runninginstances[newinstance]
if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{}
runninginstances[newinstance] = m
go StartInstance(newinstance)
}
ri_mutex.Unlock()
}
}
func StartInstance(endpoint string) {
// Check if exists. If so, get the object. If not, create it
o := GetRunner(endpoint)

25
poll.go
View File

@ -1,13 +1,10 @@
package main
import (
//"crypto/sha1"
"encoding/json"
//"html"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
@ -177,8 +174,10 @@ func PollMastodonPleroma(endpoint string, o *RunningInstance) {
for _, newpost := range newposts {
go check_post(newpost.Uri)
at_sign := strings.Index(newpost.Account.Acct, "@")
newinstance := newpost.Account.Acct[at_sign+1:]
matchset := re.FindStringSubmatch(newpost.Uri)
newinstance := matchset[1]
log.Print(newinstance + " reported by " + endpoint)
// Check min_id
if newpost.Id > min_id {
@ -186,21 +185,7 @@ func PollMastodonPleroma(endpoint string, o *RunningInstance) {
}
// Only done if we are crawling
if settings.Crawl == true && stringexists(newinstance, settings.Banned) == false {
// Skip over this if its the same as the endpoint
if newinstance == endpoint {
continue
}
ri_mutex.Lock()
o, exists := runninginstances[newinstance]
if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{}
runninginstances[newinstance] = m
go StartInstance(newinstance)
}
ri_mutex.Unlock()
}
go CheckInstance(newinstance, endpoint)
}
time.Sleep(time.Second * 10)
}

View File

@ -91,20 +91,10 @@ func StreamMastodon(endpoint string, o *RunningInstance) {
go check_post(newpost.Uri)
at_sign := strings.Index(newpost.Account.Acct, "@")
newinstance := newpost.Account.Acct[at_sign+1:]
matchset := re.FindStringSubmatch(newpost.Uri)
newinstance := matchset[1]
if settings.Crawl == true && stringexists(newinstance, settings.Banned) == false {
ri_mutex.Lock()
o, exists := runninginstances[newinstance]
if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{}
runninginstances[newinstance] = m
go StartInstance(newinstance)
}
ri_mutex.Unlock()
}
go CheckInstance(newinstance, endpoint)
}
if retry == true {
time.Sleep(time.Minute * 30)
@ -112,11 +102,4 @@ func StreamMastodon(endpoint string, o *RunningInstance) {
break
}
}
// ri_mutex.Lock()
// m = runninginstances[endpoint]
// m.LastRun = time.Now().Format(time.RFC3339)
// m.Status = STREAM_ENDED
// runninginstances[endpoint] = m
// ri_mutex.Unlock()
}

15
web.go
View File

@ -125,20 +125,7 @@ func inboxHandler() http.HandlerFunc {
newinstance := createobject.ID[8 : 8+slashend]
log.Print("The at sign is: ", newinstance)
// Only done if we are crawling
if settings.Crawl == true && stringexists(newinstance, settings.Banned) == false {
// Skip over this if its the same as the endpoint
ri_mutex.Lock()
o, exists := runninginstances[newinstance]
if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{}
runninginstances[newinstance] = m
go StartInstance(newinstance)
}
ri_mutex.Unlock()
}
go CheckInstance(newinstance, "")
case "Like":
case "Announcement":