diff options
author | Aldo Cortesi <aldo@corte.si> | 2017-04-26 11:01:27 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@corte.si> | 2017-04-26 11:01:27 +1200 |
commit | b72f1390937e9799f588fd9a1564056131fb1aa7 (patch) | |
tree | 2396e9f5c272e671e45e8e4c4abee30ad34b3448 | |
parent | f90b4c2ff0f3fd71350900c10dea2a67846e1bdb (diff) | |
download | mitmproxy-b72f1390937e9799f588fd9a1564056131fb1aa7.tar.gz mitmproxy-b72f1390937e9799f588fd9a1564056131fb1aa7.tar.bz2 mitmproxy-b72f1390937e9799f588fd9a1564056131fb1aa7.zip |
configure(options, updated) -> configure(updated)
Options are now available globally on ctx, so the first argument of configure
is redundant.
29 files changed, 99 insertions, 102 deletions
diff --git a/examples/simple/custom_option.py b/examples/simple/custom_option.py index c8bc98d4..5b6070dd 100644 --- a/examples/simple/custom_option.py +++ b/examples/simple/custom_option.py @@ -6,6 +6,6 @@ def load(l): l.add_option("custom", bool, False, "A custom option") -def configure(options, updated): +def configure(updated): if "custom" in updated: - ctx.log.info("custom option value: %s" % options.custom) + ctx.log.info("custom option value: %s" % ctx.options.custom) diff --git a/examples/simple/modify_body_inject_iframe.py b/examples/simple/modify_body_inject_iframe.py index d54468d2..dff72afa 100644 --- a/examples/simple/modify_body_inject_iframe.py +++ b/examples/simple/modify_body_inject_iframe.py @@ -1,26 +1,21 @@ # (this script works best with --anticache) from bs4 import BeautifulSoup +from mitmproxy import ctx class Injector: - def __init__(self): - self.iframe_url = None - def load(self, loader): loader.add_option( "iframe", str, "", "IFrame to inject" ) - def configure(self, options, updated): - self.iframe_url = options.iframe - def response(self, flow): - if self.iframe_url: + if ctx.options.iframe: html = BeautifulSoup(flow.response.content, "html.parser") if html.body: iframe = html.new_tag( "iframe", - src=self.iframe_url, + src=ctx.options.iframe, frameborder=0, height=0, width=0) diff --git a/mitmproxy/addonmanager.py b/mitmproxy/addonmanager.py index e763d914..13c90413 100644 --- a/mitmproxy/addonmanager.py +++ b/mitmproxy/addonmanager.py @@ -110,7 +110,7 @@ class AddonManager: master.options.changed.connect(self._configure_all) def _configure_all(self, options, updated): - self.trigger("configure", options, updated) + self.trigger("configure", updated) def clear(self): """ diff --git a/mitmproxy/addons/check_alpn.py b/mitmproxy/addons/check_alpn.py index cb3c87e3..193159b2 100644 --- a/mitmproxy/addons/check_alpn.py +++ b/mitmproxy/addons/check_alpn.py @@ -7,7 +7,7 @@ class CheckALPN: def __init__(self): self.failed = False - def configure(self, options, updated): + def configure(self, updated): self.failed = mitmproxy.ctx.master.options.http2 and not tcp.HAS_ALPN if self.failed: ctx.log.warn( diff --git a/mitmproxy/addons/check_ca.py b/mitmproxy/addons/check_ca.py index a83ab8e1..f786af5a 100644 --- a/mitmproxy/addons/check_ca.py +++ b/mitmproxy/addons/check_ca.py @@ -5,7 +5,7 @@ class CheckCA: def __init__(self): self.failed = False - def configure(self, options, updated): + def configure(self, updated): has_ca = ( mitmproxy.ctx.master.server and mitmproxy.ctx.master.server.config and diff --git a/mitmproxy/addons/clientplayback.py b/mitmproxy/addons/clientplayback.py index 3345e65a..acb77bb2 100644 --- a/mitmproxy/addons/clientplayback.py +++ b/mitmproxy/addons/clientplayback.py @@ -20,12 +20,12 @@ class ClientPlayback: def load(self, flows: typing.Sequence[flow.Flow]): self.flows = flows - def configure(self, options, updated): + def configure(self, updated): if "client_replay" in updated: - if options.client_replay: - ctx.log.info("Client Replay: {}".format(options.client_replay)) + if ctx.options.client_replay: + ctx.log.info("Client Replay: {}".format(ctx.options.client_replay)) try: - flows = io.read_flows_from_paths(options.client_replay) + flows = io.read_flows_from_paths(ctx.options.client_replay) except exceptions.FlowReadException as e: raise exceptions.OptionsError(str(e)) self.load(flows) diff --git a/mitmproxy/addons/core_option_validation.py b/mitmproxy/addons/core_option_validation.py index fd5f2788..baeee764 100644 --- a/mitmproxy/addons/core_option_validation.py +++ b/mitmproxy/addons/core_option_validation.py @@ -4,12 +4,14 @@ """ from mitmproxy import exceptions from mitmproxy import platform +from mitmproxy import ctx from mitmproxy.net import server_spec from mitmproxy.utils import human class CoreOptionValidation: - def configure(self, opts, updated): + def configure(self, updated): + opts = ctx.options if opts.add_upstream_certs_to_client_chain and not opts.upstream_cert: raise exceptions.OptionsError( "The no-upstream-cert and add-upstream-certs-to-client-chain " diff --git a/mitmproxy/addons/dumper.py b/mitmproxy/addons/dumper.py index 587bb29b..3c3e1c65 100644 --- a/mitmproxy/addons/dumper.py +++ b/mitmproxy/addons/dumper.py @@ -31,13 +31,13 @@ class Dumper: self.filter = None # type: flowfilter.TFilter self.outfp = outfile # type: typing.io.TextIO - def configure(self, options, updated): + def configure(self, updated): if "view_filter" in updated: - if options.view_filter: - self.filter = flowfilter.parse(options.view_filter) + if ctx.options.view_filter: + self.filter = flowfilter.parse(ctx.options.view_filter) if not self.filter: raise exceptions.OptionsError( - "Invalid filter expression: %s" % options.view_filter + "Invalid filter expression: %s" % ctx.options.view_filter ) else: self.filter = None diff --git a/mitmproxy/addons/intercept.py b/mitmproxy/addons/intercept.py index 4a3fe17e..ac8c4c88 100644 --- a/mitmproxy/addons/intercept.py +++ b/mitmproxy/addons/intercept.py @@ -1,20 +1,21 @@ from mitmproxy import flowfilter from mitmproxy import exceptions +from mitmproxy import ctx class Intercept: def __init__(self): self.filt = None - def configure(self, opts, updated): + def configure(self, updated): if "intercept" in updated: - if not opts.intercept: + if not ctx.options.intercept: self.filt = None return - self.filt = flowfilter.parse(opts.intercept) + self.filt = flowfilter.parse(ctx.options.intercept) if not self.filt: raise exceptions.OptionsError( - "Invalid interception filter: %s" % opts.intercept + "Invalid interception filter: %s" % ctx.options.intercept ) def process_flow(self, f): diff --git a/mitmproxy/addons/onboarding.py b/mitmproxy/addons/onboarding.py index e67beadd..07536c34 100644 --- a/mitmproxy/addons/onboarding.py +++ b/mitmproxy/addons/onboarding.py @@ -9,9 +9,9 @@ class Onboarding(wsgiapp.WSGIApp): def __init__(self): super().__init__(app.Adapter(app.application), None, None) - def configure(self, options, updated): - self.host = options.onboarding_host - self.port = options.onboarding_port + def configure(self, updated): + self.host = ctx.options.onboarding_host + self.port = ctx.options.onboarding_port def request(self, f): if ctx.options.onboarding: diff --git a/mitmproxy/addons/proxyauth.py b/mitmproxy/addons/proxyauth.py index 5fed266e..fecdcb84 100644 --- a/mitmproxy/addons/proxyauth.py +++ b/mitmproxy/addons/proxyauth.py @@ -113,16 +113,16 @@ class ProxyAuth: return False # Handlers - def configure(self, options, updated): + def configure(self, updated): if "proxyauth" in updated: self.nonanonymous = False self.singleuser = None self.htpasswd = None - if options.proxyauth: - if options.proxyauth == "any": + if ctx.options.proxyauth: + if ctx.options.proxyauth == "any": self.nonanonymous = True - elif options.proxyauth.startswith("@"): - p = options.proxyauth[1:] + elif ctx.options.proxyauth.startswith("@"): + p = ctx.options.proxyauth[1:] try: self.htpasswd = passlib.apache.HtpasswdFile(p) except (ValueError, OSError) as v: @@ -130,18 +130,18 @@ class ProxyAuth: "Could not open htpasswd file: %s" % p ) else: - parts = options.proxyauth.split(':') + parts = ctx.options.proxyauth.split(':') if len(parts) != 2: raise exceptions.OptionsError( "Invalid single-user auth specification." ) self.singleuser = parts if self.enabled(): - if options.mode == "transparent": + if ctx.options.mode == "transparent": raise exceptions.OptionsError( "Proxy Authentication not supported in transparent mode." ) - if options.mode == "socks5": + if ctx.options.mode == "socks5": raise exceptions.OptionsError( "Proxy Authentication not supported in SOCKS mode. " "https://github.com/mitmproxy/mitmproxy/issues/738" diff --git a/mitmproxy/addons/replace.py b/mitmproxy/addons/replace.py index d6c11ca4..054264fa 100644 --- a/mitmproxy/addons/replace.py +++ b/mitmproxy/addons/replace.py @@ -47,7 +47,7 @@ class Replace: def __init__(self): self.lst = [] - def configure(self, options, updated): + def configure(self, updated): """ .replacements is a list of tuples (fpat, rex, s): @@ -57,7 +57,7 @@ class Replace: """ if "replacements" in updated: lst = [] - for rep in options.replacements: + for rep in ctx.options.replacements: fpatt, rex, s = parse_hook(rep) flt = flowfilter.parse(fpatt) diff --git a/mitmproxy/addons/script.py b/mitmproxy/addons/script.py index 0cca3eac..0497af85 100644 --- a/mitmproxy/addons/script.py +++ b/mitmproxy/addons/script.py @@ -59,14 +59,12 @@ class Script: ctx.master.addons.invoke_addon( self.ns, "configure", - ctx.options, ctx.options.keys() ) self.last_load = time.time() self.last_mtime = mtime - class ScriptLoader: """ An addon that manages loading scripts from options. @@ -82,14 +80,14 @@ class ScriptLoader: # Returning once we have proper commands raise NotImplementedError - def configure(self, options, updated): + def configure(self, updated): if "scripts" in updated: - for s in options.scripts: - if options.scripts.count(s) > 1: + for s in ctx.options.scripts: + if ctx.options.scripts.count(s) > 1: raise exceptions.OptionsError("Duplicate script: %s" % s) for a in self.addons[:]: - if a.path not in options.scripts: + if a.path not in ctx.options.scripts: ctx.log.info("Un-loading script: %s" % a.name) ctx.master.addons.remove(a) self.addons.remove(a) @@ -106,7 +104,7 @@ class ScriptLoader: ordered = [] newscripts = [] - for s in options.scripts: + for s in ctx.options.scripts: if s in current: ordered.append(current[s]) else: diff --git a/mitmproxy/addons/serverplayback.py b/mitmproxy/addons/serverplayback.py index 7d2aa15f..2255aaf2 100644 --- a/mitmproxy/addons/serverplayback.py +++ b/mitmproxy/addons/serverplayback.py @@ -89,12 +89,12 @@ class ServerPlayback: del self.flowmap[hsh] return ret - def configure(self, options, updated): + def configure(self, updated): if "server_replay" in updated: self.clear() - if options.server_replay: + if ctx.options.server_replay: try: - flows = io.read_flows_from_paths(options.server_replay) + flows = io.read_flows_from_paths(ctx.options.server_replay) except exceptions.FlowReadException as e: raise exceptions.OptionsError(str(e)) self.load_flows(flows) diff --git a/mitmproxy/addons/setheaders.py b/mitmproxy/addons/setheaders.py index 9e60eabd..d4d16e40 100644 --- a/mitmproxy/addons/setheaders.py +++ b/mitmproxy/addons/setheaders.py @@ -1,5 +1,6 @@ from mitmproxy import exceptions from mitmproxy import flowfilter +from mitmproxy import ctx def parse_setheader(s): @@ -43,17 +44,10 @@ class SetHeaders: def __init__(self): self.lst = [] - def configure(self, options, updated): - """ - options.setheaders is a tuple of (fpatt, header, value) - - fpatt: String specifying a filter pattern. - header: Header name. - value: Header value string - """ + def configure(self, updated): if "setheaders" in updated: self.lst = [] - for shead in options.setheaders: + for shead in ctx.options.setheaders: fpatt, header, value = parse_setheader(shead) flt = flowfilter.parse(fpatt) diff --git a/mitmproxy/addons/stickyauth.py b/mitmproxy/addons/stickyauth.py index 1a1d4fc4..24831d5b 100644 --- a/mitmproxy/addons/stickyauth.py +++ b/mitmproxy/addons/stickyauth.py @@ -1,5 +1,6 @@ from mitmproxy import exceptions from mitmproxy import flowfilter +from mitmproxy import ctx class StickyAuth: @@ -7,13 +8,13 @@ class StickyAuth: self.flt = None self.hosts = {} - def configure(self, options, updated): + def configure(self, updated): if "stickyauth" in updated: - if options.stickyauth: - flt = flowfilter.parse(options.stickyauth) + if ctx.options.stickyauth: + flt = flowfilter.parse(ctx.options.stickyauth) if not flt: raise exceptions.OptionsError( - "stickyauth: invalid filter expression: %s" % options.stickyauth + "stickyauth: invalid filter expression: %s" % ctx.options.stickyauth ) self.flt = flt else: diff --git a/mitmproxy/addons/stickycookie.py b/mitmproxy/addons/stickycookie.py index fb1c5072..04d99975 100644 --- a/mitmproxy/addons/stickycookie.py +++ b/mitmproxy/addons/stickycookie.py @@ -5,6 +5,7 @@ from mitmproxy.net.http import cookies from mitmproxy import exceptions from mitmproxy import flowfilter +from mitmproxy import ctx def ckey(attrs, f): @@ -33,13 +34,13 @@ class StickyCookie: self.jar = collections.defaultdict(dict) self.flt = None - def configure(self, options, updated): + def configure(self, updated): if "stickycookie" in updated: - if options.stickycookie: - flt = flowfilter.parse(options.stickycookie) + if ctx.options.stickycookie: + flt = flowfilter.parse(ctx.options.stickycookie) if not flt: raise exceptions.OptionsError( - "stickycookie: invalid filter expression: %s" % options.stickycookie + "stickycookie: invalid filter expression: %s" % ctx.options.stickycookie ) self.flt = flt else: diff --git a/mitmproxy/addons/streambodies.py b/mitmproxy/addons/streambodies.py index a10bdb93..181f0337 100644 --- a/mitmproxy/addons/streambodies.py +++ b/mitmproxy/addons/streambodies.py @@ -8,10 +8,10 @@ class StreamBodies: def __init__(self): self.max_size = None - def configure(self, options, updated): - if "stream_large_bodies" in updated and options.stream_large_bodies: + def configure(self, updated): + if "stream_large_bodies" in updated and ctx.options.stream_large_bodies: try: - self.max_size = human.parse_size(options.stream_large_bodies) + self.max_size = human.parse_size(ctx.options.stream_large_bodies) except ValueError as e: raise exceptions.OptionsError(e) diff --git a/mitmproxy/addons/streamfile.py b/mitmproxy/addons/streamfile.py index 183d2036..fde5a1c5 100644 --- a/mitmproxy/addons/streamfile.py +++ b/mitmproxy/addons/streamfile.py @@ -3,6 +3,7 @@ import os.path from mitmproxy import exceptions from mitmproxy import flowfilter from mitmproxy import io +from mitmproxy import ctx class StreamFile: @@ -20,26 +21,26 @@ class StreamFile: self.stream = io.FilteredFlowWriter(f, flt) self.active_flows = set() - def configure(self, options, updated): + def configure(self, updated): # We're already streaming - stop the previous stream and restart if "streamfile_filter" in updated: - if options.streamfile_filter: - self.filt = flowfilter.parse(options.streamfile_filter) + if ctx.options.streamfile_filter: + self.filt = flowfilter.parse(ctx.options.streamfile_filter) if not self.filt: raise exceptions.OptionsError( - "Invalid filter specification: %s" % options.streamfile_filter + "Invalid filter specification: %s" % ctx.options.streamfile_filter ) else: self.filt = None if "streamfile" in updated: if self.stream: self.done() - if options.streamfile: - if options.streamfile.startswith("+"): - path = options.streamfile[1:] + if ctx.options.streamfile: + if ctx.options.streamfile.startswith("+"): + path = ctx.options.streamfile[1:] mode = "ab" else: - path = options.streamfile + path = ctx.options.streamfile mode = "wb" self.start_stream_to_path(path, mode, self.filt) diff --git a/mitmproxy/addons/upstream_auth.py b/mitmproxy/addons/upstream_auth.py index 0003d2fd..685494c2 100644 --- a/mitmproxy/addons/upstream_auth.py +++ b/mitmproxy/addons/upstream_auth.py @@ -28,16 +28,16 @@ class UpstreamAuth(): def __init__(self): self.auth = None - def configure(self, options, updated): + def configure(self, updated): # FIXME: We're doing this because our proxy core is terminally confused # at the moment. Ideally, we should be able to check if we're in # reverse proxy mode at the HTTP layer, so that scripts can put the # proxy in reverse proxy mode for specific reuests. if "upstream_auth" in updated: - if options.upstream_auth is None: + if ctx.options.upstream_auth is None: self.auth = None else: - self.auth = parse_upstream_auth(options.upstream_auth) + self.auth = parse_upstream_auth(ctx.options.upstream_auth) def http_connect(self, f): if self.auth and f.mode == "upstream": diff --git a/mitmproxy/addons/view.py b/mitmproxy/addons/view.py index 7e9d66a1..341958c2 100644 --- a/mitmproxy/addons/view.py +++ b/mitmproxy/addons/view.py @@ -18,6 +18,7 @@ import sortedcontainers import mitmproxy.flow from mitmproxy import flowfilter from mitmproxy import exceptions +from mitmproxy import ctx from mitmproxy import http # noqa # The underlying sorted list implementation expects the sort key to be stable @@ -302,26 +303,26 @@ class View(collections.Sequence): return self._store.get(flow_id) # Event handlers - def configure(self, opts, updated): + def configure(self, updated): if "view_filter" in updated: filt = None - if opts.view_filter: - filt = flowfilter.parse(opts.view_filter) + if ctx.options.view_filter: + filt = flowfilter.parse(ctx.options.view_filter) if not filt: raise exceptions.OptionsError( - "Invalid interception filter: %s" % opts.view_filter + "Invalid interception filter: %s" % ctx.options.view_filter ) self.set_filter(filt) if "console_order" in updated: - if opts.console_order not in self.orders: + if ctx.options.console_order not in self.orders: raise exceptions.OptionsError( - "Unknown flow order: %s" % opts.console_order + "Unknown flow order: %s" % ctx.options.console_order ) - self.set_order(self.orders[opts.console_order]) + self.set_order(self.orders[ctx.options.console_order]) if "console_order_reversed" in updated: - self.set_reversed(opts.console_order_reversed) + self.set_reversed(ctx.options.console_order_reversed) if "console_focus_follow" in updated: - self.focus_follow = opts.console_focus_follow + self.focus_follow = ctx.options.console_focus_follow def request(self, f): self.add(f) diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index 471c9c31..39ebb2e6 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -107,7 +107,6 @@ class context: self.master.addons.invoke_addon( addon, "configure", - self.options, kwargs.keys() ) diff --git a/mitmproxy/tools/main.py b/mitmproxy/tools/main.py index de9032c8..c0326739 100644 --- a/mitmproxy/tools/main.py +++ b/mitmproxy/tools/main.py @@ -76,7 +76,7 @@ def run(MasterKlass, args, extra=None): # pragma: no cover unknown = optmanager.load_paths(opts, args.conf) server = process_options(parser, opts, args) master = MasterKlass(opts, server) - master.addons.trigger("configure", opts, opts.keys()) + master.addons.trigger("configure", opts.keys()) remaining = opts.update_known(**unknown) if remaining and opts.verbosity > 1: print("Ignored options: %s" % remaining) diff --git a/test/mitmproxy/addons/test_onboarding.py b/test/mitmproxy/addons/test_onboarding.py index 63125c23..42a3b574 100644 --- a/test/mitmproxy/addons/test_onboarding.py +++ b/test/mitmproxy/addons/test_onboarding.py @@ -1,4 +1,5 @@ from mitmproxy.addons import onboarding +from mitmproxy.test import taddons from .. import tservers @@ -7,10 +8,14 @@ class TestApp(tservers.HTTPProxyTest): return [onboarding.Onboarding()] def test_basic(self): - assert self.app("/").status_code == 200 + with taddons.context() as tctx: + tctx.configure(self.addons()[0]) + assert self.app("/").status_code == 200 def test_cert(self): - for ext in ["pem", "p12"]: - resp = self.app("/cert/%s" % ext) - assert resp.status_code == 200 - assert resp.content + with taddons.context() as tctx: + tctx.configure(self.addons()[0]) + for ext in ["pem", "p12"]: + resp = self.app("/cert/%s" % ext) + assert resp.status_code == 200 + assert resp.content diff --git a/test/mitmproxy/addons/test_serverplayback.py b/test/mitmproxy/addons/test_serverplayback.py index c92f80b8..29de48a0 100644 --- a/test/mitmproxy/addons/test_serverplayback.py +++ b/test/mitmproxy/addons/test_serverplayback.py @@ -6,7 +6,6 @@ from mitmproxy.test import tflow import mitmproxy.test.tutils from mitmproxy.addons import serverplayback -from mitmproxy import options from mitmproxy import exceptions from mitmproxy import io diff --git a/test/mitmproxy/data/addonscripts/addon.py b/test/mitmproxy/data/addonscripts/addon.py index 8bd25808..8c834d82 100644 --- a/test/mitmproxy/data/addonscripts/addon.py +++ b/test/mitmproxy/data/addonscripts/addon.py @@ -9,11 +9,11 @@ class Addon: def load(self, opts): event_log.append("addonload") - def configure(self, options, updated): + def configure(self, updated): event_log.append("addonconfigure") -def configure(options, updated): +def configure(updated): event_log.append("scriptconfigure") diff --git a/test/mitmproxy/proxy/test_server.py b/test/mitmproxy/proxy/test_server.py index b54a764f..b4bb46bb 100644 --- a/test/mitmproxy/proxy/test_server.py +++ b/test/mitmproxy/proxy/test_server.py @@ -297,7 +297,7 @@ class TestHTTPAuth(tservers.HTTPProxyTest): def test_auth(self): self.master.addons.add(proxyauth.ProxyAuth()) self.master.addons.trigger( - "configure", self.master.options, self.master.options.keys() + "configure", self.master.options.keys() ) self.master.options.proxyauth = "test:test" assert self.pathod("202").status_code == 407 diff --git a/test/mitmproxy/tools/console/test_master.py b/test/mitmproxy/tools/console/test_master.py index add8c4d3..c87c9e83 100644 --- a/test/mitmproxy/tools/console/test_master.py +++ b/test/mitmproxy/tools/console/test_master.py @@ -30,7 +30,7 @@ class TestMaster(tservers.MasterTest): opts["verbosity"] = 1 o = options.Options(**opts) m = console.master.ConsoleMaster(o, proxy.DummyServer()) - m.addons.trigger("configure", o, o.keys()) + m.addons.trigger("configure", o.keys()) return m def test_basic(self): diff --git a/test/mitmproxy/tservers.py b/test/mitmproxy/tservers.py index d8dda5eb..b8005529 100644 --- a/test/mitmproxy/tservers.py +++ b/test/mitmproxy/tservers.py @@ -74,7 +74,7 @@ class TestMaster(taddons.RecordingMaster): self.state = TestState() self.addons.add(self.state) self.addons.add(*addons) - self.addons.trigger("configure", self.options, self.options.keys()) + self.addons.trigger("configure", self.options.keys()) self.addons.trigger("running") def reset(self, addons): |