addons: start -> load throughout

This commit is contained in:
Aldo Cortesi 2017-03-24 10:15:41 +13:00
parent b531353ee0
commit 541c1e8b9f
25 changed files with 49 additions and 49 deletions

View File

@ -54,5 +54,5 @@ class Rerouter:
flow.request.port = port
def start(opts):
def load(opts):
return Rerouter()

View File

@ -25,7 +25,7 @@ HAR = {}
SERVERS_SEEN = set()
def start(opts):
def load(opts):
"""
Called once on script startup before any other events.
"""

View File

@ -14,6 +14,6 @@ Usage:
"""
def start(opts):
def load(opts):
import pydevd
pydevd.settrace("localhost", port=5678, stdoutToServer=True, stderrToServer=True)

View File

@ -112,7 +112,7 @@ class TlsFeedback(TlsLayer):
tls_strategy = None
def start(opts):
def load(opts):
global tls_strategy
if len(sys.argv) == 2:
tls_strategy = ProbabilisticStrategy(float(sys.argv[1]))

View File

@ -3,5 +3,5 @@ class AddHeader:
flow.response.headers["newheader"] = "foo"
def start(opts):
def load(opts):
return AddHeader()

View File

@ -20,7 +20,7 @@ class ViewSwapCase(contentviews.View):
view = ViewSwapCase()
def start(opts):
def load(opts):
contentviews.add(view)

View File

@ -1,7 +1,7 @@
from mitmproxy import ctx
def start(options):
def load(options):
ctx.log.info("Registering option 'custom'")
options.add_option("custom", bool, False, "A custom option")

View File

@ -17,7 +17,7 @@ class Filter:
print(flow)
def start(opts):
def load(opts):
if len(sys.argv) != 2:
raise ValueError("Usage: -s 'filt.py FILTER'")
return Filter(sys.argv[1])

View File

@ -23,7 +23,7 @@ class Writer:
self.w.add(flow)
def start(opts):
def load(opts):
if len(sys.argv) != 2:
raise ValueError('Usage: -s "flowriter.py filename"')
return Writer(sys.argv[1])

View File

@ -7,6 +7,6 @@ If you want to help us out: https://github.com/mitmproxy/mitmproxy/issues/1530 :
from mitmproxy import ctx
def start(opts):
def load(opts):
ctx.log.info("This is some informative text.")
ctx.log.error("This is an error.")

View File

@ -23,7 +23,7 @@ class Injector:
flow.response.content = str(html).encode("utf8")
def start(opts):
def load(opts):
if len(sys.argv) != 2:
raise ValueError('Usage: -s "iframe_injector.py url"')
return Injector(sys.argv[1])

View File

@ -9,7 +9,7 @@ class Replacer:
flow.response.replace(self.src, self.dst)
def start(opts):
def load(opts):
parser = argparse.ArgumentParser()
parser.add_argument("src", type=str)
parser.add_argument("dst", type=str)

View File

@ -14,7 +14,7 @@ def hello_world():
return 'Hello World!'
def start(opts):
def load(opts):
# Host app at the magic domain "proxapp" on port 80. Requests to this
# domain and port combination will now be routed to the WSGI app instance.
return wsgiapp.WSGIApp(app, "proxapp", 80)

View File

@ -42,7 +42,7 @@ class AddonManager:
self.chain.extend(addons)
with self.master.handlecontext():
for i in addons:
self.invoke_addon(i, "start", self.master.options)
self.invoke_addon(i, "load", self.master.options)
def remove(self, addon):
"""

View File

@ -184,22 +184,22 @@ class Script:
def load_script(self):
self.ns = load_script(self.path, self.args)
ret = self.run("start", self.last_options)
ret = self.run("load", self.last_options)
if ret:
self.ns = ret
self.run("start", self.last_options)
self.run("load", self.last_options)
def tick(self):
if self.should_reload.is_set():
self.should_reload.clear()
ctx.log.info("Reloading script: %s" % self.name)
self.ns = load_script(self.path, self.args)
self.start(self.last_options)
self.load(self.last_options)
self.configure(self.last_options, self.last_options.keys())
else:
self.run("tick")
def start(self, opts):
def load(self, opts):
self.last_options = opts
self.load_script()
@ -287,7 +287,7 @@ class ScriptLoader:
ctx.master.addons.chain = ochain[:pos + 1] + ordered + ochain[pos + 1:]
for s in newscripts:
ctx.master.addons.invoke_addon(s, "start", options)
ctx.master.addons.invoke_addon(s, "load", options)
if self.is_running:
# If we're already running, we configure and tell the addon
# we're up and running.

View File

@ -16,7 +16,7 @@ class ServerPlayback:
self.stop = False
self.final_flow = None
def load(self, flows):
def load_flows(self, flows):
for i in flows:
if i.response:
l = self.flowmap.setdefault(self._hash(i), [])
@ -100,7 +100,7 @@ class ServerPlayback:
flows = io.read_flows_from_paths(options.server_replay)
except exceptions.FlowReadException as e:
raise exceptions.OptionsError(str(e))
self.load(flows)
self.load_flows(flows)
def tick(self):
if self.stop and not self.final_flow.live:

View File

@ -32,7 +32,7 @@ Events = frozenset([
"configure",
"done",
"log",
"start",
"load",
"running",
"tick",
])

View File

@ -12,7 +12,7 @@ class ScriptThread(basethread.BaseThread):
def concurrent(fn):
if fn.__name__ not in eventsequence.Events - {"start", "configure", "tick"}:
if fn.__name__ not in eventsequence.Events - {"load", "configure", "tick"}:
raise NotImplementedError(
"Concurrent decorator not supported for '%s' method." % fn.__name__
)

View File

@ -104,7 +104,7 @@ def test_load_script():
"mitmproxy/data/addonscripts/recorder.py"
), []
)
assert ns.start
assert ns.load
def test_script_print_stdout():
@ -129,7 +129,7 @@ class TestScript:
)
)
sc.load_script()
assert sc.ns.call_log[0][0:2] == ("solo", "start")
assert sc.ns.call_log[0][0:2] == ("solo", "load")
sc.ns.call_log = []
f = tflow.tflow(resp=True)
@ -157,7 +157,7 @@ class TestScript:
sc = script.Script(
tutils.test_data.path("mitmproxy/data/addonscripts/error.py")
)
sc.start(tctx.options)
sc.load(tctx.options)
f = tflow.tflow(resp=True)
sc.request(f)
assert tctx.master.logs[0].level == "error"
@ -173,10 +173,10 @@ class TestScript:
"mitmproxy/data/addonscripts/addon.py"
)
)
sc.start(tctx.options)
sc.load(tctx.options)
tctx.configure(sc)
assert sc.ns.event_log == [
'scriptstart', 'addonstart', 'addonconfigure'
'scriptload', 'addonload', 'addonconfigure'
]
@ -213,7 +213,7 @@ class TestScriptLoader:
), [f]
)
evts = [i[1] for i in sc.ns.call_log]
assert evts == ['start', 'requestheaders', 'request', 'responseheaders', 'response', 'done']
assert evts == ['load', 'requestheaders', 'request', 'responseheaders', 'response', 'done']
f = tflow.tflow(resp=True)
with m.handlecontext():
@ -271,15 +271,15 @@ class TestScriptLoader:
)
debug = [i.msg for i in tctx.master.logs if i.level == "debug"]
assert debug == [
'a start',
'a load',
'a configure',
'a running',
'b start',
'b load',
'b configure',
'b running',
'c start',
'c load',
'c configure',
'c running',
]
@ -307,7 +307,7 @@ class TestScriptLoader:
assert debug == [
'c done',
'b done',
'x start',
'x load',
'x configure',
'x running',
]

View File

@ -44,12 +44,12 @@ def test_server_playback():
assert not sp.flowmap
sp.load([f])
sp.load_flows([f])
assert sp.flowmap
assert sp.next_flow(f)
assert not sp.flowmap
sp.load([f])
sp.load_flows([f])
assert sp.flowmap
sp.clear()
assert not sp.flowmap
@ -192,7 +192,7 @@ def test_load():
r2 = tflow.tflow(resp=True)
r2.request.headers["key"] = "two"
s.load([r, r2])
s.load_flows([r, r2])
assert s.count() == 2
@ -218,7 +218,7 @@ def test_load_with_server_replay_nopop():
r2 = tflow.tflow(resp=True)
r2.request.headers["key"] = "two"
s.load([r, r2])
s.load_flows([r, r2])
assert s.count() == 2
s.next_flow(r)
@ -319,7 +319,7 @@ def test_server_playback_full():
f = tflow.tflow()
f.response = mitmproxy.test.tutils.tresp(content=f.request.content)
s.load([f, f])
s.load_flows([f, f])
tf = tflow.tflow()
assert not tf.response
@ -352,7 +352,7 @@ def test_server_playback_kill():
f = tflow.tflow()
f.response = mitmproxy.test.tutils.tresp(content=f.request.content)
s.load([f])
s.load_flows([f])
f = tflow.tflow()
f.request.host = "nonexistent"

View File

@ -6,8 +6,8 @@ class Addon:
def event_log(self):
return event_log
def start(self, opts):
event_log.append("addonstart")
def load(self, opts):
event_log.append("addonload")
def configure(self, options, updated):
event_log.append("addonconfigure")
@ -17,6 +17,6 @@ def configure(options, updated):
event_log.append("addonconfigure")
def start(opts):
event_log.append("scriptstart")
def load(opts):
event_log.append("scriptload")
return Addon()

View File

@ -9,5 +9,5 @@ class ConcurrentClass:
time.sleep(0.1)
def start(opts):
def load(opts):
return ConcurrentClass()

View File

@ -1,2 +1,2 @@
def start(opts):
def load(l):
print("stdoutprint")

View File

@ -22,5 +22,5 @@ class CallLogger:
raise AttributeError
def start(opts):
def load(opts):
return CallLogger(*sys.argv[1:])

View File

@ -24,7 +24,7 @@ class TestConcurrent(tservers.MasterTest):
"mitmproxy/data/addonscripts/concurrent_decorator.py"
)
)
sc.start(tctx.options)
sc.load(tctx.options)
f1, f2 = tflow.tflow(), tflow.tflow()
tctx.cycle(sc, f1)
@ -42,7 +42,7 @@ class TestConcurrent(tservers.MasterTest):
"mitmproxy/data/addonscripts/concurrent_decorator_err.py"
)
)
sc.start(tctx.options)
sc.load(tctx.options)
assert tctx.master.has_log("decorator not supported")
def test_concurrent_class(self):
@ -52,7 +52,7 @@ class TestConcurrent(tservers.MasterTest):
"mitmproxy/data/addonscripts/concurrent_decorator_class.py"
)
)
sc.start(tctx.options)
sc.load(tctx.options)
f1, f2 = tflow.tflow(), tflow.tflow()
tctx.cycle(sc, f1)