mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 00:01:36 +00:00
Decouple libmproxy.script from libmproxy.flow
This commit is contained in:
parent
2522ba69fa
commit
cd0b9e01be
@ -731,3 +731,7 @@ class ConsoleMaster(flow.FlowMaster):
|
||||
if f:
|
||||
self.process_flow(f)
|
||||
return f
|
||||
|
||||
def script_change(self, script):
|
||||
self.masterq.put(("script_change", script))
|
||||
signals.status_message.send(message="<{}> reloaded.".format(script.args[0]))
|
||||
|
@ -642,6 +642,7 @@ class FlowMaster(controller.Master):
|
||||
|
||||
self.stream = None
|
||||
self.apps = AppRegistry()
|
||||
script.sig_script_change.connect(self.script_change)
|
||||
|
||||
def start_app(self, host, port):
|
||||
self.apps.add(
|
||||
@ -666,10 +667,6 @@ class FlowMaster(controller.Master):
|
||||
except script.ScriptError as e:
|
||||
self.add_event("Script error:\n" + str(e), "error")
|
||||
self.scripts.remove(script_obj)
|
||||
|
||||
def reload_scripts(self):
|
||||
for s in self.scripts[:]:
|
||||
s.load()
|
||||
|
||||
def load_script(self, command):
|
||||
"""
|
||||
@ -1025,6 +1022,9 @@ class FlowMaster(controller.Master):
|
||||
def handle_accept_intercept(self, f):
|
||||
self.state.update_flow(f)
|
||||
|
||||
def handle_script_change(self, script):
|
||||
script.load()
|
||||
|
||||
def shutdown(self):
|
||||
self.unload_scripts()
|
||||
controller.Master.shutdown(self)
|
||||
@ -1041,6 +1041,10 @@ class FlowMaster(controller.Master):
|
||||
self.stream.fo.close()
|
||||
self.stream = None
|
||||
|
||||
def script_change(self, script):
|
||||
self.masterq.put(("script_change", script))
|
||||
self.add_event("<{}> reloaded.".format(script.args[0]))
|
||||
|
||||
|
||||
def read_flows_from_paths(paths):
|
||||
"""
|
||||
|
@ -6,7 +6,9 @@ import shlex
|
||||
import sys
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import PatternMatchingEventHandler, FileModifiedEvent
|
||||
import blinker
|
||||
|
||||
sig_script_change = blinker.Signal()
|
||||
|
||||
class ScriptError(Exception):
|
||||
pass
|
||||
@ -70,7 +72,7 @@ class Script:
|
||||
self.ctx = ScriptContext(master)
|
||||
self.ns = None
|
||||
self.load()
|
||||
observe_scripts(master, self.args[0])
|
||||
self.observe_scripts()
|
||||
|
||||
@classmethod
|
||||
def parse_command(cls, command):
|
||||
@ -139,6 +141,13 @@ class Script:
|
||||
raise ScriptError(traceback.format_exc(e))
|
||||
else:
|
||||
return None
|
||||
|
||||
def observe_scripts(self):
|
||||
script_dir = os.path.dirname(self.args[0])
|
||||
event_handler = ScriptModified(self)
|
||||
observer = Observer()
|
||||
observer.schedule(event_handler, script_dir)
|
||||
observer.start()
|
||||
|
||||
|
||||
class ReplyProxy(object):
|
||||
@ -199,19 +208,9 @@ def concurrent(fn):
|
||||
|
||||
class ScriptModified(PatternMatchingEventHandler):
|
||||
|
||||
def __init__(self, flow_master):
|
||||
self.flow_master = flow_master
|
||||
def __init__(self, script):
|
||||
PatternMatchingEventHandler.__init__(self, ignore_directories=True, patterns=["*.py"])
|
||||
self.context = ScriptContext(self.flow_master)
|
||||
self.script = script
|
||||
|
||||
def on_modified(self, event=FileModifiedEvent):
|
||||
self.flow_master.reload_scripts()
|
||||
self.context.log("script: <{}> reloaded.".format(event.src_path))
|
||||
|
||||
|
||||
def observe_scripts(flow_master, path):
|
||||
script_dir = os.path.dirname(path)
|
||||
event_handler = ScriptModified(flow_master)
|
||||
observer = Observer()
|
||||
observer.schedule(event_handler, script_dir)
|
||||
observer.start()
|
||||
sig_script_change.send(self.script)
|
||||
|
Loading…
Reference in New Issue
Block a user