[sans-io] minor fixes, make dev server usable again

This commit is contained in:
Maximilian Hils 2017-12-11 20:18:06 +01:00
parent c8f1b3d7f9
commit a4c357a1a2
3 changed files with 26 additions and 9 deletions

View File

@ -29,8 +29,6 @@ class ProxyConnectionHandler(server.ConnectionHandler):
super().__init__(r, w, options)
async def handle_hook(self, hook: commands.Hook) -> None:
if isinstance(hook, commands.Log):
return # FIXME: these are already logged at the server, the "real" log messes up order.
q = asyncio.Queue()
hook.data.reply = AsyncReply(

View File

@ -8,7 +8,6 @@ The counterpart to commands are events.
"""
import typing
from mitmproxy import log
from mitmproxy.proxy2.context import Connection
@ -86,7 +85,6 @@ class Hook(Command):
class Log(Command):
blocking = False
message: str
level: str

View File

@ -132,21 +132,42 @@ class ConnectionHandler(metaclass=abc.ABCMeta):
class SimpleConnectionHandler(ConnectionHandler):
"""Simple handler that does not process any hooks."""
"""Simple handler that does not really process any hooks."""
async def handle_hook(self, hook: commands.Hook) -> None:
hook_handlers: typing.Dict[str, typing.Callable]
def __init__(self, reader, writer, options, hooks):
super().__init__(reader, writer, options)
self.hook_handlers = hooks
async def handle_hook(
self,
hook: commands.Hook
) -> None:
if hook.name in self.hook_handlers:
self.hook_handlers[hook.name](hook.data)
if hook.blocking:
self.server_event(events.HookReply(hook))
if __name__ == "__main__":
loop = asyncio.get_event_loop()
opts = moptions.Options()
opts.mode = "reverse:example.com"
# opts.mode = "reverse:example.com"
async def handle(reader, writer):
await SimpleConnectionHandler(reader, writer, opts).handle_client()
layer_stack = [
# layers.TLSLayer,
lambda c: layers.HTTPLayer(c, HTTPMode.regular),
layers.TCPLayer,
]
def next_layer(nl: layer.NextLayer):
nl.layer = layer_stack.pop(0)(nl.context)
await SimpleConnectionHandler(reader, writer, opts, {
"next_layer": next_layer
}).handle_client()
coro = asyncio.start_server(handle, '127.0.0.1', 8080, loop=loop)
server = loop.run_until_complete(coro)