From c8f1b3d7f93494631c62daa5536dec7c55b3e867 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 11 Dec 2017 20:13:28 +0100 Subject: [PATCH] [sans-io] make context.server non-optional this makes for a nicer API, we just check for a missing address now. --- mitmproxy/proxy2/context.py | 8 ++++---- mitmproxy/proxy2/server.py | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) 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