From 81a00f6f761b74722cd55ad05ae979d5a0db1ec3 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 30 Oct 2016 11:07:09 +1300 Subject: Replace interception in mitmproxy and mitmweb with addon Fixes #1674 --- mitmproxy/addons/intercept.py | 1 + mitmproxy/test/taddons.py | 6 +++--- mitmproxy/tools/console/master.py | 18 ++---------------- mitmproxy/tools/console/statusbar.py | 4 ++-- mitmproxy/tools/console/window.py | 4 ++-- mitmproxy/tools/web/master.py | 30 ++---------------------------- test/mitmproxy/addons/test_intercept.py | 2 ++ 7 files changed, 14 insertions(+), 51 deletions(-) diff --git a/mitmproxy/addons/intercept.py b/mitmproxy/addons/intercept.py index a016025f..0efc4337 100644 --- a/mitmproxy/addons/intercept.py +++ b/mitmproxy/addons/intercept.py @@ -11,6 +11,7 @@ class Intercept: if "intercept" in updated: if not opts.intercept: self.filt = None + return self.filt = flowfilter.parse(opts.intercept) if not self.filt: raise exceptions.OptionsError( diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index 819d58ab..270df207 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -6,9 +6,9 @@ from mitmproxy import events class context: """ - A helper context for testing addons. Its most important function is to - set up the ctx module so handlers can be called just like they would be - when running from within mitmproxy. + A context for testing addons, which sets up the mitmproxy.ctx module so + handlers can run as they would within mitmproxy. The context also + provides a number of helper methods for common testing scenarios. """ def __init__(self, master = None, options = None): self.options = options or mitmproxy.options.Options() diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index 1f48f350..69c60657 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -23,6 +23,7 @@ from mitmproxy import io from mitmproxy import flowfilter from mitmproxy import log from mitmproxy.addons import state +from mitmproxy.addons import intercept import mitmproxy.options from mitmproxy.tools.console import flowlist from mitmproxy.tools.console import flowview @@ -234,11 +235,6 @@ class ConsoleMaster(master.Master): self.options = self.options # type: Options self.options.errored.connect(self.options_error) - r = self.set_intercept(options.intercept) - if r: - print("Intercept error: {}".format(r), file=sys.stderr) - sys.exit(1) - if options.filter: self.set_view_filter(options.filter) @@ -256,7 +252,7 @@ class ConsoleMaster(master.Master): signals.push_view_state.connect(self.sig_push_view_state) signals.sig_add_log.connect(self.sig_add_log) self.addons.add(*addons.default_addons()) - self.addons.add(self.state) + self.addons.add(self.state, intercept.Intercept()) def __setattr__(self, name, value): self.__dict__[name] = value @@ -616,9 +612,6 @@ class ConsoleMaster(master.Master): signals.flowlist_change.send(self) return v - def set_intercept(self, txt): - return self.state.set_intercept(txt) - def change_default_display_mode(self, t): v = contentviews.get_by_shortcut(t) self.state.default_body_view = v @@ -659,13 +652,6 @@ class ConsoleMaster(master.Master): ) def process_flow(self, f): - should_intercept = ( - self.state.intercept and flowfilter.match(self.state.intercept, f) - and not f.request.is_replay - and f.reply.state == "handled" - ) - if should_intercept: - f.intercept(self) signals.flowlist_change.send(self) signals.flow_change.send(self, flow=f) diff --git a/mitmproxy/tools/console/statusbar.py b/mitmproxy/tools/console/statusbar.py index 68b92703..57ed86db 100644 --- a/mitmproxy/tools/console/statusbar.py +++ b/mitmproxy/tools/console/statusbar.py @@ -160,10 +160,10 @@ class StatusBar(urwid.WidgetWrap): r.append("[") r.append(("heading_key", "T")) r.append("CP:%d]" % len(self.master.options.tcp_hosts)) - if self.master.state.intercept_txt: + if self.master.options.intercept: r.append("[") r.append(("heading_key", "i")) - r.append(":%s]" % self.master.state.intercept_txt) + r.append(":%s]" % self.master.options.intercept) if self.master.state.filter_txt: r.append("[") r.append(("heading_key", "f")) diff --git a/mitmproxy/tools/console/window.py b/mitmproxy/tools/console/window.py index b20d2e33..a01c59bc 100644 --- a/mitmproxy/tools/console/window.py +++ b/mitmproxy/tools/console/window.py @@ -88,8 +88,8 @@ class Window(urwid.Frame): signals.status_prompt.send( self, prompt = "Intercept filter", - text = self.master.state.intercept_txt, - callback = self.master.set_intercept + text = self.master.options.intercept, + callback = self.master.options.setter("intercept") ) elif k == "o": self.master.view_options() diff --git a/mitmproxy/tools/web/master.py b/mitmproxy/tools/web/master.py index e95daf44..cf0bfd73 100644 --- a/mitmproxy/tools/web/master.py +++ b/mitmproxy/tools/web/master.py @@ -7,10 +7,9 @@ import tornado.ioloop from typing import Optional from mitmproxy import addons -from mitmproxy import controller from mitmproxy import exceptions -from mitmproxy import flowfilter from mitmproxy.addons import state +from mitmproxy.addons import intercept from mitmproxy import options from mitmproxy import master from mitmproxy.tools.web import app @@ -139,7 +138,7 @@ class WebMaster(master.Master): super().__init__(options, server) self.state = WebState() self.addons.add(*addons.default_addons()) - self.addons.add(self.state) + self.addons.add(self.state, intercept.Intercept()) self.app = app.Application( self, self.options.wdebug, self.options.wauthenticator ) @@ -179,31 +178,6 @@ class WebMaster(master.Master): except (Stop, KeyboardInterrupt): self.shutdown() - def _process_flow(self, f): - should_intercept = ( - self.state.intercept and flowfilter.match(self.state.intercept, f) - and not f.request.is_replay - and f.reply.state == "handled" - ) - if should_intercept: - f.intercept(self) - return f - - @controller.handler - def request(self, f): - super().request(f) - return self._process_flow(f) - - @controller.handler - def response(self, f): - super().response(f) - return self._process_flow(f) - - @controller.handler - def error(self, f): - super().error(f) - return self._process_flow(f) - def add_log(self, e, level="info"): super().add_log(e, level) return self.state.add_log(e, level) diff --git a/test/mitmproxy/addons/test_intercept.py b/test/mitmproxy/addons/test_intercept.py index f389ff8e..efdac374 100644 --- a/test/mitmproxy/addons/test_intercept.py +++ b/test/mitmproxy/addons/test_intercept.py @@ -24,6 +24,8 @@ def test_simple(): r, intercept="~~" ) + tctx.configure(r, intercept=None) + assert not r.filt tctx.configure(r, intercept="~s") -- cgit v1.2.3