From 2aa7ac584b7884b8fd7622aec86b47d1e33a4a11 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 24 Feb 2018 15:49:38 +1300 Subject: addon options: stickycookie, streambodies --- mitmproxy/addons/stickycookie.py | 6 ++++++ mitmproxy/addons/streambodies.py | 19 +++++++++++++++++++ mitmproxy/options.py | 23 ----------------------- test/mitmproxy/addons/test_stickycookie.py | 16 ++++++++-------- test/mitmproxy/addons/test_streambodies.py | 2 +- 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/mitmproxy/addons/stickycookie.py b/mitmproxy/addons/stickycookie.py index e58e0a58..e5f85fa1 100644 --- a/mitmproxy/addons/stickycookie.py +++ b/mitmproxy/addons/stickycookie.py @@ -34,6 +34,12 @@ class StickyCookie: self.jar = collections.defaultdict(dict) # type: Dict[TOrigin, Dict[str, str]] self.flt = None # type: Optional[flowfilter.TFilter] + def load(self, loader): + loader.add_option( + "stickycookie", Optional[str], None, + "Set sticky cookie filter. Matched against requests." + ) + def configure(self, updated): if "stickycookie" in updated: if ctx.options.stickycookie: diff --git a/mitmproxy/addons/streambodies.py b/mitmproxy/addons/streambodies.py index c841075f..6ca9918f 100644 --- a/mitmproxy/addons/streambodies.py +++ b/mitmproxy/addons/streambodies.py @@ -1,3 +1,5 @@ +import typing + from mitmproxy.net.http import http1 from mitmproxy import exceptions from mitmproxy import ctx @@ -8,6 +10,23 @@ class StreamBodies: def __init__(self): self.max_size = None + def load(self, loader): + loader.add_option( + "stream_large_bodies", typing.Optional[str], None, + """ + Stream data to the client if response body exceeds the given + threshold. If streamed, the body will not be stored in any way. + Understands k/m/g suffixes, i.e. 3m for 3 megabytes. + """ + ) + loader.add_option( + "stream_websockets", bool, False, + """ + Stream WebSocket messages between client and server. + Messages are captured and cannot be modified. + """ + ) + def configure(self, updated): if "stream_large_bodies" in updated and ctx.options.stream_large_bodies: try: diff --git a/mitmproxy/options.py b/mitmproxy/options.py index bf2012d7..ed17489a 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -67,10 +67,6 @@ class Options(optmanager.OptManager): view_filter = None # type: Optional[str] # FIXME: Options that should be uncomplicated to migrate to addons - stickyauth = None # type: Optional[str] - stickycookie = None # type: Optional[str] - stream_large_bodies = None # type: Optional[str] - stream_websockets = None # type: bool upstream_auth = None # type: Optional[str] view_order = None # type: str view_order_reversed = None # type: bool @@ -89,25 +85,6 @@ class Options(optmanager.OptManager): "showhost", bool, False, "Use the Host header to construct URLs for display." ) - self.add_option( - "stickycookie", Optional[str], None, - "Set sticky cookie filter. Matched against requests." - ) - self.add_option( - "stream_large_bodies", Optional[str], None, - """ - Stream data to the client if response body exceeds the given - threshold. If streamed, the body will not be stored in any way. - Understands k/m/g suffixes, i.e. 3m for 3 megabytes. - """ - ) - self.add_option( - "stream_websockets", bool, False, - """ - Stream WebSocket messages between client and server. - Messages are captured and cannot be modified. - """ - ) self.add_option( "verbosity", str, 'info', "Log verbosity.", diff --git a/test/mitmproxy/addons/test_stickycookie.py b/test/mitmproxy/addons/test_stickycookie.py index f77d019d..4493e9cc 100644 --- a/test/mitmproxy/addons/test_stickycookie.py +++ b/test/mitmproxy/addons/test_stickycookie.py @@ -15,7 +15,7 @@ def test_domain_match(): class TestStickyCookie: def test_config(self): sc = stickycookie.StickyCookie() - with taddons.context() as tctx: + with taddons.context(sc) as tctx: with pytest.raises(Exception, match="invalid filter"): tctx.configure(sc, stickycookie="~b") @@ -26,7 +26,7 @@ class TestStickyCookie: def test_simple(self): sc = stickycookie.StickyCookie() - with taddons.context() as tctx: + with taddons.context(sc) as tctx: tctx.configure(sc, stickycookie=".*") f = tflow.tflow(resp=True) f.response.headers["set-cookie"] = "foo=bar" @@ -50,7 +50,7 @@ class TestStickyCookie: def test_response(self): sc = stickycookie.StickyCookie() - with taddons.context() as tctx: + with taddons.context(sc) as tctx: tctx.configure(sc, stickycookie=".*") c = "SSID=mooo; domain=.google.com, FOO=bar; Domain=.google.com; Path=/; " \ @@ -68,7 +68,7 @@ class TestStickyCookie: def test_response_multiple(self): sc = stickycookie.StickyCookie() - with taddons.context() as tctx: + with taddons.context(sc) as tctx: tctx.configure(sc, stickycookie=".*") # Test setting of multiple cookies @@ -82,7 +82,7 @@ class TestStickyCookie: def test_response_weird(self): sc = stickycookie.StickyCookie() - with taddons.context() as tctx: + with taddons.context(sc) as tctx: tctx.configure(sc, stickycookie=".*") # Test setting of weird cookie keys @@ -100,7 +100,7 @@ class TestStickyCookie: def test_response_overwrite(self): sc = stickycookie.StickyCookie() - with taddons.context() as tctx: + with taddons.context(sc) as tctx: tctx.configure(sc, stickycookie=".*") # Test overwriting of a cookie value @@ -115,7 +115,7 @@ class TestStickyCookie: def test_response_delete(self): sc = stickycookie.StickyCookie() - with taddons.context() as tctx: + with taddons.context(sc) as tctx: tctx.configure(sc, stickycookie=".*") # Test that a cookie is be deleted @@ -127,7 +127,7 @@ class TestStickyCookie: def test_request(self): sc = stickycookie.StickyCookie() - with taddons.context() as tctx: + with taddons.context(sc) as tctx: tctx.configure(sc, stickycookie=".*") f = self._response(sc, "SSID=mooo", "www.google.com") diff --git a/test/mitmproxy/addons/test_streambodies.py b/test/mitmproxy/addons/test_streambodies.py index 426ec9ae..b980ea0b 100644 --- a/test/mitmproxy/addons/test_streambodies.py +++ b/test/mitmproxy/addons/test_streambodies.py @@ -7,7 +7,7 @@ import pytest def test_simple(): sa = streambodies.StreamBodies() - with taddons.context() as tctx: + with taddons.context(sa) as tctx: with pytest.raises(exceptions.OptionsError): tctx.configure(sa, stream_large_bodies = "invalid") tctx.configure(sa, stream_large_bodies = "10") -- cgit v1.2.3