diff --git a/mitmproxy/proxy2/context.py b/mitmproxy/proxy2/context.py index 86bf907d1..c49c6d557 100644 --- a/mitmproxy/proxy2/context.py +++ b/mitmproxy/proxy2/context.py @@ -27,8 +27,9 @@ class Client(Connection): class Server(Connection): sni: Union[bytes, bool] = True """True: client SNI, False: no SNI, bytes: custom value""" + address: Optional[tuple] - def __init__(self, address): + def __init__(self, address: Optional[tuple]): self.address = address @@ -38,17 +39,16 @@ class Context: """ client: Client - server: Optional[Server] + server: Server options: Options layers: List["mitmproxy.proxy2.layer.Layer"] def __init__( self, client: Client, - server: Optional[Server], options: Options, ) -> None: self.client = client - self.server = server self.options = options + self.server = Server(None) self.layers = [] diff --git a/mitmproxy/proxy2/server.py b/mitmproxy/proxy2/server.py index 173888f34..68da2ce4f 100644 --- a/mitmproxy/proxy2/server.py +++ b/mitmproxy/proxy2/server.py @@ -29,7 +29,7 @@ class ConnectionHandler(metaclass=abc.ABCMeta): addr = writer.get_extra_info('peername') self.client = Client(addr) - self.context = Context(self.client, None, options) + self.context = Context(self.client, options) if options.mode.startswith("reverse:"): self.layer = ReverseProxy(self.context) @@ -83,6 +83,8 @@ class ConnectionHandler(metaclass=abc.ABCMeta): break async def open_connection(self, command: commands.OpenConnection): + if not command.connection.address: + raise ValueError("Cannot open connection, no hostname given.") try: reader, writer = await asyncio.open_connection( *command.connection.address