From 778150f83c42ac1167a202b9843d877b90582940 Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Wed, 10 Feb 2021 23:58:20 +0000 Subject: [PATCH] Rearranging files Down the line I need to move headers.go and uniquefifo.go into a module --- .gitignore | 4 +- Makefile | 14 -- fedictl.go => fedictl/fedictl.go | 0 headers.go => fedictl/headers.go | 0 uniquefifo.go => fedictl/uniquefifo.go | 0 config.go => fedilogue/config.go | 0 .../config.jsonc.sample | 0 ctl.go => fedilogue/ctl.go | 0 db.go => fedilogue/db.go | 0 fedilogue/fedictl.go | 133 ++++++++++++++++++ fedilogue.go => fedilogue/fedilogue.go | 0 fedilogue/headers.go | 100 +++++++++++++ instance.go => fedilogue/instance.go | 0 log.go => fedilogue/log.go | 0 oauth.go => fedilogue/oauth.go | 0 poll.go => fedilogue/poll.go | 0 retrieve.go => fedilogue/retrieve.go | 0 stream.go => fedilogue/stream.go | 0 tables.sql => fedilogue/tables.sql | 0 fedilogue/uniquefifo.go | 82 +++++++++++ web.go => fedilogue/web.go | 0 21 files changed, 317 insertions(+), 16 deletions(-) delete mode 100644 Makefile rename fedictl.go => fedictl/fedictl.go (100%) rename headers.go => fedictl/headers.go (100%) rename uniquefifo.go => fedictl/uniquefifo.go (100%) rename config.go => fedilogue/config.go (100%) rename config.jsonc.sample => fedilogue/config.jsonc.sample (100%) rename ctl.go => fedilogue/ctl.go (100%) rename db.go => fedilogue/db.go (100%) create mode 100644 fedilogue/fedictl.go rename fedilogue.go => fedilogue/fedilogue.go (100%) create mode 100644 fedilogue/headers.go rename instance.go => fedilogue/instance.go (100%) rename log.go => fedilogue/log.go (100%) rename oauth.go => fedilogue/oauth.go (100%) rename poll.go => fedilogue/poll.go (100%) rename retrieve.go => fedilogue/retrieve.go (100%) rename stream.go => fedilogue/stream.go (100%) rename tables.sql => fedilogue/tables.sql (100%) create mode 100644 fedilogue/uniquefifo.go rename web.go => fedilogue/web.go (100%) diff --git a/.gitignore b/.gitignore index 4253606..21375e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -fedilogue -fedictl +fedictl/fedictl +fedilogue/fedilogue config.jsonc keys/* clients/* diff --git a/Makefile b/Makefile deleted file mode 100644 index e7fcf9e..0000000 --- a/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -FEDILOGUE_GOFILES = fedilogue.go ctl.go headers.go instance.go poll.go stream.go web.go db.go config.go oauth.go retrieve.go log.go uniquefifo.go -FEDICTL_GOFILES = fedictl.go headers.go log.go uniquefifo.go - -build: - go build -o fedilogue $(FEDILOGUE_GOFILES) - go build -o fedictl $(FEDICTL_GOFILES) -run: - go run $(FEDILOGUE_GOFILES) -acctkey: - mkdir -p keys - openssl genrsa -out keys/acctprivate.pem 1024 - openssl rsa -in keys/acctprivate.pem -pubout -out keys/acctpublic.pem -clean: - rm -f fedilogue fedictl diff --git a/fedictl.go b/fedictl/fedictl.go similarity index 100% rename from fedictl.go rename to fedictl/fedictl.go diff --git a/headers.go b/fedictl/headers.go similarity index 100% rename from headers.go rename to fedictl/headers.go diff --git a/uniquefifo.go b/fedictl/uniquefifo.go similarity index 100% rename from uniquefifo.go rename to fedictl/uniquefifo.go diff --git a/config.go b/fedilogue/config.go similarity index 100% rename from config.go rename to fedilogue/config.go diff --git a/config.jsonc.sample b/fedilogue/config.jsonc.sample similarity index 100% rename from config.jsonc.sample rename to fedilogue/config.jsonc.sample diff --git a/ctl.go b/fedilogue/ctl.go similarity index 100% rename from ctl.go rename to fedilogue/ctl.go diff --git a/db.go b/fedilogue/db.go similarity index 100% rename from db.go rename to fedilogue/db.go diff --git a/fedilogue/fedictl.go b/fedilogue/fedictl.go new file mode 100644 index 0000000..7764df7 --- /dev/null +++ b/fedilogue/fedictl.go @@ -0,0 +1,133 @@ +package main + +import ( + "encoding/binary" + "encoding/json" + "flag" + "fmt" + "io" + "net" + "os" +) + +func main() { + + shutdownPtr := flag.Bool("shutdown", false, "Shutdown server") + suspendPtr := flag.String("suspend", "", "Instance to Suspend") + resumePtr := flag.String("resume", "", "Instance to Resume") + addPtr := flag.String("add", "", "Instance to add") + statusPtr := flag.Bool("status", false, "Check status") + flag.Parse() + + /* Condition verification */ + totalflags := 0 + var commandMap CommandMap + var responseback ResponseBack + + if *shutdownPtr { + totalflags++ + commandMap.Type = "shutdown" + } + if *statusPtr { + totalflags++ + commandMap.Type = "status" + } + if *addPtr != "" { + totalflags++ + commandMap.Type = "add" + commandMap.Endpoint = *addPtr + } + if *suspendPtr != "" { + totalflags++ + commandMap.Type = "suspend" + } + if *resumePtr != "" { + totalflags++ + commandMap.Type = "resume" + } + if totalflags > 1 { + fmt.Println("Incompatible arguments, exiting.") + os.Exit(1) + } else if totalflags == 0 { + fmt.Println("No options specified, exiting.") + os.Exit(1) + } + + commandByte, err := json.Marshal(commandMap) + if err != nil { + fmt.Println(err) + return + } + c, err := net.Dial("tcp", "127.0.0.1:5555") + if err != nil { + fmt.Println(err) + return + } + sizebytes := make([]byte, 4) + b := len(commandByte) + + // Send the request + binary.LittleEndian.PutUint32(sizebytes, uint32(b)) + _, err = c.Write(sizebytes) + if err != nil { + fmt.Println(err) + return + } + _, err = c.Write(commandByte) + if err != nil { + fmt.Println(err) + return + } + + // Read the response + n, err := io.ReadFull(c, sizebytes) + if err != nil || n != 4 { + fmt.Println("err", err, n) + } + jsonsize := int(binary.LittleEndian.Uint32(sizebytes)) + responsebytes := make([]byte, jsonsize) + _, err = io.ReadFull(c, responsebytes) + if err != nil { + fmt.Println("Read Error", err) + } + err = json.Unmarshal(responsebytes, &responseback) + if err != nil { + fmt.Println("Unmarshal error", err) + } + + switch commandMap.Type { + case "add": + fmt.Println(responseback.Message) + case "status": + fmt.Println("Status\t\tLastRun\t\t\tCaptureType\tEndpoint") + for endpoint, runninginstance := range responseback.RunningInstances { + if runninginstance.Status == 0 { + fmt.Fprintf(os.Stdout, "New\t") + fmt.Fprintf(os.Stdout, "\tNever\t\t\t") + } else if runninginstance.Status == 200 { + fmt.Fprintf(os.Stdout, "Running\t") + fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun) + } else if runninginstance.Status == 429 { + fmt.Fprintf(os.Stdout, "TooManyRequests\t") + fmt.Fprintf(os.Stdout, "%s\t", runninginstance.LastRun) + } else if runninginstance.Status == 600 { + fmt.Fprintf(os.Stdout, "ClientIssue") + fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun) + } else if runninginstance.Status == 602 { + fmt.Fprintf(os.Stdout, "BadInstance") + fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun) + } else if runninginstance.Status == 605 { + fmt.Fprintf(os.Stdout, "UnsupportedNode") + fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun) + } else { + fmt.Fprintf(os.Stdout, "%d\t", runninginstance.Status) + fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.LastRun) + } + if runninginstance.LastRun == "Queued" { + fmt.Fprintf(os.Stdout, "\t\t") + } + fmt.Fprintf(os.Stdout, "\t%s\t", runninginstance.CaptureType) + fmt.Fprintf(os.Stdout, "%s\n", endpoint) + } + } +} diff --git a/fedilogue.go b/fedilogue/fedilogue.go similarity index 100% rename from fedilogue.go rename to fedilogue/fedilogue.go diff --git a/fedilogue/headers.go b/fedilogue/headers.go new file mode 100644 index 0000000..01a1710 --- /dev/null +++ b/fedilogue/headers.go @@ -0,0 +1,100 @@ +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/instance.go b/fedilogue/instance.go similarity index 100% rename from instance.go rename to fedilogue/instance.go diff --git a/log.go b/fedilogue/log.go similarity index 100% rename from log.go rename to fedilogue/log.go diff --git a/oauth.go b/fedilogue/oauth.go similarity index 100% rename from oauth.go rename to fedilogue/oauth.go diff --git a/poll.go b/fedilogue/poll.go similarity index 100% rename from poll.go rename to fedilogue/poll.go diff --git a/retrieve.go b/fedilogue/retrieve.go similarity index 100% rename from retrieve.go rename to fedilogue/retrieve.go diff --git a/stream.go b/fedilogue/stream.go similarity index 100% rename from stream.go rename to fedilogue/stream.go diff --git a/tables.sql b/fedilogue/tables.sql similarity index 100% rename from tables.sql rename to fedilogue/tables.sql diff --git a/fedilogue/uniquefifo.go b/fedilogue/uniquefifo.go new file mode 100644 index 0000000..15fc3bc --- /dev/null +++ b/fedilogue/uniquefifo.go @@ -0,0 +1,82 @@ +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/web.go b/fedilogue/web.go similarity index 100% rename from web.go rename to fedilogue/web.go