-
Notifications
You must be signed in to change notification settings - Fork 2
/
callback-server.py
60 lines (45 loc) · 1.83 KB
/
callback-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
53
54
55
56
57
58
59
60
from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl, subprocess, shlex, sys
import threading
import tempfile
# How to run: FIXME:
# python callback-server
if len(sys.argv) < 2:
print("No hostname given!")
sys.exit(1)
# NAT settings:
# 80 --> 50080
# 443 --> 50443
tmpdir = tempfile.TemporaryDirectory(prefix="tmp_server", dir="/tmp")
serving_dir = tempfile.TemporaryDirectory(prefix="serving", dir=tmpdir.name) # Feel free to change
with open(serving_dir.name+"/index.html", 'w') as f: # sanity check file
f.write("<html><body>Hello World</body></html>")
# Generate SSL params for HTTPS server
subprocess.check_call(
shlex.split('openssl req -nodes -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -subj') + [f'/CN={sys.argv[1]}'],
cwd=tmpdir.name
)
class Handler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=serving_dir.name, **kwargs)
def run_http_server():
server_address = ('0.0.0.0', 50080)
httpd = HTTPServer(server_address, Handler)
print(f"Server running on http://{server_address[0]}:{server_address[1]}")
httpd.serve_forever()
def run_https_server():
server_address = ('0.0.0.0', 50443)
httpd = HTTPServer(server_address, Handler)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=tmpdir.name+'/key.pem', certfile=tmpdir.name+"/cert.pem", server_side=True)
print(f"Server running on https://{server_address[0]}:{server_address[1]}")
httpd.serve_forever()
print(f"Root directory: {tmpdir.name}")
print(f" [*] Web Serving directory: {serving_dir.name}")
http_server = threading.Thread(target=run_http_server)
http_server.start()
https_server = threading.Thread(target=run_https_server)
https_server.start()
http_server.join()
https_server.join()
serving_dir.cleanup()
tmpdir.cleanup()