make sure that running() is only invoked once on startup. (#4964)

fix #3584
This commit is contained in:
Maximilian Hils 2021-11-27 14:11:23 +01:00 committed by GitHub
parent 3a2c87432c
commit 6997129bc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 5 deletions

View File

@ -19,6 +19,7 @@
* Remove workarounds for old pyOpenSSL versions (#4831, @KarlParkinson)
* Add fonts to asset filter (~a) (#4928, @elespike)
* Fix bug that crashed when using `view.flows.resolve` (#4916, @rbdixon)
* Fix a bug where `running()` is invoked twice on startup (#3584, @mhils)
## 28 September 2021: mitmproxy 7.0.4

View File

@ -75,6 +75,7 @@ class Script:
path.strip("'\" ")
)
self.ns = None
self.is_running = False
if not os.path.isfile(self.fullpath):
raise exceptions.OptionsError('No such script')
@ -85,6 +86,9 @@ class Script:
else:
self.loadscript()
def running(self):
self.is_running = True
def done(self):
if self.reloadtask:
self.reloadtask.cancel()
@ -105,7 +109,8 @@ class Script:
if self.ns:
# We're already running, so we have to explicitly register and
# configure the addon
ctx.master.addons.invoke_addon(self.ns, hooks.RunningHook())
if self.is_running:
ctx.master.addons.invoke_addon(self.ns, hooks.RunningHook())
try:
ctx.master.addons.invoke_addon(
self.ns,

View File

@ -1,5 +1,6 @@
import asyncio
import os
import re
import sys
import traceback
@ -11,6 +12,7 @@ from mitmproxy.addons import script
from mitmproxy.proxy.layers.http import HttpRequestHook
from mitmproxy.test import taddons
from mitmproxy.test import tflow
from mitmproxy.tools import main
# We want this to be speedy for testing
@ -90,7 +92,7 @@ class TestScript:
)
with taddons.context(sc) as tctx:
tctx.configure(sc)
await tctx.master.await_log("recorder running")
await tctx.master.await_log("recorder configure")
rec = tctx.master.addons.get("recorder")
assert rec.call_log[0][0:2] == ("recorder", "load")
@ -128,7 +130,7 @@ class TestScript:
True,
)
tctx.master.addons.add(sc)
await tctx.master.await_log("error running")
await tctx.master.await_log("error load")
tctx.configure(sc)
f = tflow.tflow(resp=True)
@ -331,3 +333,21 @@ class TestScriptLoader:
'e running',
'e configure',
]
def test_order(event_loop, tdata, capsys):
"""Integration test: Make sure that the runtime hooks are triggered on startup in the correct order."""
asyncio.set_event_loop(event_loop)
main.mitmdump([
"-n",
"-s", tdata.path("mitmproxy/data/addonscripts/recorder/recorder.py"),
"-s", tdata.path("mitmproxy/data/addonscripts/shutdown.py"),
])
assert re.match(
r"Loading script.+recorder.py\n"
r"\('recorder', 'load', .+\n"
r"\('recorder', 'configure', .+\n"
r"Loading script.+shutdown.py\n"
r"\('recorder', 'running', .+\n$",
capsys.readouterr().out,
)

View File

@ -1,8 +1,8 @@
from mitmproxy import ctx
def running():
ctx.log.info("error running")
def load(loader):
ctx.log.info("error load")
def request(flow):