diff --git a/mitmproxy/addonmanager.py b/mitmproxy/addonmanager.py index b3a71f1b4..1fc8e4dcb 100644 --- a/mitmproxy/addonmanager.py +++ b/mitmproxy/addonmanager.py @@ -1,3 +1,5 @@ +import typing + from mitmproxy import exceptions from mitmproxy import eventsequence from mitmproxy import controller @@ -9,6 +11,30 @@ def _get_name(itm): return getattr(itm, "name", itm.__class__.__name__.lower()) +class Loader: + """ + A loader object is passed to the load() event when addons start up. + """ + def __init__(self, master): + self.master = master + + def add_option( + self, + name: str, + typespec: type, + default: typing.Any, + help: str, + choices: typing.Optional[typing.Sequence[str]] = None + ) -> None: + self.master.options.add_option( + name, + typespec, + default, + help, + choices + ) + + class AddonManager: def __init__(self, master): self.chain = [] @@ -41,8 +67,9 @@ class AddonManager: """ self.chain.extend(addons) with self.master.handlecontext(): + l = Loader(self.master) for i in addons: - self.invoke_addon(i, "load", self.master.options) + self.invoke_addon(i, "load", l) def remove(self, addon): """ diff --git a/mitmproxy/addons/script.py b/mitmproxy/addons/script.py index cb98704a0..ca1085d09 100644 --- a/mitmproxy/addons/script.py +++ b/mitmproxy/addons/script.py @@ -6,6 +6,7 @@ import threading import traceback import types +from mitmproxy import addonmanager from mitmproxy import exceptions from mitmproxy import ctx from mitmproxy import eventsequence @@ -184,10 +185,11 @@ class Script: def load_script(self): self.ns = load_script(self.path, self.args) - ret = self.run("load", self.last_options) + l = addonmanager.Loader(ctx.master) + ret = self.run("load", l) if ret: self.ns = ret - self.run("load", self.last_options) + self.run("load", l) def tick(self): if self.should_reload.is_set():