Rearranging files

Down the line I need to move headers.go and uniquefifo.go into a module
This commit is contained in:
farhan 2021-02-10 23:58:20 +00:00
parent 126aac8c81
commit 778150f83c
21 changed files with 317 additions and 16 deletions

4
.gitignore vendored
View File

@ -1,5 +1,5 @@
fedilogue
fedictl
fedictl/fedictl
fedilogue/fedilogue
config.jsonc
keys/*
clients/*

View File

@ -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

133
fedilogue/fedictl.go Normal file
View File

@ -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)
}
}
}

100
fedilogue/headers.go Normal file
View File

@ -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"`
}

82
fedilogue/uniquefifo.go Normal file
View File

@ -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)
}
*/