From aec45bd8b3d6f6e8eeb47b725a37f744d06bc0da Mon Sep 17 00:00:00 2001 From: jrconlin Date: Tue, 10 May 2022 16:20:02 -0700 Subject: [PATCH 1/2] feat: Add tool to generate endpoints --- scripts/gendpoint.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 scripts/gendpoint.py diff --git a/scripts/gendpoint.py b/scripts/gendpoint.py new file mode 100644 index 000000000..f4442f6b1 --- /dev/null +++ b/scripts/gendpoint.py @@ -0,0 +1,56 @@ +#! env python3 + +import argparse +import os + +from cryptography.fernet import Fernet + + +def config(env_args: os._Environ) -> argparse.Namespace: + """Read the configuration from the args and environment.""" + parser = argparse.ArgumentParser( + description="Generate Endpoint from environment and known UAID + CHID") + parser.add_argument( + '--key', '-k', + help="Endpoint cryptographic keys. Can be either a single key " + "string or a list of strings. Will only use the first key" + " found.", + default=env_args.get("AUTOEND_CRYPTO_KEYS")) + parser.add_argument( + '--uaid', '-u', required=True, + help="User Agent ID") + parser.add_argument( + '--chid', '-c', required=True, + help="Channel ID") + parser.add_argument( + '--root', '-r', + default=env_args.get("AUTOEND_ENDPOINT_URL"), + help="Server root (e.g. 'https://example.com')") + args = parser.parse_args() + if not args.key: + raise Exception("Missing key") + if not args.root: + raise Exception("Missing root") + return args + + +def main(): + args = config(os.environ) + if isinstance(args.key, list): + key = args.key[0] + else: + key = args.key + print(gendpoint(key, args)) + + +def gendpoint(key: bytes, args: argparse.Namespace) -> str: + """Generate the endpoint""" + fernet = Fernet(key) + base = bytes( + list((bytes.fromhex(args.uaid.replace('-', '')) + + bytes.fromhex(args.chid.replace('-', ''))))) + ekey = fernet.encrypt(base).strip(b'=').decode('utf-8') + return f"{args.root}/v1/{ekey}" + + +main() From e7340f2532063b8567b2077c58e4fd533f526dbb Mon Sep 17 00:00:00 2001 From: jrconlin Date: Thu, 12 May 2022 15:02:58 -0700 Subject: [PATCH 2/2] f fix gen'd url --- scripts/gendpoint.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/gendpoint.py b/scripts/gendpoint.py index f4442f6b1..17bbb1d16 100644 --- a/scripts/gendpoint.py +++ b/scripts/gendpoint.py @@ -25,7 +25,7 @@ def config(env_args: os._Environ) -> argparse.Namespace: parser.add_argument( '--root', '-r', default=env_args.get("AUTOEND_ENDPOINT_URL"), - help="Server root (e.g. 'https://example.com')") + help="Server root (e.g. 'example.com')") args = parser.parse_args() if not args.key: raise Exception("Missing key") @@ -50,7 +50,7 @@ def gendpoint(key: bytes, args: argparse.Namespace) -> str: list((bytes.fromhex(args.uaid.replace('-', '')) + bytes.fromhex(args.chid.replace('-', ''))))) ekey = fernet.encrypt(base).strip(b'=').decode('utf-8') - return f"{args.root}/v1/{ekey}" + return f"https://{args.root}/wpush/v1/{ekey}" main()