From f5dc0aace180aea8ba5eadb84fd49c12e8d4af69 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 5 May 2018 09:34:22 +1200 Subject: [PATCH] Revamp dup_and_replay example - Exposes view.add as a command - Copes with cases where a view addon isn't present - Avoids infinite loop caused by replaying replays Fixes #3096 --- examples/complex/dup_and_replay.py | 14 ++++++++++---- mitmproxy/addons/view.py | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/complex/dup_and_replay.py b/examples/complex/dup_and_replay.py index 2baa1ea6d..adcebff37 100644 --- a/examples/complex/dup_and_replay.py +++ b/examples/complex/dup_and_replay.py @@ -2,7 +2,13 @@ from mitmproxy import ctx def request(flow): - f = flow.copy() - ctx.master.view.add(f) - f.request.path = "/changed" - ctx.master.replay_request(f, block=True) + # Avoid an infinite loop by not replaying already replayed requests + if flow.request.is_replay: + return + flow = flow.copy() + # Only interactive tools have a view. If we have one, add a duplicate entry + # for our flow. + if "view" in ctx.master.addons: + ctx.master.commands.call("view.add", [flow]) + flow.request.path = "/changed" + ctx.master.commands.call("replay.client", [flow]) diff --git a/mitmproxy/addons/view.py b/mitmproxy/addons/view.py index beaadcc6d..ae00e9d18 100644 --- a/mitmproxy/addons/view.py +++ b/mitmproxy/addons/view.py @@ -293,6 +293,7 @@ class View(collections.Sequence): self._refilter() self.sig_store_refresh.send(self) + @command.command("view.marked.toggle") def add(self, flows: typing.Sequence[mitmproxy.flow.Flow]) -> None: """ Adds a flow to the state. If the flow already exists, it is