-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp_server.py
52 lines (42 loc) · 1.89 KB
/
http_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
import http.server
import socketserver
import os
import urllib.parse
import subprocess
from functools import partial
class AuthHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, auth_token=None, **kwargs):
self.auth_token = auth_token
super().__init__(*args, **kwargs)
def do_GET(self):
# Parse URL and query parameters
parsed = urllib.parse.urlparse(self.path)
query = urllib.parse.parse_qs(parsed.query)
# Check authentication for all paths except CSS/JS resources
if not parsed.path.endswith(('.css', '.js')):
auth = query.get('auth', [None])[0]
if not auth or auth != self.auth_token:
self.send_error(403, "Authentication required!!!")
return
# Strip auth parameter from filename query
if 'filename' in query:
filename = query['filename'][0]
timestamp = query.get('timestamp', ['0'])[0]
# Reconstruct path without auth parameter
self.path = f"{parsed.path}?filename={filename}×tamp={timestamp}"
return super().do_GET()
def run_server(port, directory, auth_token):
handler = partial(AuthHandler, auth_token=auth_token, directory=directory)
ip = subprocess.check_output("ip route get 1 | awk '{print $7}'", shell=True).decode().strip()
with socketserver.TCPServer((ip, port), handler) as httpd:
print(f"Serving at port {port} with authentication")
httpd.serve_forever()
if __name__ == "__main__":
port = int(os.environ.get('CINELOG_VIEWER_PORT', 10000))
auth_token = os.environ.get('CINELOG_AUTH_UUID')
directory = os.path.expanduser("~/.zim/modules/cinelog/asciinema-player")
if not auth_token:
print("Error: CINELOG_AUTH_UUID environment variable not set")
exit(1)
run_server(port, directory, auth_token)