diff --git a/.gitignore b/.gitignore index fa5320b..69061e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ redis -docker-compose.yml +.env diff --git a/docker-compose.example.yml b/docker-compose.yml similarity index 87% rename from docker-compose.example.yml rename to docker-compose.yml index 995c85b..9ce70c8 100644 --- a/docker-compose.example.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: build: . ports: [ "80:80" ] environment: - - SECRET=secret + - SECRET=${SECRET} redis: image: redis:7 volumes: [ "./redis:/data" ] diff --git a/readme b/readme index 614061f..c1ac13c 100644 --- a/readme +++ b/readme @@ -2,3 +2,29 @@ sus === simple URL shortener + + +usage +----- + +running the server + +1. generate the secret used to create and delete shortlinks + + echo SECRET=`pwgen -s 64 1` >> .env + +2. run the services + + docker-compose up -d --build + +setting up susmng + +1. install susmng + + make install-susmng + +2. create config files + + susmng init + +3. edit config files (~/.config/susmng/config.json) to add your secrets diff --git a/susmng.py b/susmng.py index 290b08e..9b1b0b5 100755 --- a/susmng.py +++ b/susmng.py @@ -6,6 +6,7 @@ import hmac import hashlib import pathlib import os +import json def get_args(): @@ -13,26 +14,43 @@ def get_args(): import argparse parser = argparse.ArgumentParser() - parser.add_argument('susserver') parser.add_argument('command') - parser.add_argument('shortlink') - parser.add_argument('value') - parser.add_argument('--secret-file', type=pathlib.Path, default=pathlib.Path(os.path.expanduser('~/.susmng_secret'))) - parser.add_argument('--http', action='store_true') + parser.add_argument('-s', '--server', default="") + parser.add_argument('-l', '--shortlink', default="") + parser.add_argument('-v', '--value', default="") + parser.add_argument('-c', '--config', type=pathlib.Path, default=pathlib.Path(os.path.expanduser('~/.config/susmng/config.json'))) + parser.add_argument('-H', '--http', action='store_true') return parser.parse_args() def main(args): """ Entry point for script """ - if not args.secret_file.exists(): - print(f"secret file does not exist at: {args.secret_file}") + if args.command == "init": + print(f"creating config file and quitting") + + if args.config.exists(): + print("config file exists... doing nothing") + return + + with open(args.config, 'w+') as fp: + json.dump({ 'secrets': { 'sus.example.com': 'secret' } }, fp, indent=2) + return - with open(args.secret_file) as fp: - secret = fp.read().strip() + with open(args.config) as fp: + config = json.load(fp) - r = requests.post(f"{'http' if args.http else 'https'}://{args.susserver}", + server = args.server + if server == "": + server = list(config['secrets'].keys())[0] + + secret = config['secrets'][server] + + if args.command == "delete" and args.value != "confirm": + print("--value not set to 'confirm'... delete operation may fail") + + r = requests.post(f"{'http' if args.http else 'https'}://{server}", data = { 'Command': args.command, 'Shortlink': args.shortlink,