commit 902f6513ff23f07237f3f17ef8cade7810f0a3d3 Author: 50501AZ Webmaster Date: Sat Jul 26 22:38:03 2025 -0700 First commit diff --git a/args/args.go b/args/args.go new file mode 100644 index 0000000..09806dd --- /dev/null +++ b/args/args.go @@ -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; +} \ No newline at end of file diff --git a/args/readme.md b/args/readme.md new file mode 100644 index 0000000..f312cf6 --- /dev/null +++ b/args/readme.md @@ -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") +``` \ No newline at end of file diff --git a/conf/conf.go b/conf/conf.go new file mode 100644 index 0000000..730c801 --- /dev/null +++ b/conf/conf.go @@ -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;} \ No newline at end of file diff --git a/conf/readme.md b/conf/readme.md new file mode 100644 index 0000000..59e9e52 --- /dev/null +++ b/conf/readme.md @@ -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. \ No newline at end of file diff --git a/config-sample.txt b/config-sample.txt new file mode 100644 index 0000000..589afac --- /dev/null +++ b/config-sample.txt @@ -0,0 +1,3 @@ +WGV99fSwgKhdQSa89HQIGxas /+16028675309/room/roomID/* +WGV99fSwgKhdQSa89HQIGxas /+16028675309/direct/username.69/send +ZQR3T6lqsvnXcgcWhpPOWWdv +16028675309/direct/username.69/send/ \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5ab0d05 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module signal-cli-http + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..3853754 --- /dev/null +++ b/main.go @@ -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()) +} \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..ace0a89 --- /dev/null +++ b/readme.md @@ -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. \ No newline at end of file