Skip to content

Commit

Permalink
feat: Add tool to generate endpoints (#307)
Browse files Browse the repository at this point in the history
* feat: Add tool to generate endpoints
  • Loading branch information
jrconlin authored May 13, 2022
1 parent 8ef8909 commit 2829fa4
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions scripts/gendpoint.py
Original file line number Diff line number Diff line change
@@ -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. '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"https://{args.root}/wpush/v1/{ekey}"


main()

0 comments on commit 2829fa4

Please sign in to comment.