Incoming should be working, but I haven't tested it too much.

This commit is contained in:
Ben 2025-08-01 00:44:37 -07:00
parent d302f39719
commit 309b836931
Signed by: webmaster
GPG key ID: A5FCBAF34E6E8B50
8 changed files with 259 additions and 98 deletions

View file

@ -3,13 +3,15 @@ package web
/* This file handles listening to HTTP requests */
import (
"encoding/json"
"signal-cli-http/auth"
"signal-cli-http/subprocess"
"fmt"
"io"
"log"
"net/http"
"time"
)
func StartWebserver(port int) error {
@ -17,12 +19,20 @@ func StartWebserver(port int) error {
return http.ListenAndServe(":"+fmt.Sprint(port), nil);
}
func writeLog(method string, status int, start time.Time) {
duration := time.Now().Sub(start);
log.Default().Printf("%s %d %s", method, status, duration.String())
}
func getRoot(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
// Check that Authentication header exists
authArr, ok := r.Header["Authentication"]
if (!ok) || (len(authArr) == 0) {
w.WriteHeader(400);
w.Write([]byte("Authentication header missing\n"))
writeLog(r.Method, 400, startTime)
return;
}
bearer := authArr[0];
@ -32,28 +42,47 @@ func getRoot(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(500);
w.Write([]byte("Error reading body\n"));
writeLog(r.Method, 500, startTime)
return;
}
// Check Authentication header
if !auth.Authenticate(bearer, body) {
w.WriteHeader(403);
w.Write([]byte("Bearer key not whitelisted for this request type\n"));
writeLog(r.Method, 403, startTime)
return;
}
// Attempt to unmarshal JSON
bodyUnmarshaled := auth.UnmarshalJSON(body);
if bodyUnmarshaled == nil {
var bodyUnmarshaled any;
if err := json.Unmarshal(body, &bodyUnmarshaled); err != nil {
w.WriteHeader(400);
w.Write([]byte("Body content is not a valid JSON"));
writeLog(r.Method, 400, startTime)
return;
}
// Type assertion
b, ok := bodyUnmarshaled.(map[string]any)
b, ok := bodyUnmarshaled.(map[string]any);
if !ok {
w.WriteHeader(400);
w.Write([]byte("Body content is not of the write format"));
writeLog(r.Method, 400, startTime)
return;
}
// Handle incoming
method, ok := b["method"];
if method == "receive" {
incoming := subprocess.GetIncoming(b)
w.WriteHeader(200);
w.Write([]byte(incoming));
writeLog(r.Method, 200, startTime)
return
}
// Run request
bodyContent, err := subprocess.Request(b)
if err != nil {
@ -62,6 +91,7 @@ func getRoot(w http.ResponseWriter, r *http.Request) {
return
}
// Request returned something
w.WriteHeader(200);
w.Write([]byte(bodyContent));