-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpx_proxy.py
More file actions
376 lines (336 loc) · 11.6 KB
/
Copy pathhttpx_proxy.py
File metadata and controls
376 lines (336 loc) · 11.6 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from contextlib import contextmanager
from httpcore._sync.http_proxy import HTTPProxy, TunnelHTTPConnection, merge_headers, logger
from httpcore._sync.http11 import HTTP11Connection
from httpcore._async.http_proxy import AsyncHTTPProxy, AsyncTunnelHTTPConnection
from httpcore._async.http11 import AsyncHTTP11Connection
from httpcore._models import URL, Request
from httpcore._exceptions import ProxyError
from httpcore._ssl import default_ssl_context
from httpcore._trace import Trace
from httpx import AsyncHTTPTransport, HTTPTransport, Client
from httpx._config import DEFAULT_LIMITS, DEFAULT_TIMEOUT_CONFIG, Proxy, create_ssl_context
class ProxyTunnelHTTPConnection(TunnelHTTPConnection):
# Unfortunately the only way to get connect_response.headers into the Response
# is to override this whole method
def handle_request(self, request):
timeouts = request.extensions.get("timeout", {})
timeout = timeouts.get("connect", None)
with self._connect_lock:
if not self._connected:
target = b"%b:%d" % (self._remote_origin.host, self._remote_origin.port)
connect_url = URL(
scheme=self._proxy_origin.scheme,
host=self._proxy_origin.host,
port=self._proxy_origin.port,
target=target,
)
connect_headers = merge_headers(
[(b"Host", target), (b"Accept", b"*/*")], self._proxy_headers
)
connect_request = Request(
method=b"CONNECT",
url=connect_url,
headers=connect_headers,
extensions=request.extensions,
)
connect_response = self._connection.handle_request(
connect_request
)
if connect_response.status < 200 or connect_response.status > 299:
reason_bytes = connect_response.extensions.get("reason_phrase", b"")
reason_str = reason_bytes.decode("ascii", errors="ignore")
msg = "%d %s" % (connect_response.status, reason_str)
self._connection.close()
raise ProxyError(msg)
stream = connect_response.extensions["network_stream"]
# Upgrade the stream to SSL
ssl_context = (
default_ssl_context()
if self._ssl_context is None
else self._ssl_context
)
alpn_protocols = ["http/1.1", "h2"] if self._http2 else ["http/1.1"]
ssl_context.set_alpn_protocols(alpn_protocols)
kwargs = {
"ssl_context": ssl_context,
"server_hostname": self._remote_origin.host.decode("ascii"),
"timeout": timeout,
}
with Trace("start_tls", logger, request, kwargs) as trace:
stream = stream.start_tls(**kwargs)
trace.return_value = stream
# Determine if we should be using HTTP/1.1 or HTTP/2
ssl_object = stream.get_extra_info("ssl_object")
http2_negotiated = (
ssl_object is not None
and ssl_object.selected_alpn_protocol() == "h2"
)
# Create the HTTP/1.1 or HTTP/2 connection
if http2_negotiated or (self._http2 and not self._http1):
from httpcore._sync.http2 import HTTP2Connection
self._connection = HTTP2Connection(
origin=self._remote_origin,
stream=stream,
keepalive_expiry=self._keepalive_expiry,
)
else:
self._connection = HTTP11Connection(
origin=self._remote_origin,
stream=stream,
keepalive_expiry=self._keepalive_expiry,
)
self._connected = True
# this is the only modification
response = self._connection.handle_request(request)
response.headers = merge_headers(response.headers, connect_response.headers)
return response
class AsyncProxyTunnelHTTPConnection(AsyncTunnelHTTPConnection):
async def handle_async_request(self, request):
timeouts = request.extensions.get("timeout", {})
timeout = timeouts.get("connect", None)
async with self._connect_lock:
if not self._connected:
target = b"%b:%d" % (self._remote_origin.host, self._remote_origin.port)
connect_url = URL(
scheme=self._proxy_origin.scheme,
host=self._proxy_origin.host,
port=self._proxy_origin.port,
target=target,
)
connect_headers = merge_headers(
[(b"Host", target), (b"Accept", b"*/*")], self._proxy_headers
)
connect_request = Request(
method=b"CONNECT",
url=connect_url,
headers=connect_headers,
extensions=request.extensions,
)
connect_response = await self._connection.handle_async_request(
connect_request
)
if connect_response.status < 200 or connect_response.status > 299:
reason_bytes = connect_response.extensions.get("reason_phrase", b"")
reason_str = reason_bytes.decode("ascii", errors="ignore")
msg = "%d %s" % (connect_response.status, reason_str)
await self._connection.aclose()
raise ProxyError(msg)
stream = connect_response.extensions["network_stream"]
# Upgrade the stream to SSL
ssl_context = (
default_ssl_context()
if self._ssl_context is None
else self._ssl_context
)
alpn_protocols = ["http/1.1", "h2"] if self._http2 else ["http/1.1"]
ssl_context.set_alpn_protocols(alpn_protocols)
kwargs = {
"ssl_context": ssl_context,
"server_hostname": self._remote_origin.host.decode("ascii"),
"timeout": timeout,
}
async with Trace("start_tls", logger, request, kwargs) as trace:
stream = await stream.start_tls(**kwargs)
trace.return_value = stream
# Determine if we should be using HTTP/1.1 or HTTP/2
ssl_object = stream.get_extra_info("ssl_object")
http2_negotiated = (
ssl_object is not None
and ssl_object.selected_alpn_protocol() == "h2"
)
# Create the HTTP/1.1 or HTTP/2 connection
if http2_negotiated or (self._http2 and not self._http1):
from httpcore._async.http2 import AsyncHTTP2Connection
self._connection = AsyncHTTP2Connection(
origin=self._remote_origin,
stream=stream,
keepalive_expiry=self._keepalive_expiry,
)
else:
self._connection = AsyncHTTP11Connection(
origin=self._remote_origin,
stream=stream,
keepalive_expiry=self._keepalive_expiry,
)
self._connected = True
# this is the only modification
response = await self._connection.handle_async_request(request)
response.headers = merge_headers(response.headers, connect_response.headers)
return response
class HTTPProxyHeaders(HTTPProxy):
def create_connection(self, origin):
if origin.scheme == b"http":
return super().create_connection(origin)
return ProxyTunnelHTTPConnection(
proxy_origin=self._proxy_url.origin,
proxy_headers=self._proxy_headers,
remote_origin=origin,
ssl_context=self._ssl_context,
proxy_ssl_context=self._proxy_ssl_context,
keepalive_expiry=self._keepalive_expiry,
http1=self._http1,
http2=self._http2,
network_backend=self._network_backend,
)
class AsyncHTTPProxyHeaders(AsyncHTTPProxy):
def create_connection(self, origin):
if origin.scheme == b"http":
return super().create_connection(origin)
return AsyncProxyTunnelHTTPConnection(
proxy_origin=self._proxy_url.origin,
proxy_headers=self._proxy_headers,
remote_origin=origin,
ssl_context=self._ssl_context,
proxy_ssl_context=self._proxy_ssl_context,
keepalive_expiry=self._keepalive_expiry,
http1=self._http1,
http2=self._http2,
network_backend=self._network_backend,
)
# class ProxyConnectionPool(ConnectionPool):
# def create_connection(self, origin):
# if self._proxy is not None:
# if self._proxy.url.scheme in (b"socks5", b"socks5h"):
# return super().create_connection(origin)
# elif origin.scheme == b"http":
# return super().create_connection(origin)
# return ProxyTunnelHTTPConnection(
# proxy_origin=self._proxy.url.origin,
# proxy_headers=self._proxy.headers,
# proxy_ssl_context=self._proxy.ssl_context,
# remote_origin=origin,
# ssl_context=self._ssl_context,
# keepalive_expiry=self._keepalive_expiry,
# http1=self._http1,
# http2=self._http2,
# network_backend=self._network_backend,
# )
# return super().create_connection(origin)
class HTTPProxyTransport(HTTPTransport):
def __init__(
self,
verify = True,
cert = None,
trust_env: bool = True,
http1: bool = True,
http2: bool = False,
limits = DEFAULT_LIMITS,
proxy = None,
uds: str | None = None,
local_address: str | None = None,
retries: int = 0,
socket_options = None,
) -> None:
proxy = Proxy(url=proxy) if isinstance(proxy, (str, URL)) else proxy
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
if proxy and proxy.url.scheme in ("http", "https"):
self._pool = HTTPProxyHeaders(
proxy_url=URL(
scheme=proxy.url.raw_scheme,
host=proxy.url.raw_host,
port=proxy.url.port,
target=proxy.url.raw_path,
),
proxy_auth=proxy.raw_auth,
proxy_headers=proxy.headers.raw,
ssl_context=ssl_context,
proxy_ssl_context=proxy.ssl_context,
max_connections=limits.max_connections,
max_keepalive_connections=limits.max_keepalive_connections,
keepalive_expiry=limits.keepalive_expiry,
http1=http1,
http2=http2,
socket_options=socket_options,
)
else:
super().__init__(verify, cert, trust_env, http1, http2, limits, proxy, uds, local_address, retries, socket_options)
class AsyncHTTPProxyTransport(AsyncHTTPTransport):
def __init__(
self,
verify = True,
cert = None,
trust_env: bool = True,
http1: bool = True,
http2: bool = False,
limits = DEFAULT_LIMITS,
proxy = None,
uds: str | None = None,
local_address: str | None = None,
retries: int = 0,
socket_options = None,
) -> None:
proxy = Proxy(url=proxy) if isinstance(proxy, (str, URL)) else proxy
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
if proxy and proxy.url.scheme in ("http", "https"):
self._pool = AsyncHTTPProxyHeaders(
proxy_url=URL(
scheme=proxy.url.raw_scheme,
host=proxy.url.raw_host,
port=proxy.url.port,
target=proxy.url.raw_path,
),
proxy_auth=proxy.raw_auth,
proxy_headers=proxy.headers.raw,
proxy_ssl_context=proxy.ssl_context,
ssl_context=ssl_context,
max_connections=limits.max_connections,
max_keepalive_connections=limits.max_keepalive_connections,
keepalive_expiry=limits.keepalive_expiry,
http1=http1,
http2=http2,
socket_options=socket_options,
)
else:
super().__init__(verify, cert, trust_env, http1, http2, limits, proxy, uds, local_address, retries, socket_options)
def request(method: str,
url: URL | str,
*,
cookies = None,
proxy = None,
timeout = DEFAULT_TIMEOUT_CONFIG,
verify = True,
trust_env: bool = True,
**kwargs):
transport = HTTPProxyTransport(proxy=proxy)
with Client(
cookies=cookies,
verify=verify,
timeout=timeout,
trust_env=trust_env,
mounts={'http://': transport, 'https://': transport}
) as client:
return client.request(method=method, url=url, **kwargs)
def get(*args, **kwargs):
return request('GET', *args, **kwargs)
def options(*args, **kwargs):
return request('OPTIONS', *args, **kwargs)
def head(*args, **kwargs):
return request('HEAD', *args, **kwargs)
def post(*args, **kwargs):
return request('POST', *args, **kwargs)
def put(*args, **kwargs):
return request('PUT', *args, **kwargs)
def patch(*args, **kwargs):
return request('PATCH', *args, **kwargs)
def delete(*args, **kwargs):
return request('DELETE', *args, **kwargs)
@contextmanager
def stream(method: str,
url: URL | str,
*,
cookies = None,
proxy = None,
timeout = DEFAULT_TIMEOUT_CONFIG,
verify = True,
trust_env: bool = True,
**kwargs):
transport = HTTPProxyTransport(proxy=proxy)
with Client(
cookies=cookies,
verify=verify,
timeout=timeout,
trust_env=trust_env,
mounts={'http://': transport, 'https://': transport}
) as client:
with client.stream(method=method, url=url, **kwargs) as response:
yield response