Regex implementation (yet to be implemented)
This commit is contained in:
parent
902f6513ff
commit
9a7834c330
5 changed files with 72 additions and 3 deletions
|
@ -16,8 +16,7 @@ var flagsParsed bool = false;
|
||||||
|
|
||||||
/* what JSON file to read config values from */
|
/* what JSON file to read config values from */
|
||||||
var confLocation = flag.String("conf", "./config.txt", "Config file to read 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 */
|
||||||
@return set boolean will be true if argument is not nil */
|
|
||||||
func GetConfLocation() (location string, set bool) {
|
func GetConfLocation() (location string, set bool) {
|
||||||
if confLocation == nil {return "", false}
|
if confLocation == nil {return "", false}
|
||||||
return *confLocation, true;
|
return *confLocation, true;
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package conf
|
package conf
|
||||||
|
|
||||||
|
/* This file contains the Config object and its methods, which handle reading
|
||||||
|
from a config file and matching requests to the whitelist. */
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -7,11 +10,14 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// import "fmt"
|
||||||
|
|
||||||
/* Object to handle what is in a JSON config */
|
/* Object to handle what is in a JSON config */
|
||||||
type Config struct {
|
type Config struct {
|
||||||
configData map[string][]string;
|
configData map[string][]string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Opens and reads a file at the path */
|
||||||
func NewConfig(filePath string) (newConfig *Config, err error) {
|
func NewConfig(filePath string) (newConfig *Config, err error) {
|
||||||
// Open file
|
// Open file
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
|
@ -34,4 +40,5 @@ func NewConfig(filePath string) (newConfig *Config, err error) {
|
||||||
return &Config{configData: newConfigData}, nil;
|
return &Config{configData: newConfigData}, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Gets a reference copy to the config data */
|
||||||
func (config Config) GetConfigData() map[string][]string {return config.configData;}
|
func (config Config) GetConfigData() map[string][]string {return config.configData;}
|
|
@ -18,5 +18,5 @@ There is a regex-like behavior to these paths using the `*` and `?` characters.
|
||||||
|
|
||||||
The `*` character matches to any number of path segments. The `?` character matches to only one segment. Here's some examples:
|
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/*` 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.
|
* `HZJWwB0TAjz6pjAHosII5ofR /+16028675309/direct/?/send` will allow the bearer token to send a direct message to anyone on that phone number.
|
62
conf/regex.go
Normal file
62
conf/regex.go
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
package conf
|
||||||
|
|
||||||
|
/* This file contains regex helper functions for parsing configs */
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
/* Splits and normalises */
|
||||||
|
func splitPath(path string) []string {
|
||||||
|
return strings.Split(strings.Trim(path, "/"), "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Attempts to match a request path to a set of whitelisted paths
|
||||||
|
@return false for anything other than a valid match */
|
||||||
|
func match(request string, matchTo []string) bool {
|
||||||
|
requestSplit := splitPath(request)
|
||||||
|
for _, matchToAttempt := range matchTo {
|
||||||
|
matchToAttemptSplit := splitPath(matchToAttempt);
|
||||||
|
if matchSegments(requestSplit, matchToAttemptSplit) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Returns false for anything other than a valid match */
|
||||||
|
func matchSegments(request []string, matchTo []string) bool {
|
||||||
|
/* This is a recursive function which, at each recursion level, matches the
|
||||||
|
path segments that are in the front of the request and matchTo lists
|
||||||
|
It matches identical strings, anything to &, and splits in two when
|
||||||
|
matching anything to *, to account for consuming or not consuming the *
|
||||||
|
at the current recursion level. */
|
||||||
|
|
||||||
|
// Recursion base case for perfect match
|
||||||
|
if len(request) == 0 && len(matchTo) == 0 {return true}
|
||||||
|
// End of path for one but not the other
|
||||||
|
if (len(request) & len(matchTo)) == 0 {return false}
|
||||||
|
|
||||||
|
// Grab current path segments
|
||||||
|
requestCurrent := request[0];
|
||||||
|
matchToCurrent := matchTo[0];
|
||||||
|
|
||||||
|
// & character and direct match have the same behavior
|
||||||
|
if (matchToCurrent == "&") || (requestCurrent == matchToCurrent) {
|
||||||
|
return matchSegments(request[1:], matchTo[1:]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// * character
|
||||||
|
if (matchToCurrent == "*") {
|
||||||
|
// These are split for performance
|
||||||
|
// Usually the * only refers to 1 or 2 things so putting consumption
|
||||||
|
// first is probably a better choice
|
||||||
|
if (matchSegments(request[1:], matchTo[1:])) {return true}
|
||||||
|
if (matchSegments(request[1:], matchTo)) {return true}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Code will reach here if there's no match for the current segment
|
||||||
|
return false;
|
||||||
|
}
|
1
main.go
1
main.go
|
@ -17,6 +17,7 @@ func main() {
|
||||||
if !confLocationSet {log.Default().Print("No config value!"); return;}
|
if !confLocationSet {log.Default().Print("No config value!"); return;}
|
||||||
log.Default().Print("Reading config value from ", configLocation);
|
log.Default().Print("Reading config value from ", configLocation);
|
||||||
|
|
||||||
|
// Set up config
|
||||||
config, err := conf.NewConfig(configLocation);
|
config, err := conf.NewConfig(configLocation);
|
||||||
if err != nil {log.Default().Print("Error reading config: ", err); return;}
|
if err != nil {log.Default().Print("Error reading config: ", err); return;}
|
||||||
fmt.Println(config.GetConfigData())
|
fmt.Println(config.GetConfigData())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue