Fix server addr issue in tls_passthrough example. (#5904)

* fix server peername is None issue

peername would be None, we should use other not None property as key.

* Update tls_passthrough.py

---------

Co-authored-by: Maximilian Hils <github@maximilianhils.com>
This commit is contained in:
Xiao Wang 2023-02-07 16:57:24 +08:00 committed by GitHub
parent b9f3574728
commit 430833e3d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -2,6 +2,9 @@
## Unreleased: mitmproxy next
* Fix a bug where peername would be None in tls_passthrough script, which would make it not working.
([#5904](https://github.com/mitmproxy/mitmproxy/pull/5904), @truebit)
* Add experimental QUIC support.
([#5435](https://github.com/mitmproxy/mitmproxy/issues/5435), @meitinger)
* ASGI/WSGI apps can now listen on all ports for a specific hostname.

View File

@ -95,22 +95,27 @@ class MaybeTls:
else:
self.strategy = ConservativeStrategy()
@staticmethod
def get_addr(server: connection.Server):
# .peername may be unset in upstream proxy mode, so we need a fallback.
return server.peername or server.address
def tls_clienthello(self, data: tls.ClientHelloData):
server_address = data.context.server.peername
server_address = self.get_addr(data.context.server)
if not self.strategy.should_intercept(server_address):
logging.info(f"TLS passthrough: {human.format_address(server_address)}.")
data.ignore_connection = True
self.strategy.record_skipped(server_address)
def tls_established_client(self, data: tls.TlsData):
server_address = data.context.server.peername
server_address = self.get_addr(data.context.server)
logging.info(
f"TLS handshake successful: {human.format_address(server_address)}"
)
self.strategy.record_success(server_address)
def tls_failed_client(self, data: tls.TlsData):
server_address = data.context.server.peername
server_address = self.get_addr(data.context.server)
logging.info(f"TLS handshake failed: {human.format_address(server_address)}")
self.strategy.record_failure(server_address)