This repository was archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps.py
More file actions
executable file
·59 lines (41 loc) · 1.27 KB
/
https.py
File metadata and controls
executable file
·59 lines (41 loc) · 1.27 KB
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
#!/usr/bin/env python3.6
import types
import ssl
import os
from http.server import (
HTTPServer,
SimpleHTTPRequestHandler)
from ktls.utils import set_ktls_sockopt
def sendall(self, b):
""" overwrite origin socket.sendall
ref: cpython/Lib/socketserver.py +791
"""
fd = self.fileno()
os.write(fd, b)
class HTTPSServer(HTTPServer):
def get_request(self):
""" overwrite origin get_request for setting ktls
ref: cpython/Lib/socketserver.py +490
"""
conn, addr = super().get_request()
# set ktls socket options
conn = set_ktls_sockopt(conn)
conn.sendall = types.MethodType(sendall, conn)
return conn, addr
def run():
host, port = "localhost", 4433
cert, key = 'ktls/ca/cert.pem', 'ktls/ca/key.pem'
handler = SimpleHTTPRequestHandler
# prepare ssl context
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ctx.load_cert_chain(certfile=cert, keyfile=key)
ctx.set_ciphers('ECDH-ECDSA-AES128-GCM-SHA256')
# run the https server
with HTTPSServer((host, port), handler) as httpd:
httpd.socket = ctx.wrap_socket(httpd.socket,
server_side=True)
httpd.serve_forever()
try:
run()
except KeyboardInterrupt:
pass