[sans-io] make context.server non-optional

this makes for a nicer API, we just check for a missing address now.
This commit is contained in:
Maximilian Hils 2017-12-11 20:13:28 +01:00
parent edaf3219fc
commit c8f1b3d7f9
2 changed files with 7 additions and 5 deletions

View File

@ -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 = []

View File

@ -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