From e2bbae5b99fa4d6309c26b8d6639c94e69534e67 Mon Sep 17 00:00:00 2001 From: Siarhei Siniak Date: Sun, 19 May 2024 12:54:50 +0300 Subject: [PATCH] [~] Refactor --- dotfiles/.local/bin/commands | 41 ++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/dotfiles/.local/bin/commands b/dotfiles/.local/bin/commands index 81de7aa..d45aae4 100755 --- a/dotfiles/.local/bin/commands +++ b/dotfiles/.local/bin/commands @@ -1056,12 +1056,25 @@ def http_server(argv): action='store_true', default=False, ) + parser.add_option( + '--token', + dest='token', + type=str, + default=None, + ) parser.add_option( '--port', dest='port', type='int', default=80, ) + parser.add_option( + '-H', '--header', + dest='response_headers', + type='str', + action='append', + default=[], + ) parser.add_option( '--host', dest='host', @@ -1092,17 +1105,29 @@ def http_server(argv): if options.public: location_section = 'location / {%s}' % index_section else: - token = os.urandom(16).hex() + if options.token is None: + options.token = os.urandom(16).hex() if not options.prefix is None: - token = options.prefix + token + path = options.prefix + options.token + else: + path = options.token + print( 'access url is http://%s:%d/%s/' % ( options.host, options.port, - token, + path, ) ) + + assert all([ + not re.compile( + r'^[A-Za-z-]+ [a-z0-9A-Z-\.]+$' + ).match(o) is None + for o in options.response_headers + ]) + location_section = ( 'location / {' 'deny all;' @@ -1110,8 +1135,16 @@ def http_server(argv): 'location /%s/ {' 'alias /app/;' '%s' + '%s' '}' - ) % (token, index_section) + ) % ( + path, + '\n'.join([ + 'add_header %s;' % o + for o in options.response_headers + ]), + index_section + ) subprocess.check_call( r''' sudo docker run \