Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 192
Fix tunnel proxy and support HTTPS#55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
97040fa29bcacb7211b09bb20c7f96507c8c49dfa9c802c69546fc5f1f4d645File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -140,47 +140,57 @@ async def _tunnel_request( | ||
| connection = await self._get_connection_from_pool(origin) | ||
| if connection is None: | ||
| connection = AsyncHTTPConnection( | ||
| origin=origin, http2=False, ssl_context=self._ssl_context, | ||
| # First, create a connection to the proxy server | ||
| proxy_connection = AsyncHTTPConnection( | ||
| origin=self.proxy_origin, http2=False, ssl_context=self._ssl_context, | ||
| ) | ||
| async with self._thread_lock: | ||
| self._connections.setdefault(origin, set()) | ||
| self._connections[origin].add(connection) | ||
| self._connections[origin].add(proxy_connection) | ||
| # Establish the connection by issuing a CONNECT request... | ||
| # Issue a CONNECT request... | ||
| # CONNECT www.example.org:80 HTTP/1.1 | ||
| # [proxy-headers] | ||
| target = b"%b:%d" % (url[1], url[2]) | ||
| connect_url = self.proxy_origin + (target,) | ||
| connect_headers = self.proxy_headers | ||
| proxy_response = await connection.request( | ||
| connect_headers = self.proxy_headers or [(b"host", self.proxy_origin[1])] | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The host header is required by h11 but not typically passed as part of the proxy headers. Not sure if we want to merge the headers here or leave that to the caller to sort out. | ||
| proxy_response = await proxy_connection.request( | ||
| b"CONNECT", connect_url, headers=connect_headers, timeout=timeout | ||
| ) | ||
| proxy_status_code = proxy_response[1] | ||
| proxy_reason_phrase = proxy_response[2] | ||
| proxy_stream = proxy_response[4] | ||
| # Ingest any request body. | ||
| await read_body(proxy_stream) | ||
| # Read the response data without closing the socket | ||
| _ = b"".join([chunk async for chunk in proxy_stream]) | ||
| # If the proxy responds with an error, then drop the connection | ||
| # from the pool, and raise an exception. | ||
| if proxy_status_code < 200 or proxy_status_code > 299: | ||
| async with self._thread_lock: | ||
| self._connections[connection.origin].remove(connection) | ||
| if not self._connections[connection.origin]: | ||
| del self._connections[connection.origin] | ||
| msg = "%d %s" % (proxy_status_code, proxy_reason_phrase.decode("ascii")) | ||
| raise ProxyError(msg) | ||
| # Upgrade to TLS. | ||
| await connection.start_tls(target, timeout) | ||
| # Upgrade to TLS if required | ||
| # We assume the target speaks TLS on the specified port | ||
| if url[0] == b"https": | ||
| await proxy_connection.start_tls(url[1], timeout) | ||
| # Create a new connection to the target | ||
| connection = AsyncHTTPConnection( | ||
| origin=origin, | ||
| http2=False, | ||
| ssl_context=self._ssl_context, | ||
| socket=proxy_connection.socket, | ||
| ) | ||
| async with self._thread_lock: | ||
| self._connections[origin].remove(proxy_connection) | ||
| self._connections[origin].add(connection) | ||
| # Once the connection has been established we can send requests on | ||
| # it as normal. | ||
| response = await connection.request( | ||
| method, url, headers=headers, stream=stream, timeout=timeout | ||
| method, url, headers=headers, stream=stream, timeout=timeout, | ||
| ) | ||
| wrapped_stream = ResponseByteStream( | ||
| response[4], connection=connection, callback=self._response_closed | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,3 +18,4 @@ autoflake | ||
| mypy | ||
| isort | ||
| mitmproxy | ||
| trustme | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the h11 docs: