Finally finished

This commit is contained in:
Ben 2025-08-01 15:16:25 -07:00
parent 309b836931
commit 5b213eb72f
Signed by: webmaster
GPG key ID: A5FCBAF34E6E8B50
10 changed files with 112 additions and 60 deletions

View file

@ -14,16 +14,19 @@ import (
"time"
)
/* Starts the listener */
func StartWebserver(port int) error {
http.HandleFunc("/", getRoot);
return http.ListenAndServe(":"+fmt.Sprint(port), nil);
}
/* Writes data to the log */
func writeLog(method string, status int, start time.Time) {
duration := time.Now().Sub(start);
log.Default().Printf("%s %d %s", method, status, duration.String())
}
/* Handler for all requests */
func getRoot(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
@ -31,8 +34,8 @@ func getRoot(w http.ResponseWriter, r *http.Request) {
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)
w.Write([]byte("Authentication header missing\n"));
writeLog(r.Method, 400, startTime);
return;
}
bearer := authArr[0];
@ -42,7 +45,7 @@ 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)
writeLog(r.Method, 500, startTime);
return;
}
@ -50,7 +53,7 @@ func getRoot(w http.ResponseWriter, r *http.Request) {
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)
writeLog(r.Method, 403, startTime);
return;
}
@ -59,7 +62,7 @@ func getRoot(w http.ResponseWriter, r *http.Request) {
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)
writeLog(r.Method, 400, startTime);
return;
}
@ -68,7 +71,7 @@ func getRoot(w http.ResponseWriter, r *http.Request) {
if !ok {
w.WriteHeader(400);
w.Write([]byte("Body content is not of the write format"));
writeLog(r.Method, 400, startTime)
writeLog(r.Method, 400, startTime);
return;
}
@ -79,22 +82,21 @@ func getRoot(w http.ResponseWriter, r *http.Request) {
incoming := subprocess.GetIncoming(b)
w.WriteHeader(200);
w.Write([]byte(incoming));
writeLog(r.Method, 200, startTime)
return
writeLog(r.Method, 200, startTime);
return;
}
// Run request
bodyContent, err := subprocess.Request(b)
bodyContent, err := subprocess.Request(b);
if err != nil {
w.WriteHeader(500);
w.Write([]byte("Internal server error: " + err.Error() + "\n"));
return
writeLog(r.Method, 500, startTime);
return;
}
// Request returned something
w.WriteHeader(200);
w.Write([]byte(bodyContent));
// Log the request
log.Default().Print("HTTP Request: ", bearer, " " , 200, " ", string(body))
log.Default().Print("HTTP Request: ", bearer, " " , 200, " ", string(body));
}

View file

@ -1,17 +1,19 @@
# Web - Signal-CLI HTTP
This module handles the HTTP requests. The path used for requests genuinely does not matter. However, every request must have a JSON object body, and an `Authorization: bearer <token>` header. The response will also always be a JSON.
This module handles the HTTP requests. The path used for requests genuinely does not matter. However, every request must have a JSON object body, and an `Authorization: bearer <token>` header.
Possible response codes:
* 400 Bad Request: Bad JSON, missing Authorization header, etc.
* 400 Bad Request: Bad JSON, missing Authorization header, **sent a request with a defined id**, etc.
* 401 Unauthorized: bearer token not allowed for presented request.
* 500 Internal Server Error: Self explanitory.
* 501 Not Implemented: Will be returned for message receiving.
* 200 OK: Request was forwarded to JSONRPC subprocess and that process returned something, including if the "error" key is present in the returned JSON.
Any non-200 response code will come with a JSON with the following format:
There is no body content with non-200 response codes. With 200 the response is a valid JSON map or array.
```json
{"error":"Error message string"}
```
This program simply relays requests to the signal-cli program. **It will not prevent you from breaking anything, outside of not whitelisting certain requests. This program does not understand what requests mean.** Each request comes formatted as a JSON object outlined in the [JSON-RPC documentation](https://github.com/AsamK/signal-cli/blob/master/man/signal-cli-jsonrpc.5.adoc](https://github.com/AsamK/signal-cli/blob/master/man/signal-cli-jsonrpc.5.adoc).
The program will ensure that the request object is a JSON map, and that the `request` key is present. For any request type that is not `receive`, the program will generate an ID for your request (do not put on in the request, it will return an error) and return the program's response.
For `receive` it will return a JSON list of JSON maps in the incoming message cache that match to your request. There is no limit to how many messages it returns so be careful.