From 28f04c93fd48a66b7303d39efebb677148208b92 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Thu, 27 Aug 2020 11:07:04 +0200 Subject: [PATCH] intercept: don't fail on requests taken by other addons --- mitmproxy/addons/intercept.py | 44 +++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/mitmproxy/addons/intercept.py b/mitmproxy/addons/intercept.py index 38e325c5d..e36eff260 100644 --- a/mitmproxy/addons/intercept.py +++ b/mitmproxy/addons/intercept.py @@ -1,20 +1,18 @@ import typing -from mitmproxy import flowfilter +from mitmproxy import flow, flowfilter from mitmproxy import exceptions from mitmproxy import ctx class Intercept: - def __init__(self): - self.filt = None + filt: typing.Optional[flowfilter.TFilter] = None def load(self, loader): loader.add_option( "intercept_active", bool, False, "Intercept toggle" ) - loader.add_option( "intercept", typing.Optional[str], None, "Intercept filter expression." @@ -22,25 +20,28 @@ class Intercept: def configure(self, updated): if "intercept" in updated: - if not ctx.options.intercept: + if ctx.options.intercept: + self.filt = flowfilter.parse(ctx.options.intercept) + if not self.filt: + raise exceptions.OptionsError(f"Invalid interception filter: {ctx.options.intercept}") + ctx.options.intercept_active = True + else: self.filt = None ctx.options.intercept_active = False - return - self.filt = flowfilter.parse(ctx.options.intercept) - if not self.filt: - raise exceptions.OptionsError( - "Invalid interception filter: %s" % ctx.options.intercept - ) - ctx.options.intercept_active = True - def process_flow(self, f): - if self.filt: - should_intercept = all([ - self.filt(f), - not f.is_replay == "request", - ]) - if should_intercept and ctx.options.intercept_active: - f.intercept() + def should_intercept(self, f: flow.Flow) -> bool: + return ( + ctx.options.intercept_active + and self.filt + and self.filt(f) + and not f.is_replay + ) + + def process_flow(self, f: flow.Flow) -> None: + if self.should_intercept(f): + if f.reply.state != "start": + return ctx.log.debug("Cannot intercept request that is already taken by another addon.") + f.intercept() # Handlers @@ -51,5 +52,4 @@ class Intercept: self.process_flow(f) def tcp_message(self, f): - if self.filt and ctx.options.intercept_active: - f.intercept() + self.process_flow(f)