diff --git a/mitmproxy/addons/proxyserver.py b/mitmproxy/addons/proxyserver.py index 5ec61be97..7e7b05501 100644 --- a/mitmproxy/addons/proxyserver.py +++ b/mitmproxy/addons/proxyserver.py @@ -29,6 +29,8 @@ 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() submit = lambda x: self.loop.call_soon_threadsafe(lambda: q.put_nowait(x)) hook.data.reply = AsyncReply(submit, hook.data) diff --git a/mitmproxy/proxy2/commands.py b/mitmproxy/proxy2/commands.py index d962354dc..d5c4727f5 100644 --- a/mitmproxy/proxy2/commands.py +++ b/mitmproxy/proxy2/commands.py @@ -81,8 +81,16 @@ class Hook(Command): class Log(Hook): + blocking = False + + # this is more of a hack at the moment. def __init__(self, *args, level="info"): - super().__init__("log", log.LogEntry(repr(args), level)) + if len(args) == 1: + args = args[0] + super().__init__("log", log.LogEntry(str(args), level)) + + def __repr__(self): + return f"Log: {self.data.msg}" TCommandGenerator = typing.Generator[Command, typing.Any, None] diff --git a/mitmproxy/proxy2/layer.py b/mitmproxy/proxy2/layer.py index 2d2b5eb2c..d4c8a3e4b 100644 --- a/mitmproxy/proxy2/layer.py +++ b/mitmproxy/proxy2/layer.py @@ -108,6 +108,9 @@ class NextLayer(Layer): self.events = [] self.layer = None + def __repr__(self): + return f"NextLayer:{repr(self.layer)}" + def _handle_event(self, event: mevents.Event): self.events.append(event) diff --git a/mitmproxy/proxy2/layers/http.py b/mitmproxy/proxy2/layers/http.py index 2d0972c69..1d46cc8b9 100644 --- a/mitmproxy/proxy2/layers/http.py +++ b/mitmproxy/proxy2/layers/http.py @@ -33,7 +33,7 @@ class HTTPLayer(Layer): def log_event(orig): def next_event(): e = orig() - print(e, file=sys.__stdout__) + yield commands.Log(str(e)) return e return next_event @@ -46,6 +46,8 @@ class HTTPLayer(Layer): @expect(events.Start, events.DataReceived, events.ConnectionClosed) def _handle_event(self, event: events.Event) -> commands.TCommandGenerator: + if isinstance(event, events.Start): + return if isinstance(event, events.DataReceived): if event.connection == self.context.client: self.client_conn.receive_data(event.data) @@ -57,7 +59,7 @@ class HTTPLayer(Layer): yield from self.state() def read_request_headers(self): - event = self.client_conn.next_event() + event = yield from self.client_conn.next_event() if event is h11.NEED_DATA: return elif isinstance(event, h11.Request): @@ -75,7 +77,7 @@ class HTTPLayer(Layer): def read_request_body(self): while True: - event = self.client_conn.next_event() + event = yield from self.client_conn.next_event() if event is h11.NEED_DATA: return elif isinstance(event, h11.Data): @@ -102,7 +104,7 @@ class HTTPLayer(Layer): self.state = self.read_response_headers def read_response_headers(self): - event = self.server_conn.next_event() + event = yield from self.server_conn.next_event() if event is h11.NEED_DATA: return elif isinstance(event, h11.Response): @@ -123,7 +125,7 @@ class HTTPLayer(Layer): def read_response_body(self): while True: - event = self.server_conn.next_event() + event = yield from self.server_conn.next_event() if event is h11.NEED_DATA: return elif isinstance(event, h11.Data): diff --git a/mitmproxy/proxy2/server.py b/mitmproxy/proxy2/server.py index d011d09da..8e99da5a4 100644 --- a/mitmproxy/proxy2/server.py +++ b/mitmproxy/proxy2/server.py @@ -73,7 +73,7 @@ class ConnectionHandler(metaclass=abc.ABCMeta): reader, writer = self.transports[connection] while True: try: - data = await reader.read(4096) + data = await reader.read(65535) except socket.error: data = b"" if data: @@ -129,7 +129,8 @@ class SimpleConnectionHandler(ConnectionHandler): """Simple handler that does not process any hooks.""" async def handle_hook(self, hook: commands.Hook) -> None: - self.server_event(events.HookReply(hook, None)) + if hook.blocking: + self.server_event(events.HookReply(hook, None)) if __name__ == "__main__":