diff --git a/mitmproxy/proxy2/context.py b/mitmproxy/proxy2/context.py index be152b4c9..ca58692b8 100644 --- a/mitmproxy/proxy2/context.py +++ b/mitmproxy/proxy2/context.py @@ -17,9 +17,10 @@ class Connection: """ Connections exposed to the layers only contain metadata, no socket objects. """ - address: Optional[tuple] - local_address: Optional[tuple] state: ConnectionState + peername: Optional[tuple] + sockname: Optional[tuple] + tls: bool = False tls_established: bool = False certificate_chain: Optional[Sequence[certs.Cert]] = None @@ -37,28 +38,34 @@ class Connection: def __repr__(self): attrs = repr({ - k: {"cipher_list": lambda: f"<{len(v)} ciphers>"}.get(k,lambda: v)() + k: {"cipher_list": lambda: f"<{len(v)} ciphers>"}.get(k, lambda: v)() for k, v in self.__dict__.items() }) return f"{type(self).__name__}({attrs})" class Client(Connection): - sni: Union[bytes, None] = None - address: tuple state = ConnectionState.OPEN + peername: tuple + sockname: tuple - def __init__(self, address, local_address): - self.address = address - self.local_address = local_address + sni: Union[bytes, None] = None + + def __init__(self, peername, sockname): + self.peername = peername + self.sockname = sockname class Server(Connection): + state = ConnectionState.CLOSED + + peername = None + sockname = None + address: Optional[tuple] + sni = True """True: client SNI, False: no SNI, bytes: custom value""" via: Optional[server_spec.ServerSpec] = None - state = ConnectionState.CLOSED - local_address = None def __init__(self, address: Optional[tuple]): self.address = address diff --git a/mitmproxy/proxy2/server.py b/mitmproxy/proxy2/server.py index c136bc059..af05d6d34 100644 --- a/mitmproxy/proxy2/server.py +++ b/mitmproxy/proxy2/server.py @@ -80,10 +80,10 @@ class ConnectionHandler(metaclass=abc.ABCMeta): timeout_watchdog: TimeoutWatchdog def __init__(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, options: moptions.Options) -> None: - addr = writer.get_extra_info('peername') - local_addr = writer.get_extra_info('sockname') - - self.client = Client(addr, local_addr) + self.client = Client( + writer.get_extra_info('peername'), + writer.get_extra_info('sockname') + ) self.context = Context(self.client, options) self.transports = { self.client: ConnectionIO(handler=None, reader=reader, writer=writer) @@ -133,6 +133,8 @@ class ConnectionHandler(metaclass=abc.ABCMeta): self.transports[command.connection].reader = reader self.transports[command.connection].writer = writer command.connection.state = ConnectionState.OPEN + command.connection.peername = writer.get_extra_info('peername') + command.connection.sockname = writer.get_extra_info('sockname') self.server_event(events.OpenConnectionReply(command, None)) try: await self.handle_connection(command.connection) diff --git a/test/mitmproxy/proxy2/layers/test_http.py b/test/mitmproxy/proxy2/layers/test_http.py index 1b1bd2e80..87c30b7cf 100644 --- a/test/mitmproxy/proxy2/layers/test_http.py +++ b/test/mitmproxy/proxy2/layers/test_http.py @@ -518,7 +518,6 @@ def test_proxy_chain(tctx, strategy): assert playbook -@pytest.mark.xfail(reason="h11 enforces host headers by default") def test_no_headers(tctx): """Test that we can correctly reassemble requests/responses with no headers.""" server = Placeholder()