configure(options, updated) -> configure(updated)

Options are now available globally on ctx, so the first argument of configure
is redundant.
This commit is contained in:
Aldo Cortesi 2017-04-26 11:01:27 +12:00
parent f90b4c2ff0
commit b72f139093
29 changed files with 99 additions and 102 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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):
"""

View File

@ -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(

View File

@ -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

View File

@ -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)

View File

@ -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 "

View File

@ -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

View File

@ -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):

View File

@ -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:

View File

@ -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"

View File

@ -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)

View File

@ -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:

View File

@ -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)

View File

@ -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)

View File

@ -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:

View File

@ -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:

View File

@ -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)

View File

@ -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)

View File

@ -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":

View File

@ -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)

View File

@ -107,7 +107,6 @@ class context:
self.master.addons.invoke_addon(
addon,
"configure",
self.options,
kwargs.keys()
)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -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):

View File

@ -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):