First commit

This commit is contained in:
Ben 2025-07-26 22:38:03 -07:00
commit 902f6513ff
Signed by: webmaster
GPG key ID: A5FCBAF34E6E8B50
8 changed files with 130 additions and 0 deletions

24
args/args.go Normal file
View file

@ -0,0 +1,24 @@
package args
/* This file manages declaration of, parsing of, and returning copies of
command line arguments. */
import "flag"
/* Method to trigger flag parsing. Can be called multiple times safely. */
func Parse() {
if (flagsParsed) {return}
flag.Parse();
flagsParsed = true;
}
/* module-specific variable to avoid re-parsing flags */
var flagsParsed bool = false;
/* what JSON file to read config values from */
var confLocation = flag.String("conf", "./config.txt", "Config file to read from")
/* Returns nil if flags are not parsed yet
@return set boolean will be true if argument is not nil */
func GetConfLocation() (location string, set bool) {
if confLocation == nil {return "", false}
return *confLocation, true;
}

8
args/readme.md Normal file
View file

@ -0,0 +1,8 @@
# Args - Signal-CLI HTTP
This module handles command line arguments. A list follows. You can also pass through -h for this list.
```
-conf string
Config file to read from (default "./config.txt")
```

37
conf/conf.go Normal file
View file

@ -0,0 +1,37 @@
package conf
import (
"bufio"
"errors"
"os"
"strings"
)
/* Object to handle what is in a JSON config */
type Config struct {
configData map[string][]string;
}
func NewConfig(filePath string) (newConfig *Config, err error) {
// Open file
file, err := os.Open(filePath)
if err != nil {return}
defer file.Close()
// Create configuration
newConfigData := make(map[string][]string);
// Read lines into newConfigData
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, " ", 2);
if len(parts) != 2 {err = errors.New("Bad config file!"); return;}
newConfigData[parts[0]] = append(newConfigData[parts[0]], parts[1]);
}
// Create Config object and copy a reference to newConfigData into it
return &Config{configData: newConfigData}, nil;
}
func (config Config) GetConfigData() map[string][]string {return config.configData;}

22
conf/readme.md Normal file
View file

@ -0,0 +1,22 @@
# Conf - Signal-CLI HTTP
This module handles reading and parsing the config file, and acting as a verifier for the `Authorization` header on the HTTP requests.
The config file is made up of multiple lines. The first token in each line is the `Authorization` bearer token. This cannot have spaces but can be any string. Choose wisely. The remainder of the line contains a path that the `Authorization` header is checked against. It does not matter if you include a leading or trailing slash.
Here's a sample config:
```
WGV99fSwgKhdQSa89HQIGxas /+16028675309/room/roomID/*
WGV99fSwgKhdQSa89HQIGxas /+16028675309/direct/username.69/send
ZQR3T6lqsvnXcgcWhpPOWWdv +16028675309/direct/username.69/send/
```
The config file is a **whitelist** for each bearer token to access a specific endpoint (or set of endpoints). The endpoints for this program are granular enough to only allow one action for each endpoint, so this level of whitelisting should™ be okay.
There is a regex-like behavior to these paths using the `*` and `?` characters. For the regex-like behavior to be triggered these characters must be by themselves per path segment (no other characters not separated by a `/` or a start or end of string).
The `*` character matches to any number of path segments. The `?` character matches to only one segment. Here's some examples:
* `HZJWwB0TAjz6pjAHosII5ofR /+16028675309/*` will allow the bearer token to access any endpoint with the phone number `+16028675309`
* `HZJWwB0TAjz6pjAHosII5ofR /+16028675309/direct/?/send` will allow the bearer token to send a direct message to anyone on that phone number.

3
config-sample.txt Normal file
View file

@ -0,0 +1,3 @@
WGV99fSwgKhdQSa89HQIGxas /+16028675309/room/roomID/*
WGV99fSwgKhdQSa89HQIGxas /+16028675309/direct/username.69/send
ZQR3T6lqsvnXcgcWhpPOWWdv +16028675309/direct/username.69/send/

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module signal-cli-http
go 1.19

23
main.go Normal file
View file

@ -0,0 +1,23 @@
package main
/* This is the main function of this program. It handles setting everything up */
import (
"signal-cli-http/args"
"signal-cli-http/conf"
"fmt"
"log"
)
func main() {
// Read arguments
args.Parse();
configLocation, confLocationSet := args.GetConfLocation();
if !confLocationSet {log.Default().Print("No config value!"); return;}
log.Default().Print("Reading config value from ", configLocation);
config, err := conf.NewConfig(configLocation);
if err != nil {log.Default().Print("Error reading config: ", err); return;}
fmt.Println(config.GetConfigData())
}

10
readme.md Normal file
View file

@ -0,0 +1,10 @@
# Signal-CLI HTTP
**Very** early in development.
Very simple HTTP frontend to [signal-cli](https://github.com/AsamK/signal-cli).
Please also read the following README files for the individual modules:
* [args](args/readme.md) - handles command line arguments.
* [conf](conf/readme.md) - handles the config file.