From 74a0230788f1e3a1800d8125fca274cda402c769 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 24 Feb 2018 14:53:54 +1300 Subject: [PATCH] addon options: shift keepserving and onboarding options --- mitmproxy/addons/keepserving.py | 10 ++++++++ mitmproxy/addons/onboarding.py | 20 ++++++++++++++++ mitmproxy/options.py | 29 ----------------------- test/mitmproxy/addons/test_keepserving.py | 3 +-- test/mitmproxy/addons/test_onboarding.py | 18 +++++++------- test/mitmproxy/tservers.py | 4 ++-- 6 files changed, 43 insertions(+), 41 deletions(-) diff --git a/mitmproxy/addons/keepserving.py b/mitmproxy/addons/keepserving.py index 9c975a7b0..6413299d8 100644 --- a/mitmproxy/addons/keepserving.py +++ b/mitmproxy/addons/keepserving.py @@ -2,6 +2,16 @@ from mitmproxy import ctx class KeepServing: + def load(self, loader): + loader.add_option( + "keepserving", bool, False, + """ + Continue serving after client playback, server playback or file + read. This option is ignored by interactive tools, which always keep + serving. + """ + ) + def event_processing_complete(self): if not ctx.master.options.keepserving: ctx.master.shutdown() diff --git a/mitmproxy/addons/onboarding.py b/mitmproxy/addons/onboarding.py index 07536c347..900acb086 100644 --- a/mitmproxy/addons/onboarding.py +++ b/mitmproxy/addons/onboarding.py @@ -2,6 +2,9 @@ from mitmproxy.addons import wsgiapp from mitmproxy.addons.onboardingapp import app from mitmproxy import ctx +APP_HOST = "mitm.it" +APP_PORT = 80 + class Onboarding(wsgiapp.WSGIApp): name = "onboarding" @@ -9,6 +12,23 @@ class Onboarding(wsgiapp.WSGIApp): def __init__(self): super().__init__(app.Adapter(app.application), None, None) + def load(self, loader): + loader.add_option( + "onboarding", bool, True, + "Toggle the mitmproxy onboarding app." + ) + loader.add_option( + "onboarding_host", str, APP_HOST, + """ + Onboarding app domain. For transparent mode, use an IP when a DNS + entry for the app domain is not present. + """ + ) + loader.add_option( + "onboarding_port", int, APP_PORT, + "Port to serve the onboarding app from." + ) + def configure(self, updated): self.host = ctx.options.onboarding_host self.port = ctx.options.onboarding_port diff --git a/mitmproxy/options.py b/mitmproxy/options.py index ff13c385b..318458581 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -12,8 +12,6 @@ log_verbosity = [ "debug", ] -APP_HOST = "mitm.it" -APP_PORT = 80 CA_DIR = "~/.mitmproxy" LISTEN_PORT = 8080 @@ -69,10 +67,6 @@ class Options(optmanager.OptManager): view_filter = None # type: Optional[str] # FIXME: Options that should be uncomplicated to migrate to addons - keepserving = None # type: bool - onboarding = None # type: bool - onboarding_host = None # type: str - onboarding_port = None # type: int server_replay_refresh = None # type: bool replacements = None # type: Sequence[str] rfile = None # type: Optional[str] @@ -102,33 +96,10 @@ class Options(optmanager.OptManager): def __init__(self, **kwargs) -> None: super().__init__() - self.add_option( - "onboarding", bool, True, - "Toggle the mitmproxy onboarding app." - ) - self.add_option( - "onboarding_host", str, APP_HOST, - """ - Onboarding app domain. For transparent mode, use an IP when a DNS - entry for the app domain is not present. - """ - ) - self.add_option( - "onboarding_port", int, APP_PORT, - "Port to serve the onboarding app from." - ) self.add_option( "server_replay_kill_extra", bool, False, "Kill extra requests during replay." ) - self.add_option( - "keepserving", bool, False, - """ - Continue serving after client playback, server playback or file - read. This option is ignored by interactive tools, which always keep - serving. - """ - ) self.add_option( "server", bool, True, "Start a proxy server. Enabled by default." diff --git a/test/mitmproxy/addons/test_keepserving.py b/test/mitmproxy/addons/test_keepserving.py index 70f7e1e61..2869d0976 100644 --- a/test/mitmproxy/addons/test_keepserving.py +++ b/test/mitmproxy/addons/test_keepserving.py @@ -4,7 +4,6 @@ from mitmproxy.test import taddons def test_keepserving(): ks = keepserving.KeepServing() - - with taddons.context() as tctx: + with taddons.context(ks) as tctx: ks.event_processing_complete() assert tctx.master.should_exit.is_set() diff --git a/test/mitmproxy/addons/test_onboarding.py b/test/mitmproxy/addons/test_onboarding.py index 474e6c3cf..810ddef1d 100644 --- a/test/mitmproxy/addons/test_onboarding.py +++ b/test/mitmproxy/addons/test_onboarding.py @@ -2,7 +2,6 @@ import pytest from mitmproxy.addons import onboarding from mitmproxy.test import taddons -from mitmproxy import options from .. import tservers @@ -11,25 +10,28 @@ class TestApp(tservers.HTTPProxyTest): return [onboarding.Onboarding()] def test_basic(self): - with taddons.context() as tctx: - tctx.configure(self.addons()[0]) + ob = onboarding.Onboarding() + with taddons.context(ob) as tctx: + tctx.configure(ob) assert self.app("/").status_code == 200 @pytest.mark.parametrize("ext", ["pem", "p12"]) def test_cert(self, ext): - with taddons.context() as tctx: - tctx.configure(self.addons()[0]) + ob = onboarding.Onboarding() + with taddons.context(ob) as tctx: + tctx.configure(ob) resp = self.app("/cert/%s" % ext) assert resp.status_code == 200 assert resp.content @pytest.mark.parametrize("ext", ["pem", "p12"]) def test_head(self, ext): - with taddons.context() as tctx: - tctx.configure(self.addons()[0]) + ob = onboarding.Onboarding() + with taddons.context(ob) as tctx: + tctx.configure(ob) p = self.pathoc() with p.connect(): - resp = p.request("head:'http://%s/cert/%s'" % (options.APP_HOST, ext)) + resp = p.request("head:'http://%s/cert/%s'" % (tctx.options.onboarding_host, ext)) assert resp.status_code == 200 assert "Content-Length" in resp.headers assert not resp.content diff --git a/test/mitmproxy/tservers.py b/test/mitmproxy/tservers.py index dd5bb3276..4363931f7 100644 --- a/test/mitmproxy/tservers.py +++ b/test/mitmproxy/tservers.py @@ -222,12 +222,12 @@ class HTTPProxyTest(ProxyTestBase): p = pathod.pathoc.Pathoc( ("127.0.0.1", self.proxy.port), True, fp=None ) - with p.connect((options.APP_HOST, options.APP_PORT)): + with p.connect((self.master.options.onboarding_host, self.master.options.onbarding_port)): return p.request("get:'%s'" % page) else: p = self.pathoc() with p.connect(): - return p.request("get:'http://%s%s'" % (options.APP_HOST, page)) + return p.request("get:'http://%s%s'" % (self.master.options.onboarding_host, page)) class TransparentProxyTest(ProxyTestBase):