|
1
|
#!/usr/bin/env python3 |
|
2
|
# |
|
3
|
# Usage: python3 http-relay.py ADDRESS reset |
|
4
|
# python3 http-relay.py ADDRESS UPSTREAM CERT KEY ?notify? |
|
5
|
# |
|
6
|
# Does for test/http.test what Tcl cannot. With "reset", listens on |
|
7
|
# ADDRESS and resets each connection it accepts. Otherwise it accepts |
|
8
|
# TLS connections on ADDRESS with the certificate CERT and its key KEY, |
|
9
|
# relays each request to the HTTP server on port UPSTREAM of 127.0.0.1, |
|
10
|
# and sends the reply back. It ends the TLS session with close_notify |
|
11
|
# only when "notify" is given. It prints "listening PORT" when ready, |
|
12
|
# and "SNI: NAME" for the TLS server name each client sends. |
|
13
|
|
|
14
|
import re |
|
15
|
import socket |
|
16
|
import ssl |
|
17
|
import struct |
|
18
|
import sys |
|
19
|
import threading |
|
20
|
|
|
21
|
|
|
22
|
# Returns the HTTP request that CLIENT sends, with its body. |
|
23
|
def read_request(client): |
|
24
|
data = b"" |
|
25
|
while b"\r\n\r\n" not in data: |
|
26
|
part = client.recv(65536) |
|
27
|
if not part: |
|
28
|
return data |
|
29
|
data += part |
|
30
|
header = data.split(b"\r\n\r\n", 1)[0] |
|
31
|
match = re.search(rb"(?im)^content-length:\s*(\d+)", header) |
|
32
|
length = len(header) + 4 + (int(match.group(1)) if match else 0) |
|
33
|
while len(data) < length: |
|
34
|
part = client.recv(65536) |
|
35
|
if not part: |
|
36
|
break |
|
37
|
data += part |
|
38
|
return data |
|
39
|
|
|
40
|
|
|
41
|
# Serves one TLS connection: sends the request to the server on port |
|
42
|
# UPSTREAM and the whole reply back, then ends the TLS session. |
|
43
|
def relay(context, client, upstream, notify): |
|
44
|
try: |
|
45
|
client = context.wrap_socket(client, server_side=True) |
|
46
|
request = read_request(client) |
|
47
|
reply = b"" |
|
48
|
with socket.create_connection(("127.0.0.1", upstream)) as server: |
|
49
|
server.sendall(request) |
|
50
|
while part := server.recv(65536): |
|
51
|
reply += part |
|
52
|
client.sendall(reply) |
|
53
|
if notify: |
|
54
|
client = client.unwrap() |
|
55
|
else: |
|
56
|
client.shutdown(socket.SHUT_RDWR) |
|
57
|
except OSError: |
|
58
|
pass |
|
59
|
client.close() |
|
60
|
|
|
61
|
|
|
62
|
# Resets each connection that LISTENER accepts. Closing a socket whose |
|
63
|
# linger time is zero sends a reset. |
|
64
|
def reset_connections(listener): |
|
65
|
while True: |
|
66
|
client, _ = listener.accept() |
|
67
|
client.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, |
|
68
|
struct.pack("ii", 1, 0)) |
|
69
|
client.close() |
|
70
|
|
|
71
|
|
|
72
|
def print_server_name(tls_socket, name, context): |
|
73
|
print("SNI:", name, flush=True) |
|
74
|
|
|
75
|
|
|
76
|
# Relays each TLS connection that LISTENER accepts, in a thread of its |
|
77
|
# own. |
|
78
|
def relay_connections(listener, upstream, cert, key, notify): |
|
79
|
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) |
|
80
|
context.load_cert_chain(cert, key) |
|
81
|
context.sni_callback = print_server_name |
|
82
|
while True: |
|
83
|
client, _ = listener.accept() |
|
84
|
thread = threading.Thread( |
|
85
|
target=relay, args=(context, client, upstream, notify), |
|
86
|
daemon=True) |
|
87
|
thread.start() |
|
88
|
|
|
89
|
|
|
90
|
def main(): |
|
91
|
address = sys.argv[1] |
|
92
|
family = socket.AF_INET6 if ":" in address else socket.AF_INET |
|
93
|
listener = socket.socket(family) |
|
94
|
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
|
95
|
listener.bind((address, 0)) |
|
96
|
listener.listen(8) |
|
97
|
print("listening", listener.getsockname()[1], flush=True) |
|
98
|
if sys.argv[2] == "reset": |
|
99
|
reset_connections(listener) |
|
100
|
else: |
|
101
|
upstream = int(sys.argv[2]) |
|
102
|
cert, key = sys.argv[3:5] |
|
103
|
notify = sys.argv[5:] == ["notify"] |
|
104
|
relay_connections(listener, upstream, cert, key, notify) |
|
105
|
|
|
106
|
|
|
107
|
main() |
|
108
|
|