diff --git a/fedictl/fedictl.go b/fedictl/fedictl.go index 7764df7..149db6b 100644 --- a/fedictl/fedictl.go +++ b/fedictl/fedictl.go @@ -1,6 +1,7 @@ package main import ( + "gitlab.com/khanzf/fedilogue/shared" "encoding/binary" "encoding/json" "flag" @@ -21,8 +22,8 @@ func main() { /* Condition verification */ totalflags := 0 - var commandMap CommandMap - var responseback ResponseBack + var commandMap shared.CommandMap + var responseback shared.ResponseBack if *shutdownPtr { totalflags++ diff --git a/fedictl/go.mod b/fedictl/go.mod new file mode 100644 index 0000000..2b15be1 --- /dev/null +++ b/fedictl/go.mod @@ -0,0 +1,5 @@ +module gitlab.com/khanzf/fedilogue/fedictl + +go 1.16 + +require gitlab.com/khanzf/fedilogue/shared v0.0.0-20210929025148-677dab517028 diff --git a/fedictl/headers.go b/fedictl/headers.go deleted file mode 100644 index 01a1710..0000000 --- a/fedictl/headers.go +++ /dev/null @@ -1,100 +0,0 @@ -package main - -import ( - "net/http" -) - -const ( - NEW_INSTANCE = 0 - RUNNING = 200 - UNAUTHORIZED = 401 - FORBIDDEN = 403 - NOT_FOUND = 404 - UNPROCESSABLE_ENTITY = 422 - TOOMANYREQUESTS = 429 - INTERNAL_ERROR = 500 - CLIENT_ISSUE = 600 - ONION_PROTOCOL = 601 - BAD_RESPONSE = 602 - BAD_NODEINFO = 604 - UNSUPPORTED_INSTANCE = 605 - STREAM_ENDED = 606 - KEEPALIVE = 607 -) - -type ObjectType struct { - Id string `json:"id"` -} - -// Parsing Unmarshal JSON type -type ReportActivity struct { - - // Retrieved values - Id string `json:"id"` - Uri string `json:"uri"` - Account AccountType - Content string `json:"content"` - Created_at string `json:"created_at"` - - // Derived values - normalized string -} - -type AccountType struct { - Acct string `json:"acct"` - Avatar string `json:"avatar"` - Bot bool `json:"bot"` - Created_at string `json:"created_at"` - Display_name string `json:"display_name"` - Url string `json:"url"` -} - -// Instance's new min_id value -type RunningInstance struct { - Software string `json:"software"` - Version string `json:"version"` - Status int `json:"status"` - LastRun string `json:"lastrun"` - CaptureType string `json:"capturetype"` - client http.Client - client_id string - client_secret string - recentactivities *UniqueFifo - recentactors *UniqueFifo -} - -type NodeInfoSoftware struct { - Name string `json:"name"` - Version string `json:"version"` -} - -type NodeInfo struct { - Software NodeInfoSoftware `json:"software"` -} - -type CommandMap struct { - Type string `json:"Type"` - Endpoint string `json:"Endpoint"` -} - -type ResponseBack struct { - Type string `json:"Type"` - Message string `json:"Message"` - 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"` -} diff --git a/fedictl/uniquefifo.go b/fedictl/uniquefifo.go deleted file mode 100644 index 15fc3bc..0000000 --- a/fedictl/uniquefifo.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "sync" -) - -type UniqueFifo struct { - slice []string - mu sync.Mutex - size int -} - - -func newUniqueFifo(size int) *UniqueFifo { - q := UniqueFifo{} - q.slice = make([]string, 0) - q.size = size - return &q -} - -func (q *UniqueFifo) Add(v string) bool { - ret := false - if len(q.slice) == 0 { - q.slice = append(q.slice, v) -//logDebug.Print("Condition 1 for ", v) - ret = false - } else { - i := q.Contains(v) - if i != -1 { - q.Remove(i) -//logDebug.Print("Condition 2 for ", v) - ret = true - } else { -//logDebug.Print("Condition 3 for ", v) - ret = false - } - q.slice = append(q.slice, "") - copy(q.slice[1:], q.slice) - q.slice[0] = v - if len(q.slice) <= q.size { - q.slice = q.slice[:len(q.slice)] - } else { - q.slice = q.slice[:q.size] - } - } - - return ret -} - -func (q *UniqueFifo) Remove(r int) { - f := q.slice[:r] - e := q.slice[r+1:] - q.slice = f - q.slice = append(q.slice, e...) -} - - -func (q *UniqueFifo) Contains(v string) int { - for i, val := range q.slice { - if val == v { - return i - } - } - return -1 -} - -/* -func main() { - u := newUniqueFifo(3) - u.Add("First") - u.Add("First") - fmt.Println(u) - u.Add("Second") - fmt.Println(u) - u.Add("First") - fmt.Println(u) - u.Add("Third") - fmt.Println(u) - u.Add("Fourth") - fmt.Println(u) -} -*/ diff --git a/fedilogue/entrypoint.sh b/fedilogue/entrypoint.sh deleted file mode 100755 index 8291757..0000000 --- a/fedilogue/entrypoint.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh - -sleep 15 - -route add default gw vpn -/usr/bin/psql -h db fedilogue fedilogue < tables.sql -cat tables.sql -echo /usr/bin/psql -h db fedilogue fedilogue -./fedilogue diff --git a/fedilogue/go.mod b/fedilogue/go.mod index cdd72e9..1fb2843 100644 --- a/fedilogue/go.mod +++ b/fedilogue/go.mod @@ -1,9 +1,10 @@ module gitlab.com/khanzf/fedilogue/fedilogue -go 1.15 +go 1.16 require ( github.com/jackc/pgx/v4 v4.10.1 github.com/microcosm-cc/bluemonday v1.0.4 + gitlab.com/khanzf/fedilogue/shared v0.0.0-20210929025148-677dab517028 muzzammil.xyz/jsonc v0.0.0-20201229145248-615b0916ca38 ) diff --git a/fedilogue/headers.go b/fedilogue/headers.go index 01a1710..1edd030 100644 --- a/fedilogue/headers.go +++ b/fedilogue/headers.go @@ -1,6 +1,7 @@ package main import ( + "gitlab.com/khanzf/fedilogue/shared" "net/http" ) @@ -59,8 +60,8 @@ type RunningInstance struct { client http.Client client_id string client_secret string - recentactivities *UniqueFifo - recentactors *UniqueFifo + recentactivities *shared.UniqueFifo + recentactors *shared.UniqueFifo } type NodeInfoSoftware struct { diff --git a/fedilogue/instance.go b/fedilogue/instance.go index b279d49..fe32fea 100644 --- a/fedilogue/instance.go +++ b/fedilogue/instance.go @@ -1,6 +1,7 @@ package main import ( + "gitlab.com/khanzf/fedilogue/shared" "encoding/json" "io/ioutil" "net/http" @@ -63,8 +64,8 @@ func GetRunner(endpoint string) (RunningInstance, bool) { o = RunningInstance{} o.client = BuildClient(endpoint) o.Status = KEEPALIVE - o.recentactivities = newUniqueFifo(10) - o.recentactors = newUniqueFifo(10) + o.recentactivities = shared.NewUniqueFifo(10) + o.recentactors = shared.NewUniqueFifo(10) runninginstances[endpoint] = o } ri_mutex.Unlock() @@ -175,8 +176,8 @@ func CheckInstance(newinstance string, callerEndpoint string) { if exists == false || o.Status == KEEPALIVE { m := RunningInstance{} m.client = BuildClient(newinstance) - m.recentactivities = newUniqueFifo(10) - m.recentactors = newUniqueFifo(10) + m.recentactivities = shared.NewUniqueFifo(10) + m.recentactors = shared.NewUniqueFifo(10) runninginstances[newinstance] = m go StartInstance(newinstance) } diff --git a/fedilogue/retrieve.go b/fedilogue/retrieve.go index 260a5c3..baa52c7 100644 --- a/fedilogue/retrieve.go +++ b/fedilogue/retrieve.go @@ -91,14 +91,14 @@ func check_activity(uri string) { o, _ := GetRunner(activityjson.instance) // Check if there were any recent requests on this - o.recentactivities.mu.Lock() + o.recentactivities.Mu.Lock() if o.recentactivities.Add(uri) == true { - o.recentactivities.mu.Unlock() + o.recentactivities.Mu.Unlock() //return activityjson, errors.New("Recently requested within local cache") return } - o.recentactivities.mu.Unlock() + o.recentactivities.Mu.Unlock() var jsondocument string // jsonmap := make(map[string]interface{}) @@ -190,13 +190,13 @@ func check_actor(uri string) { // Check if there were any recent requests on this o, _ := GetRunner(actorjson.instance) - o.recentactors.mu.Lock() + o.recentactors.Mu.Lock() if o.recentactors.Add(uri) == true { - o.recentactors.mu.Unlock() + o.recentactors.Mu.Unlock() return // return actorjson, errors.New("Recently requested actor within local cache") } - o.recentactors.mu.Unlock() + o.recentactors.Mu.Unlock() // jsonmap := make(map[string]interface{}) selectRet := pool.QueryRow(context.Background(), "SELECT FROM actors WHERE document->>'id' = $1", uri) diff --git a/fedilogue/uniquefifo.go b/fedilogue/uniquefifo.go deleted file mode 100644 index 15fc3bc..0000000 --- a/fedilogue/uniquefifo.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "sync" -) - -type UniqueFifo struct { - slice []string - mu sync.Mutex - size int -} - - -func newUniqueFifo(size int) *UniqueFifo { - q := UniqueFifo{} - q.slice = make([]string, 0) - q.size = size - return &q -} - -func (q *UniqueFifo) Add(v string) bool { - ret := false - if len(q.slice) == 0 { - q.slice = append(q.slice, v) -//logDebug.Print("Condition 1 for ", v) - ret = false - } else { - i := q.Contains(v) - if i != -1 { - q.Remove(i) -//logDebug.Print("Condition 2 for ", v) - ret = true - } else { -//logDebug.Print("Condition 3 for ", v) - ret = false - } - q.slice = append(q.slice, "") - copy(q.slice[1:], q.slice) - q.slice[0] = v - if len(q.slice) <= q.size { - q.slice = q.slice[:len(q.slice)] - } else { - q.slice = q.slice[:q.size] - } - } - - return ret -} - -func (q *UniqueFifo) Remove(r int) { - f := q.slice[:r] - e := q.slice[r+1:] - q.slice = f - q.slice = append(q.slice, e...) -} - - -func (q *UniqueFifo) Contains(v string) int { - for i, val := range q.slice { - if val == v { - return i - } - } - return -1 -} - -/* -func main() { - u := newUniqueFifo(3) - u.Add("First") - u.Add("First") - fmt.Println(u) - u.Add("Second") - fmt.Println(u) - u.Add("First") - fmt.Println(u) - u.Add("Third") - fmt.Println(u) - u.Add("Fourth") - fmt.Println(u) -} -*/