From 3fcb3f0773259caf94a5d2708183de8273bf1257 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 19 Sep 2022 17:04:40 +0200 Subject: [PATCH] set `client.sockname` to original destination for transparent modes If we don't do this we have non-unique connection tuples, which would be bad. --- mitmproxy/addons/proxyserver.py | 2 +- mitmproxy/proxy/mode_servers.py | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mitmproxy/addons/proxyserver.py b/mitmproxy/addons/proxyserver.py index ec11cea3a..9d592ccd7 100644 --- a/mitmproxy/addons/proxyserver.py +++ b/mitmproxy/addons/proxyserver.py @@ -270,7 +270,7 @@ class Proxyserver(ServerManager): def inject_event(self, event: events.MessageInjected): connection_id = ( - "tcp", + event.flow.client_conn.transport_protocol, event.flow.client_conn.peername, event.flow.client_conn.sockname, ) diff --git a/mitmproxy/proxy/mode_servers.py b/mitmproxy/proxy/mode_servers.py index 60762626b..1baebea34 100644 --- a/mitmproxy/proxy/mode_servers.py +++ b/mitmproxy/proxy/mode_servers.py @@ -139,11 +139,6 @@ class ServerInstance(Generic[M], metaclass=ABCMeta): reader: asyncio.StreamReader | wg.TcpStream, writer: asyncio.StreamWriter | wg.TcpStream, ) -> None: - connection_id = ( - "tcp", - writer.get_extra_info("peername"), - writer.get_extra_info("sockname"), - ) handler = ProxyConnectionHandler( ctx.master, reader, writer, ctx.options, self.mode ) @@ -152,12 +147,23 @@ class ServerInstance(Generic[M], metaclass=ABCMeta): socket = writer.get_extra_info("socket") try: assert platform.original_addr - handler.layer.context.server.address = platform.original_addr(socket) + original_dst = platform.original_addr(socket) except Exception as e: logger.error(f"Transparent mode failure: {e!r}") return + else: + handler.layer.context.client.sockname = original_dst + handler.layer.context.server.address = original_dst elif isinstance(self.mode, mode_specs.WireGuardMode): - handler.layer.context.server.address = writer.get_extra_info("original_dst") + original_dst = writer.get_extra_info("original_dst") + handler.layer.context.client.sockname = original_dst + handler.layer.context.server.address = original_dst + + connection_id = ( + handler.layer.context.client.transport_protocol, + handler.layer.context.client.peername, + handler.layer.context.client.sockname, + ) with self.manager.register_connection(connection_id, handler): await handler.handle_client()