diff options
author | Aldo Cortesi <aldo@corte.si> | 2016-07-23 16:08:27 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-23 16:08:27 +1200 |
commit | e85566e4ee5834e04e191763c787389cb53f3296 (patch) | |
tree | 3e51031018d35bfcaf8b9f63e4c2d15bbec51263 /mitmproxy | |
parent | 45d24696e0ae15c67e46d6cfe4a06ae52ce19888 (diff) | |
parent | afda175e1ccebf072391432ad74d6118c7cbb407 (diff) | |
download | mitmproxy-e85566e4ee5834e04e191763c787389cb53f3296.tar.gz mitmproxy-e85566e4ee5834e04e191763c787389cb53f3296.tar.bz2 mitmproxy-e85566e4ee5834e04e191763c787389cb53f3296.zip |
Merge pull request #1412 from cortesi/updated
Addon iface: .configure(options) -> .configure(options, updated)
Diffstat (limited to 'mitmproxy')
-rw-r--r-- | mitmproxy/addons.py | 15 | ||||
-rw-r--r-- | mitmproxy/builtins/anticache.py | 2 | ||||
-rw-r--r-- | mitmproxy/builtins/anticomp.py | 2 | ||||
-rw-r--r-- | mitmproxy/builtins/dumper.py | 2 | ||||
-rw-r--r-- | mitmproxy/builtins/filestreamer.py | 2 | ||||
-rw-r--r-- | mitmproxy/builtins/replace.py | 2 | ||||
-rw-r--r-- | mitmproxy/builtins/script.py | 56 | ||||
-rw-r--r-- | mitmproxy/builtins/setheaders.py | 2 | ||||
-rw-r--r-- | mitmproxy/builtins/stickyauth.py | 2 | ||||
-rw-r--r-- | mitmproxy/builtins/stickycookie.py | 2 | ||||
-rw-r--r-- | mitmproxy/console/master.py | 2 | ||||
-rw-r--r-- | mitmproxy/dump.py | 4 | ||||
-rw-r--r-- | mitmproxy/optmanager.py | 18 | ||||
-rw-r--r-- | mitmproxy/proxy/config.py | 4 | ||||
-rw-r--r-- | mitmproxy/web/master.py | 2 |
15 files changed, 63 insertions, 54 deletions
diff --git a/mitmproxy/addons.py b/mitmproxy/addons.py index c779aaf8..a4bea9fa 100644 --- a/mitmproxy/addons.py +++ b/mitmproxy/addons.py @@ -13,16 +13,23 @@ class Addons(object): self.master = master master.options.changed.connect(self.options_update) - def options_update(self, options): + def options_update(self, options, updated): for i in self.chain: with self.master.handlecontext(): - i.configure(options) + i.configure(options, updated) - def add(self, *addons): + def add(self, options, *addons): + if not addons: + raise ValueError("No adons specified.") self.chain.extend(addons) for i in addons: self.invoke_with_context(i, "start") - self.invoke_with_context(i, "configure", self.master.options) + self.invoke_with_context( + i, + "configure", + self.master.options, + self.master.options.keys() + ) def remove(self, addon): self.chain = [i for i in self.chain if i is not addon] diff --git a/mitmproxy/builtins/anticache.py b/mitmproxy/builtins/anticache.py index f208e2fb..41a5ed95 100644 --- a/mitmproxy/builtins/anticache.py +++ b/mitmproxy/builtins/anticache.py @@ -5,7 +5,7 @@ class AntiCache: def __init__(self): self.enabled = False - def configure(self, options): + def configure(self, options, updated): self.enabled = options.anticache def request(self, flow): diff --git a/mitmproxy/builtins/anticomp.py b/mitmproxy/builtins/anticomp.py index 50bd1b73..823e960c 100644 --- a/mitmproxy/builtins/anticomp.py +++ b/mitmproxy/builtins/anticomp.py @@ -5,7 +5,7 @@ class AntiComp: def __init__(self): self.enabled = False - def configure(self, options): + def configure(self, options, updated): self.enabled = options.anticomp def request(self, flow): diff --git a/mitmproxy/builtins/dumper.py b/mitmproxy/builtins/dumper.py index 239630fb..34d3632e 100644 --- a/mitmproxy/builtins/dumper.py +++ b/mitmproxy/builtins/dumper.py @@ -217,7 +217,7 @@ class Dumper(): return True return False - def configure(self, options): + def configure(self, options, updated): if options.filtstr: self.filt = filt.parse(options.filtstr) if not self.filt: diff --git a/mitmproxy/builtins/filestreamer.py b/mitmproxy/builtins/filestreamer.py index 97ddc7c4..ffa565ac 100644 --- a/mitmproxy/builtins/filestreamer.py +++ b/mitmproxy/builtins/filestreamer.py @@ -19,7 +19,7 @@ class FileStreamer: self.stream = io.FilteredFlowWriter(f, filt) self.active_flows = set() - def configure(self, options): + def configure(self, options, updated): # We're already streaming - stop the previous stream and restart if self.stream: self.done() diff --git a/mitmproxy/builtins/replace.py b/mitmproxy/builtins/replace.py index 83b96cee..74d30c05 100644 --- a/mitmproxy/builtins/replace.py +++ b/mitmproxy/builtins/replace.py @@ -8,7 +8,7 @@ class Replace: def __init__(self): self.lst = [] - def configure(self, options): + def configure(self, options, updated): """ .replacements is a list of tuples (fpat, rex, s): diff --git a/mitmproxy/builtins/script.py b/mitmproxy/builtins/script.py index ed5f2ecd..c960dd1c 100644 --- a/mitmproxy/builtins/script.py +++ b/mitmproxy/builtins/script.py @@ -159,15 +159,16 @@ class Script: if self.should_reload.is_set(): self.should_reload.clear() ctx.log.info("Reloading script: %s" % self.name) - self.load_script() - self.configure(self.last_options) + self.ns = load_script(self.path, self.args) + self.start() + self.configure(self.last_options, self.last_options.keys()) else: self.run("tick") def start(self): self.load_script() - def configure(self, options): + def configure(self, options, updated): self.last_options = options if not self.observer: self.observer = polling.PollingObserver() @@ -177,7 +178,7 @@ class Script: os.path.dirname(self.path) or "." ) self.observer.start() - self.run("configure", options) + self.run("configure", options, updated) def done(self): self.run("done") @@ -188,26 +189,27 @@ class ScriptLoader(): """ An addon that manages loading scripts from options. """ - def configure(self, options): - for s in options.scripts: - if options.scripts.count(s) > 1: - raise exceptions.OptionsError("Duplicate script: %s" % s) - - for a in ctx.master.addons.chain[:]: - if isinstance(a, Script) and a.name not in options.scripts: - ctx.log.info("Un-loading script: %s" % a.name) - ctx.master.addons.remove(a) - - current = {} - for a in ctx.master.addons.chain[:]: - if isinstance(a, Script): - current[a.name] = a - ctx.master.addons.chain.remove(a) - - for s in options.scripts: - if s in current: - ctx.master.addons.chain.append(current[s]) - else: - ctx.log.info("Loading script: %s" % s) - sc = Script(s) - ctx.master.addons.add(sc) + def configure(self, options, updated): + if "scripts" in updated: + for s in options.scripts: + if options.scripts.count(s) > 1: + raise exceptions.OptionsError("Duplicate script: %s" % s) + + for a in ctx.master.addons.chain[:]: + if isinstance(a, Script) and a.name not in options.scripts: + ctx.log.info("Un-loading script: %s" % a.name) + ctx.master.addons.remove(a) + + current = {} + for a in ctx.master.addons.chain[:]: + if isinstance(a, Script): + current[a.name] = a + ctx.master.addons.chain.remove(a) + + for s in options.scripts: + if s in current: + ctx.master.addons.chain.append(current[s]) + else: + ctx.log.info("Loading script: %s" % s) + sc = Script(s) + ctx.master.addons.add(options, sc) diff --git a/mitmproxy/builtins/setheaders.py b/mitmproxy/builtins/setheaders.py index 6bda3f55..4a784a1d 100644 --- a/mitmproxy/builtins/setheaders.py +++ b/mitmproxy/builtins/setheaders.py @@ -6,7 +6,7 @@ class SetHeaders: def __init__(self): self.lst = [] - def configure(self, options): + def configure(self, options, updated): """ options.setheaders is a tuple of (fpatt, header, value) diff --git a/mitmproxy/builtins/stickyauth.py b/mitmproxy/builtins/stickyauth.py index 1309911c..98fb65ed 100644 --- a/mitmproxy/builtins/stickyauth.py +++ b/mitmproxy/builtins/stickyauth.py @@ -10,7 +10,7 @@ class StickyAuth: self.flt = None self.hosts = {} - def configure(self, options): + def configure(self, options, updated): if options.stickyauth: flt = filt.parse(options.stickyauth) if not flt: diff --git a/mitmproxy/builtins/stickycookie.py b/mitmproxy/builtins/stickycookie.py index dc699bb4..88333d5c 100644 --- a/mitmproxy/builtins/stickycookie.py +++ b/mitmproxy/builtins/stickycookie.py @@ -32,7 +32,7 @@ class StickyCookie: self.jar = collections.defaultdict(dict) self.flt = None - def configure(self, options): + def configure(self, options, updated): if options.stickycookie: flt = filt.parse(options.stickycookie) if not flt: diff --git a/mitmproxy/console/master.py b/mitmproxy/console/master.py index f4617f62..fad4c375 100644 --- a/mitmproxy/console/master.py +++ b/mitmproxy/console/master.py @@ -242,7 +242,7 @@ class ConsoleMaster(flow.FlowMaster): signals.pop_view_state.connect(self.sig_pop_view_state) signals.push_view_state.connect(self.sig_push_view_state) signals.sig_add_log.connect(self.sig_add_log) - self.addons.add(*builtins.default_addons()) + self.addons.add(options, *builtins.default_addons()) def __setattr__(self, name, value): self.__dict__[name] = value diff --git a/mitmproxy/dump.py b/mitmproxy/dump.py index 4f34ab95..83f44d87 100644 --- a/mitmproxy/dump.py +++ b/mitmproxy/dump.py @@ -42,8 +42,8 @@ class DumpMaster(flow.FlowMaster): def __init__(self, server, options): flow.FlowMaster.__init__(self, options, server, flow.State()) self.has_errored = False - self.addons.add(*builtins.default_addons()) - self.addons.add(dumper.Dumper()) + self.addons.add(options, *builtins.default_addons()) + self.addons.add(options, dumper.Dumper()) # This line is just for type hinting self.options = self.options # type: Options self.replay_ignore_params = options.replay_ignore_params diff --git a/mitmproxy/optmanager.py b/mitmproxy/optmanager.py index e94ef51d..140c7ca8 100644 --- a/mitmproxy/optmanager.py +++ b/mitmproxy/optmanager.py @@ -35,7 +35,7 @@ class OptManager(object): self.__dict__["_initialized"] = True @contextlib.contextmanager - def rollback(self): + def rollback(self, updated): old = self._opts.copy() try: yield @@ -44,7 +44,7 @@ class OptManager(object): self.errored.send(self, exc=e) # Rollback self.__dict__["_opts"] = old - self.changed.send(self) + self.changed.send(self, updated=updated) def __eq__(self, other): return self._opts == other._opts @@ -62,22 +62,22 @@ class OptManager(object): if not self._initialized: self._opts[attr] = value return - if attr not in self._opts: - raise KeyError("No such option: %s" % attr) - with self.rollback(): - self._opts[attr] = value - self.changed.send(self) + self.update(**{attr: value}) + + def keys(self): + return set(self._opts.keys()) def get(self, k, d=None): return self._opts.get(k, d) def update(self, **kwargs): + updated = set(kwargs.keys()) for k in kwargs: if k not in self._opts: raise KeyError("No such option: %s" % k) - with self.rollback(): + with self.rollback(updated): self._opts.update(kwargs) - self.changed.send(self) + self.changed.send(self, updated=updated) def setter(self, attr): """ diff --git a/mitmproxy/proxy/config.py b/mitmproxy/proxy/config.py index 7aa4c736..a74ba7e2 100644 --- a/mitmproxy/proxy/config.py +++ b/mitmproxy/proxy/config.py @@ -79,10 +79,10 @@ class ProxyConfig: self.certstore = None self.clientcerts = None self.openssl_verification_mode_server = None - self.configure(options) + self.configure(options, set(options.keys())) options.changed.connect(self.configure) - def configure(self, options): + def configure(self, options, updated): conflict = all( [ options.add_upstream_certs_to_client_chain, diff --git a/mitmproxy/web/master.py b/mitmproxy/web/master.py index 3d384612..9ddb61d4 100644 --- a/mitmproxy/web/master.py +++ b/mitmproxy/web/master.py @@ -136,7 +136,7 @@ class WebMaster(flow.FlowMaster): def __init__(self, server, options): super(WebMaster, self).__init__(options, server, WebState()) - self.addons.add(*builtins.default_addons()) + self.addons.add(options, *builtins.default_addons()) self.app = app.Application( self, self.options.wdebug, self.options.wauthenticator ) |