def cycle(self, master, content): f = tflow.tflow(req=tutils.treq(content=content)) master.addons.handle_lifecycle("clientconnect", f.client_conn) for i in eventsequence.iterate(f): master.addons.handle_lifecycle(*i) master.addons.handle_lifecycle("clientdisconnect", f.client_conn) return f
async def load_flow(self, f): """ Loads a flow and links websocket & handshake flows """ if isinstance(f, http.HTTPFlow): self._change_reverse_host(f) if 'websocket' in f.metadata: self.waiting_flows.append(f) if isinstance(f, websocket.WebSocketFlow): hfs = [ hf for hf in self.waiting_flows if hf.id == f.metadata['websocket_handshake'] ] if hfs: hf = hfs[0] f.handshake_flow = hf self.waiting_flows.remove(hf) self._change_reverse_host(f.handshake_flow) else: # this will fail - but at least it will load the remaining flows f.handshake_flow = http.HTTPFlow(None, None) f.reply = controller.DummyReply() for e, o in eventsequence.iterate(f): await self.addons.handle_lifecycle(e, o)
async def cycle(self, master, content): f = tflow.tflow(req=tutils.treq(content=content)) layer = mock.Mock("mitmproxy.proxy.protocol.base.Layer") layer.client_conn = f.client_conn layer.reply = controller.DummyReply() await master.addons.handle_lifecycle("clientconnect", layer) for i in eventsequence.iterate(f): await master.addons.handle_lifecycle(*i) await master.addons.handle_lifecycle("clientdisconnect", layer) return f
def cycle(self, addon, f): """ Cycles the flow through the events for the flow. Stops if a reply is taken (as in flow interception). """ f.reply._state = "start" for evt, arg in eventsequence.iterate(f): self.master.addons.invoke_addon(addon, evt, arg) if f.reply.state == "taken": return
def test_http_flow(resp, err): f = tflow.tflow(resp=resp, err=err) i = eventsequence.iterate(f) assert next(i) == ("requestheaders", f) assert next(i) == ("request", f) if resp: assert next(i) == ("responseheaders", f) assert next(i) == ("response", f) if err: assert next(i) == ("error", f)
def cycle(self, master, content): f = tflow.tflow(req=tutils.treq(content=content)) layer = mock.Mock("mitmproxy.proxy.protocol.base.Layer") layer.client_conn = f.client_conn layer.reply = controller.DummyReply() master.addons.handle_lifecycle("clientconnect", layer) for i in eventsequence.iterate(f): master.addons.handle_lifecycle(*i) master.addons.handle_lifecycle("clientdisconnect", layer) return f
def test_http_flow(resp, err): f = tflow.tflow(resp=resp, err=err) i = eventsequence.iterate(f) assert isinstance(next(i), layers.http.HttpRequestHeadersHook) assert isinstance(next(i), layers.http.HttpRequestHook) if resp: assert isinstance(next(i), layers.http.HttpResponseHeadersHook) assert isinstance(next(i), layers.http.HttpResponseHook) if err: assert isinstance(next(i), layers.http.HttpErrorHook)
def run_once(self, command, flows): try: sc = Script(command) except ValueError as e: raise ValueError(str(e)) sc.load_script() for f in flows: for evt, o in eventsequence.iterate(f): sc.run(evt, o) sc.done() return sc
async def load_flow(self, f): """ Loads a flow """ if isinstance(f, http.HTTPFlow): self._change_reverse_host(f) f.reply = controller.DummyReply() for e in eventsequence.iterate(f): await self.addons.handle_lifecycle(e)
async def cycle(self, master, content): f = tflow.tflow(req=tutils.treq(content=content)) layer = mock.Mock("mitmproxy.proxy.protocol.base.Layer") layer.client_conn = f.client_conn layer.reply = controller.DummyReply() await master.addons.handle_lifecycle( server_hooks.ClientConnectedHook(layer)) for e in eventsequence.iterate(f): await master.addons.handle_lifecycle(e) await master.addons.handle_lifecycle( server_hooks.ClientDisconnectedHook(layer)) return f
def test_tcp_flow(err): f = tflow.ttcpflow(err=err) i = eventsequence.iterate(f) assert next(i) == ("tcp_start", f) assert len(f.messages) == 0 assert next(i) == ("tcp_message", f) assert len(f.messages) == 1 assert next(i) == ("tcp_message", f) assert len(f.messages) == 2 if err: assert next(i) == ("tcp_error", f) assert next(i) == ("tcp_end", f)
def load_flow(self, f): """ Loads a flow """ if isinstance(f, http.HTTPFlow): if self.server and self.options.mode == "reverse": f.request.host = self.server.config.upstream_server.address[0] f.request.port = self.server.config.upstream_server.address[1] f.request.scheme = self.server.config.upstream_server.scheme f.reply = controller.DummyReply() for e, o in eventsequence.iterate(f): getattr(self, e)(o)
async def cycle(self, addon, f): """ Cycles the flow through the events for the flow. Stops if the flow is intercepted. """ for evt in eventsequence.iterate(f): await self.master.addons.invoke_addon( addon, evt ) if f.intercepted: return
def load_flow(self, f): """ Loads a flow """ if isinstance(f, http.HTTPFlow): if self.server and self.options.mode.startswith("reverse:"): f.request.host = self.server.config.upstream_server.address[0] f.request.port = self.server.config.upstream_server.address[1] f.request.scheme = self.server.config.upstream_server.scheme f.reply = controller.DummyReply() for e, o in eventsequence.iterate(f): self.addons.handle_lifecycle(e, o)
def cycle(self, addon, f): """ Cycles the flow through the events for the flow. Stops if a reply is taken (as in flow interception). """ f.reply._state = "handled" for evt, arg in eventsequence.iterate(f): h = getattr(addon, evt, None) if h: h(arg) if f.reply.state == "taken": return
def cycle(self, addon, f): """ Cycles the flow through the events for the flow. Stops if a reply is taken (as in flow interception). """ f.reply._state = "start" for evt, arg in eventsequence.iterate(f): h = getattr(addon, evt, None) if h: h(arg) if f.reply.state == "taken": return
def test_websocket_flow(err): f = tflow.twebsocketflow(err=err) i = eventsequence.iterate(f) assert next(i) == ("websocket_start", f) assert len(f.messages) == 0 assert next(i) == ("websocket_message", f) assert len(f.messages) == 1 assert next(i) == ("websocket_message", f) assert len(f.messages) == 2 if err: assert next(i) == ("websocket_error", f) assert next(i) == ("websocket_end", f)
def load_flow(self, f): """ Loads a flow """ if isinstance(f, http.HTTPFlow): if self.options.mode.startswith("reverse:"): _, upstream_spec = server_spec.parse_with_mode(self.options.mode) f.request.host, f.request.port = upstream_spec.address f.request.scheme = upstream_spec.scheme f.reply = controller.DummyReply() for e, o in eventsequence.iterate(f): self.addons.handle_lifecycle(e, o)
def test_tcp_flow(err): f = tflow.ttcpflow(err=err) i = eventsequence.iterate(f) assert isinstance(next(i), layers.tcp.TcpStartHook) assert len(f.messages) == 0 assert isinstance(next(i), layers.tcp.TcpMessageHook) assert len(f.messages) == 1 assert isinstance(next(i), layers.tcp.TcpMessageHook) assert len(f.messages) == 2 if err: assert isinstance(next(i), layers.tcp.TcpErrorHook) else: assert isinstance(next(i), layers.tcp.TcpEndHook)
def script_run(self, flows: typing.Sequence[flow.Flow], path: mtypes.Path) -> None: """ Run a script on the specified flows. The script is loaded with default options, and all lifecycle events for each flow are simulated. """ try: s = Script(path, False) for f in flows: for evt, arg in eventsequence.iterate(f): ctx.master.addons.invoke_addon(s, evt, arg) except exceptions.OptionsError as e: script_error_handler(path, e, msg=str(e))
def test_websocket_flow(err): f = tflow.twebsocketflow(err=err) i = eventsequence.iterate(f) assert next(i) == ("websocket_start", f) assert len(f.messages) == 0 assert next(i) == ("websocket_message", f) assert len(f.messages) == 1 assert next(i) == ("websocket_message", f) assert len(f.messages) == 2 assert next(i) == ("websocket_message", f) assert len(f.messages) == 3 if err: assert next(i) == ("websocket_error", f) assert next(i) == ("websocket_end", f)
def cycle(self, addon, f): """ Cycles the flow through the events for the flow. Stops if a reply is taken (as in flow interception). """ f.reply._state = "start" for evt, arg in eventsequence.iterate(f): self.master.addons.invoke_addon( addon, evt, arg ) if f.reply.state == "taken": return
def test_websocket_flow(err): f = tflow.twebsocketflow(err=err) i = eventsequence.iterate(f) assert isinstance(next(i), layers.websocket.WebsocketStartHook) assert len(f.messages) == 0 assert isinstance(next(i), layers.websocket.WebsocketMessageHook) assert len(f.messages) == 1 assert isinstance(next(i), layers.websocket.WebsocketMessageHook) assert len(f.messages) == 2 assert isinstance(next(i), layers.websocket.WebsocketMessageHook) assert len(f.messages) == 3 if err: assert isinstance(next(i), layers.websocket.WebsocketErrorHook) else: assert isinstance(next(i), layers.websocket.WebsocketEndHook)
async def load_flow(self, f): """ Loads a flow """ if isinstance(f, http.HTTPFlow): if self.options.mode.startswith("reverse:"): # When we load flows in reverse proxy mode, we adjust the target host to # the reverse proxy destination for all flows we load. This makes it very # easy to replay saved flows against a different host. _, upstream_spec = server_spec.parse_with_mode(self.options.mode) f.request.host, f.request.port = upstream_spec.address f.request.scheme = upstream_spec.scheme for e in eventsequence.iterate(f): await self.addons.handle_lifecycle(e)
def script_run(self, flows: typing.Sequence[flow.Flow], path: str) -> None: """ Run a script on the specified flows. The script is loaded with default options, and all lifecycle events for each flow are simulated. """ try: s = Script(path) l = addonmanager.Loader(ctx.master) ctx.master.addons.invoke_addon(s, "load", l) ctx.master.addons.invoke_addon(s, "configure", ctx.options.keys()) # Script is loaded on the first tick ctx.master.addons.invoke_addon(s, "tick") for f in flows: for evt, arg in eventsequence.iterate(f): ctx.master.addons.invoke_addon(s, evt, arg) except exceptions.OptionsError as e: raise exceptions.CommandError("Error running script: %s" % e) from e
def test_websocket_flow(): f = tflow.twebsocketflow() i = eventsequence.iterate(f) assert isinstance(next(i), layers.http.HttpRequestHeadersHook) assert isinstance(next(i), layers.http.HttpRequestHook) assert isinstance(next(i), layers.http.HttpResponseHeadersHook) assert isinstance(next(i), layers.http.HttpResponseHook) assert isinstance(next(i), layers.websocket.WebsocketStartHook) assert len(f.websocket.messages) == 0 assert isinstance(next(i), layers.websocket.WebsocketMessageHook) assert len(f.websocket.messages) == 1 assert isinstance(next(i), layers.websocket.WebsocketMessageHook) assert len(f.websocket.messages) == 2 assert isinstance(next(i), layers.websocket.WebsocketMessageHook) assert len(f.websocket.messages) == 3 assert isinstance(next(i), layers.websocket.WebsocketEndHook)
def load_flow(self, f): """ Loads a flow and links websocket & handshake flows """ if isinstance(f, http.HTTPFlow): self._change_reverse_host(f) if 'websocket' in f.metadata: self.waiting_flows.append(f) if isinstance(f, websocket.WebSocketFlow): hf = [hf for hf in self.waiting_flows if hf.id == f.metadata['websocket_handshake']][0] f.handshake_flow = hf self.waiting_flows.remove(hf) self._change_reverse_host(f.handshake_flow) f.reply = controller.DummyReply() for e, o in eventsequence.iterate(f): self.addons.handle_lifecycle(e, o)
def script_run(self, flows: typing.Sequence[flow.Flow], path: mtypes.Path) -> None: """ Run a script on the specified flows. The script is configured with the current options and all lifecycle events for each flow are simulated. Note that the load event is not invoked. """ if not os.path.isfile(path): ctx.log.error('No such script: %s' % path) return mod = load_script(path) if mod: with addonmanager.safecall(): ctx.master.addons.invoke_addon(mod, "running") ctx.master.addons.invoke_addon(mod, "configure", ctx.options.keys()) for f in flows: for evt, arg in eventsequence.iterate(f): ctx.master.addons.invoke_addon(mod, evt, arg)
def script_run(self, flows: typing.Sequence[flow.Flow], path: mtypes.Path) -> None: """ Run a script on the specified flows. The script is configured with the current options and all lifecycle events for each flow are simulated. Note that the load event is not invoked. """ if not os.path.isfile(path): ctx.log.error('No such script: %s' % path) return mod = load_script(path) if mod: with addonmanager.safecall(): ctx.master.addons.invoke_addon(mod, "running") ctx.master.addons.invoke_addon( mod, "configure", ctx.options.keys() ) for f in flows: for evt, arg in eventsequence.iterate(f): ctx.master.addons.invoke_addon(mod, evt, arg)
def test_invalid(): with pytest.raises(TypeError): next(eventsequence.iterate(42))