2020-11-10 21:53:46 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-01-11 00:44:01 -05:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-01-01 02:26:39 +00:00
|
|
|
|
|
|
|
"gitlab.com/khanzf/fedilogue/shared"
|
2020-11-10 21:53:46 -05:00
|
|
|
)
|
|
|
|
|
2022-01-11 00:44:01 -05:00
|
|
|
func cmdFollow(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
2020-11-24 20:36:47 -05:00
|
|
|
if err != nil {
|
2022-01-11 00:44:01 -05:00
|
|
|
fmt.Println(err)
|
|
|
|
return
|
2020-11-24 20:36:47 -05:00
|
|
|
}
|
2022-01-11 00:44:01 -05:00
|
|
|
defer r.Body.Close()
|
2020-11-24 20:36:47 -05:00
|
|
|
|
2022-01-11 00:44:01 -05:00
|
|
|
var commandmap shared.CommandMap
|
|
|
|
err = json.Unmarshal(body, &commandmap)
|
|
|
|
if err != nil {
|
|
|
|
logWarn("Unable to unmarshal here, exiting...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go followInbox(commandmap.Endpoint)
|
2020-11-24 20:36:47 -05:00
|
|
|
|
2022-01-11 00:44:01 -05:00
|
|
|
fmt.Fprintf(w, "{}")
|
|
|
|
}
|
2020-11-24 20:36:47 -05:00
|
|
|
|
2022-01-11 00:44:01 -05:00
|
|
|
func startctl() {
|
|
|
|
http.HandleFunc("/follow", cmdFollow)
|
|
|
|
log.Print("Starting HTTP inbox on port 127.0.0.1:5555")
|
|
|
|
log.Fatal(http.ListenAndServe("127.0.0.1:5555", nil))
|
2020-11-24 20:36:47 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:44:01 -05:00
|
|
|
/*
|
2020-12-22 20:36:37 +00:00
|
|
|
func handleClient(commandClient net.Conn) {
|
2021-01-30 14:30:49 +00:00
|
|
|
defer commandClient.Close()
|
2020-11-10 21:53:46 -05:00
|
|
|
sizebyte := make([]byte, 4)
|
2022-01-01 02:26:39 +00:00
|
|
|
var commandmap shared.CommandMap
|
|
|
|
var responseback shared.ResponseBack
|
2020-11-10 21:53:46 -05:00
|
|
|
n, err := io.ReadFull(commandClient, sizebyte)
|
2020-11-13 20:13:13 -05:00
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Read error: ", err)
|
|
|
|
return
|
2020-11-13 20:13:13 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
if n != 4 {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Did not read 4 bytes, failure.")
|
|
|
|
return
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
jsonsize := int(binary.LittleEndian.Uint32(sizebyte))
|
|
|
|
jsonbyte := make([]byte, jsonsize)
|
|
|
|
n, err = io.ReadFull(commandClient, jsonbyte)
|
2020-11-13 20:13:13 -05:00
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Unable to unmarshal")
|
2020-11-13 20:13:13 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
if n != jsonsize {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Failed to read json size of ", n)
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(jsonbyte, &commandmap)
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Unable to unmarshal")
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch commandmap.Type {
|
|
|
|
case "status":
|
|
|
|
responseback.Message = "Ok"
|
|
|
|
case "add":
|
2021-02-13 02:45:24 +00:00
|
|
|
logConn("Manually added instance: " + commandmap.Endpoint)
|
2021-08-07 19:45:47 +00:00
|
|
|
//ri_mutex.Lock()
|
|
|
|
_, exists := GetRunner(commandmap.Endpoint)
|
2020-11-17 18:28:59 -05:00
|
|
|
if exists == true {
|
2021-02-13 02:45:24 +00:00
|
|
|
logConn("Already exists: " + commandmap.Endpoint)
|
2020-11-18 19:38:08 -05:00
|
|
|
responseback.Message = "Exists: " + commandmap.Endpoint
|
2020-11-17 18:28:59 -05:00
|
|
|
} else {
|
2021-02-01 02:05:56 +00:00
|
|
|
responseback.Message = "Adding: " + commandmap.Endpoint
|
2021-08-07 19:45:47 +00:00
|
|
|
ri_mutex.Lock()
|
2020-12-22 20:36:37 +00:00
|
|
|
go StartInstance(commandmap.Endpoint)
|
2021-08-07 19:45:47 +00:00
|
|
|
ri_mutex.Unlock()
|
2020-11-17 18:28:59 -05:00
|
|
|
}
|
2021-08-07 19:45:47 +00:00
|
|
|
//ri_mutex.Unlock()
|
2022-01-09 21:16:34 -05:00
|
|
|
case "follow":
|
|
|
|
logInfo("Follow handling...")
|
|
|
|
responseback.Message = "Trying to follow " + commandmap.Endpoint
|
|
|
|
go followInbox(commandmap.Endpoint)
|
2020-11-10 21:53:46 -05:00
|
|
|
case "suspend":
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Suspend")
|
2020-11-10 21:53:46 -05:00
|
|
|
case "resume":
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Resume")
|
2020-11-10 21:53:46 -05:00
|
|
|
default:
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Something else")
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
responseback.Type = "status"
|
2020-11-17 18:28:59 -05:00
|
|
|
responseback.RunningInstances = runninginstances
|
2020-11-10 21:53:46 -05:00
|
|
|
|
2021-08-07 19:45:47 +00:00
|
|
|
ri_mutex.Lock()
|
2020-11-10 21:53:46 -05:00
|
|
|
responsebytes, err := json.Marshal(responseback)
|
2021-08-07 19:45:47 +00:00
|
|
|
ri_mutex.Unlock()
|
2020-11-10 21:53:46 -05:00
|
|
|
if err != nil {
|
2021-02-11 21:05:01 +00:00
|
|
|
logFatal.Fatal(err)
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
n = len(responsebytes)
|
|
|
|
binary.LittleEndian.PutUint32(sizebyte, uint32(n))
|
|
|
|
|
2020-11-13 20:13:13 -05:00
|
|
|
_, err = commandClient.Write(sizebyte)
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Error on write:", err)
|
2020-11-13 20:13:13 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
|
2021-08-07 19:45:47 +00:00
|
|
|
ri_mutex.Lock()
|
2020-11-10 21:53:46 -05:00
|
|
|
responsebyte, err := json.Marshal(responseback)
|
2021-08-07 19:45:47 +00:00
|
|
|
ri_mutex.Unlock()
|
2020-11-10 21:53:46 -05:00
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Error response back: ", err)
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
|
|
|
|
2020-11-13 20:13:13 -05:00
|
|
|
_, err = commandClient.Write(responsebyte)
|
|
|
|
if err != nil {
|
2021-01-14 19:51:42 +00:00
|
|
|
logFatal.Fatal("Error on write:", err)
|
2020-11-13 20:13:13 -05:00
|
|
|
}
|
2020-11-10 21:53:46 -05:00
|
|
|
}
|
2022-01-11 00:44:01 -05:00
|
|
|
*/
|