diff options
Diffstat (limited to 'mitmproxy/builtins/script.py')
-rw-r--r-- | mitmproxy/builtins/script.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/mitmproxy/builtins/script.py b/mitmproxy/builtins/script.py index bbefc5c7..9bf25703 100644 --- a/mitmproxy/builtins/script.py +++ b/mitmproxy/builtins/script.py @@ -88,6 +88,8 @@ def scriptenv(path, args): sys.path.append(script_dir) try: yield + except SystemExit as v: + ctx.log.error("Script exited with code %s" % v.code) except Exception: etype, value, tb = sys.exc_info() tb = cut_traceback(tb, "scriptenv").tb_next @@ -237,16 +239,35 @@ class ScriptLoader(): ctx.log.info("Un-loading script: %s" % a.name) ctx.master.addons.remove(a) + # The machinations below are to ensure that: + # - Scripts remain in the same order + # - Scripts are listed directly after the script addon. This is + # needed to ensure that interactions with, for instance, flow + # serialization remains correct. + # - Scripts are not initialized un-necessarily. If only a + # script's order in the script list has changed, it should simply + # be moved. + current = {} for a in ctx.master.addons.chain[:]: if isinstance(a, Script): current[a.name] = a ctx.master.addons.chain.remove(a) + ordered = [] + newscripts = [] for s in options.scripts: if s in current: - ctx.master.addons.chain.append(current[s]) + ordered.append(current[s]) else: ctx.log.info("Loading script: %s" % s) sc = Script(s) - ctx.master.addons.add(sc) + ordered.append(sc) + newscripts.append(sc) + + ochain = ctx.master.addons.chain + pos = ochain.index(self) + ctx.master.addons.chain = ochain[:pos + 1] + ordered + ochain[pos + 1:] + + for s in newscripts: + ctx.master.addons.startup(s) |