Repository navigation
Expand file tree
/
Copy pathbridge.py
More file actions
228 lines (186 loc) · 7.87 KB
/
Copy pathbridge.py
File metadata and controls
228 lines (186 loc) · 7.87 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# _*_ coding: utf-8 _*_
"""
Socket bridge // Send & Receive
http.server: https://docs.python.org/3/library/http.server.html
"""
import os
import ssl
import socket
import select
import sys
import threading
import time
from socketserver import ThreadingMixIn
from urllib.parse import urlsplit
from header import filter_header
import http.client as http_lib
from subprocess import Popen, PIPE
from http.server import BaseHTTPRequestHandler, HTTPServer
REQUEST_TIMEOUT = 20
DEFAULT_CHARSET = 'UTF-8'
RECEIVE_BUFFER_SIZE = 8192
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
address_family = socket.AF_INET6
daemon_threads = True
def handle_error(self, request, client_address):
cls, e = sys.exc_info()[:2]
if cls is socket.error or cls is ssl.SSLError:
pass
else:
return HTTPServer.handle_error(self, request, client_address)
def join_with_script_dir(path):
return os.path.join(os.path.dirname(os.path.abspath(__file__)), path)
class ProxyBridge(BaseHTTPRequestHandler):
lock = threading.Lock()
cakey = join_with_script_dir('ca.key')
ca_cert = join_with_script_dir('ca.crt')
cert_key = join_with_script_dir('cert.key')
cert_dir = join_with_script_dir('certs/')
timeout = 5
def __init__(self, *args, **kwargs):
self.tls = threading.local()
self.tls.conns = dict()
self._headers_buffer = list()
super(ProxyBridge, self).__init__(*args, **kwargs)
def parse_request(self) -> bool:
symbol = super(ProxyBridge, self).parse_request()
self.command = self.command.lower()
return symbol
def do_connect(self):
"""HANDLE HTTPS CONNECT REQUEST
"""
if os.path.isfile(self.cakey) and os.path.isfile(self.ca_cert) and os.path.isfile(
self.cert_key) and os.path.isdir(self.cert_dir):
self.connect_intercept()
else:
self.exec_connect_options()
def connect_intercept(self):
hostname = self.path.split(':')[0]
cert_path = "%s/%s.crt" % (self.cert_dir.rstrip('/'), hostname)
with self.lock:
if not os.path.isfile(cert_path):
epoch = "%d" % (time.time() * 1000)
p1 = Popen(["openssl", "req", "-new", "-key", self.cert_key, "-subj", "/CN=%s" % hostname], stdout=PIPE)
p2 = Popen(["openssl", "x509", "-req", "-days", "3650", "-CA", self.ca_cert, "-CAkey", self.cakey,
"-set_serial", epoch, "-out", cert_path], stdin=p1.stdout, stderr=PIPE)
p2.communicate()
self.wfile.write(
"%s %d %s\r\n" % (self.protocol_version, 200, 'Connection Established')
)
self.end_headers()
self.connection = ssl.wrap_socket(self.connection, keyfile=self.cert_key, certfile=cert_path, server_side=True)
self.rfile = self.connection.makefile("rb", self.rbufsize)
self.wfile = self.connection.makefile("wb", self.wbufsize)
conn_type = self.headers.get('Proxy-Connection', '')
if self.protocol_version == "HTTP/1.1" and conn_type.lower() != 'close':
self.close_connection = False
else:
self.close_connection = True
def do_get(self):
"""Handle all request protocol and all request method
"""
return self.handle_request()
def handle_request(self) -> None:
req = self
content_length = int(req.headers.get('Content-Length', 0))
req_body = self.rfile.read(content_length) if content_length else None
if req.path[0] == '/':
if isinstance(self.connection, ssl.SSLSocket):
req.path = "https://%s%s" % (req.headers['Host'], req.path)
else:
req.path = "http://%s%s" % (req.headers['Host'], req.path)
u = urlsplit(req.path)
scheme, netloc, path = u.scheme, u.netloc, (u.path + '?' + u.query if u.query else u.path)
assert scheme in ('http', 'https')
if netloc:
req.headers['Host'] = netloc
setattr(req, 'headers', filter_header(req.headers))
origin = (scheme, netloc)
try:
if origin not in self.tls.conns:
if scheme == 'https':
self.tls.conns[origin] = http_lib.HTTPSConnection(netloc, timeout=self.timeout)
else:
self.tls.conns[origin] = http_lib.HTTPConnection(netloc, timeout=self.timeout)
conn = self.tls.conns[origin]
conn.request(self.command.upper(), path, req_body, dict(req.headers))
res = conn.getresponse()
version_table = {10: 'HTTP/1.0', 11: 'HTTP/1.1'}
setattr(res, 'response_version', version_table[res.version])
print("Headers: ", res.headers)
if 'Content-Length' not in res.headers and 'no-store' in res.headers.get('Cache-Control', '') \
and 'Content-Encoding' not in res.headers:
self.relay_streaming(res)
return
res_body = res.read()
except Exception as e:
if origin in self.tls.conns:
del self.tls.conns[origin]
self.send_error(502, str(e))
return
if req_body is False:
self.send_error(403)
return
setattr(res, 'headers', filter_header(res.headers))
self.wfile.write(f'{self.protocol_version} {res.status} {res.reason}\r\n'.encode(DEFAULT_CHARSET))
for line in res.headers:
self.wfile.write((line + ': ' + res.headers[line] + '\r\n').encode(DEFAULT_CHARSET))
# self.end_headers()
self._headers_buffer = [b"\r\n"]
self.flush_headers()
self.wfile.write(res_body)
self.wfile.flush()
def write_and_flush_headers(self, headers: list):
self._headers_buffer += headers
self.flush_headers()
def relay_streaming(self, res):
"""查看client 是否断开链接"""
self.wfile.write(f'{self.protocol_version} {res.status} {res.reason}\r\n'.encode(DEFAULT_CHARSET))
for line in res.headers.headers:
self.wfile.write(line)
self.end_headers()
try:
while True:
chunk = res.read(8192)
if not chunk:
break
self.wfile.write(chunk)
self.wfile.flush()
except socket.error:
print('connection closed by client')
def exec_connect_options(self):
host, port = self.path.split(':', 1)
port = int(port) or 443
try:
server = socket.create_connection((host, port), timeout=REQUEST_TIMEOUT)
except Exception as e:
return self.send_error(502, str(e))
self.send_response(200, 'Connection Established')
self.end_headers()
conns = [self.connection, server]
self.close_connection = False
while not self.close_connection:
read_list, write_list, error_list = select.select(conns, [], conns, self.timeout)
if not read_list or error_list:
break
for read in read_list:
symbol = self.exec_read_fd(read_conn=read, conns=conns)
if not symbol:
break
def exec_read_fd(self, read_conn, conns) -> bool:
other = conns[1] if read_conn is conns[0] else conns[0]
buffer = read_conn.recv(RECEIVE_BUFFER_SIZE)
if not buffer:
self.close_connection = True
return False
other.sendall(buffer)
return True
def run(server_class=ThreadingHTTPServer, handler_class=ProxyBridge):
host = sys.argv[1] if len(sys.argv) > 1 else '127.0.0.1'
port = int(sys.argv[2]) if len(sys.argv) > 2 else 10000
print(f'server running at {host}:{port}')
server_address = (host, int(port))
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
if __name__ == '__main__':
run()