Kill flow.master - create master.Master

Also extract events into .events
This commit is contained in:
Aldo Cortesi 2016-10-19 13:22:50 +13:00
parent 264a09e778
commit bce387a5a0
31 changed files with 239 additions and 230 deletions

View File

@ -12,10 +12,10 @@ from mitmproxy import flow, controller, options
from mitmproxy.proxy import ProxyServer, ProxyConfig from mitmproxy.proxy import ProxyServer, ProxyConfig
class MyMaster(flow.FlowMaster): class MyMaster(master.Master):
def run(self): def run(self):
try: try:
flow.FlowMaster.run(self) master.Master.run(self)
except KeyboardInterrupt: except KeyboardInterrupt:
self.shutdown() self.shutdown()

View File

@ -10,14 +10,14 @@ from mitmproxy import controller, proxy
from mitmproxy.proxy.server import ProxyServer from mitmproxy.proxy.server import ProxyServer
class StickyMaster(controller.Master): class StickyMaster(master.Master):
def __init__(self, server): def __init__(self, server):
controller.Master.__init__(self, server) master.Master.__init__(self, server)
self.stickyhosts = {} self.stickyhosts = {}
def run(self): def run(self):
try: try:
return controller.Master.run(self) return master.Master.run(self)
except KeyboardInterrupt: except KeyboardInterrupt:
self.shutdown() self.shutdown()

View File

@ -6,9 +6,8 @@ import threading
import traceback import traceback
from mitmproxy import exceptions from mitmproxy import exceptions
from mitmproxy import controller
from mitmproxy import ctx from mitmproxy import ctx
from mitmproxy.flow import master as flowmaster from mitmproxy import events
import watchdog.events import watchdog.events
@ -154,7 +153,7 @@ class Script:
self.last_options = None self.last_options = None
self.should_reload = threading.Event() self.should_reload = threading.Event()
for i in controller.Events: for i in events.Events:
if not hasattr(self, i): if not hasattr(self, i):
def mkprox(): def mkprox():
evt = i evt = i
@ -221,7 +220,7 @@ class ScriptLoader:
sc = Script(command) sc = Script(command)
sc.load_script() sc.load_script()
for f in flows: for f in flows:
for evt, o in flowmaster.event_sequence(f): for evt, o in events.event_sequence(f):
sc.run(evt, o) sc.run(evt, o)
sc.done() sc.done()
return sc return sc

View File

@ -18,6 +18,7 @@ from mitmproxy import builtins
from mitmproxy import contentviews from mitmproxy import contentviews
from mitmproxy import controller from mitmproxy import controller
from mitmproxy import exceptions from mitmproxy import exceptions
from mitmproxy import master
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import flowfilter from mitmproxy import flowfilter
from mitmproxy import utils from mitmproxy import utils
@ -218,11 +219,11 @@ class Options(mitmproxy.options.Options):
super().__init__(**kwargs) super().__init__(**kwargs)
class ConsoleMaster(flow.FlowMaster): class ConsoleMaster(master.Master):
palette = [] palette = []
def __init__(self, options, server): def __init__(self, options, server):
flow.FlowMaster.__init__(self, options, server) master.Master.__init__(self, options, server)
self.state = ConsoleState() self.state = ConsoleState()
self.stream_path = None self.stream_path = None
# This line is just for type hinting # This line is just for type hinting
@ -600,7 +601,7 @@ class ConsoleMaster(flow.FlowMaster):
def load_flows_path(self, path): def load_flows_path(self, path):
reterr = None reterr = None
try: try:
flow.FlowMaster.load_flows_file(self, path) master.Master.load_flows_file(self, path)
except exceptions.FlowReadException as e: except exceptions.FlowReadException as e:
reterr = str(e) reterr = str(e)
signals.flowlist_change.send(self) signals.flowlist_change.send(self)
@ -631,7 +632,7 @@ class ConsoleMaster(flow.FlowMaster):
def shutdown(self): def shutdown(self):
self.state.killall(self) self.state.killall(self)
flow.FlowMaster.shutdown(self) master.Master.shutdown(self)
def clear_flows(self): def clear_flows(self):
self.state.clear() self.state.clear()

View File

@ -1,45 +1,6 @@
import functools import functools
import threading
import contextlib
import queue import queue
from mitmproxy import exceptions
from mitmproxy import addons
from mitmproxy import options
from . import ctx as mitmproxy_ctx
from netlib import basethread
from . import exceptions
Events = frozenset([
"clientconnect",
"clientdisconnect",
"serverconnect",
"serverdisconnect",
"tcp_start",
"tcp_message",
"tcp_error",
"tcp_end",
"request",
"requestheaders",
"response",
"responseheaders",
"error",
"intercept",
"resume",
"websocket_handshake",
"next_layer",
"configure",
"done",
"log",
"start",
"tick",
])
class LogEntry: class LogEntry:
@ -83,101 +44,6 @@ class Log:
self.master.add_log(text, level) self.master.add_log(text, level)
class Master:
"""
The master handles mitmproxy's main event loop.
"""
def __init__(self, opts, server):
self.options = opts or options.Options()
self.addons = addons.Addons(self)
self.event_queue = queue.Queue()
self.should_exit = threading.Event()
self.server = server
channel = Channel(self.event_queue, self.should_exit)
server.set_channel(channel)
@contextlib.contextmanager
def handlecontext(self):
# Handlecontexts also have to nest - leave cleanup to the outermost
if mitmproxy_ctx.master:
yield
return
mitmproxy_ctx.master = self
mitmproxy_ctx.log = Log(self)
try:
yield
finally:
mitmproxy_ctx.master = None
mitmproxy_ctx.log = None
def tell(self, mtype, m):
m.reply = DummyReply()
self.event_queue.put((mtype, m))
def add_log(self, e, level):
"""
level: debug, info, warn, error
"""
with self.handlecontext():
self.addons("log", LogEntry(e, level))
def start(self):
self.should_exit.clear()
ServerThread(self.server).start()
def run(self):
self.start()
try:
while not self.should_exit.is_set():
# Don't choose a very small timeout in Python 2:
# https://github.com/mitmproxy/mitmproxy/issues/443
# TODO: Lower the timeout value if we move to Python 3.
self.tick(0.1)
finally:
self.shutdown()
def tick(self, timeout):
with self.handlecontext():
self.addons("tick")
changed = False
try:
mtype, obj = self.event_queue.get(timeout=timeout)
if mtype not in Events:
raise exceptions.ControlException("Unknown event %s" % repr(mtype))
handle_func = getattr(self, mtype)
if not callable(handle_func):
raise exceptions.ControlException("Handler %s not callable" % mtype)
if not handle_func.__dict__.get("__handler"):
raise exceptions.ControlException(
"Handler function %s is not decorated with controller.handler" % (
handle_func
)
)
handle_func(obj)
self.event_queue.task_done()
changed = True
except queue.Empty:
pass
return changed
def shutdown(self):
self.server.shutdown()
self.should_exit.set()
self.addons.done()
class ServerThread(basethread.BaseThread):
def __init__(self, server):
self.server = server
address = getattr(self.server, "address", None)
super().__init__(
"ServerThread ({})".format(repr(address))
)
def run(self):
self.server.serve_forever()
class Channel: class Channel:
""" """
The only way for the proxy server to communicate with the master The only way for the proxy server to communicate with the master

View File

@ -1,2 +1,2 @@
master = None # type: "mitmproxy.flow.FlowMaster" master = None # type: "mitmproxy.master.Master"
log = None # type: "mitmproxy.controller.Log" log = None # type: "mitmproxy.controller.Log"

View File

@ -6,6 +6,7 @@ from mitmproxy import exceptions
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import builtins from mitmproxy import builtins
from mitmproxy import options from mitmproxy import options
from mitmproxy import master
from mitmproxy.builtins import dumper, termlog from mitmproxy.builtins import dumper, termlog
from netlib import tcp from netlib import tcp
@ -30,10 +31,10 @@ class Options(options.Options):
super().__init__(**kwargs) super().__init__(**kwargs)
class DumpMaster(flow.FlowMaster): class DumpMaster(master.Master):
def __init__(self, options, server): def __init__(self, options, server):
flow.FlowMaster.__init__(self, options, server) master.Master.__init__(self, options, server)
self.has_errored = False self.has_errored = False
self.addons.add(termlog.TermLog()) self.addons.add(termlog.TermLog())
self.addons.add(*builtins.default_addons()) self.addons.add(*builtins.default_addons())

58
mitmproxy/events.py Normal file
View File

@ -0,0 +1,58 @@
from mitmproxy import controller
from mitmproxy import models
Events = frozenset([
"clientconnect",
"clientdisconnect",
"serverconnect",
"serverdisconnect",
"tcp_start",
"tcp_message",
"tcp_error",
"tcp_end",
"request",
"requestheaders",
"response",
"responseheaders",
"error",
"intercept",
"resume",
"websocket_handshake",
"next_layer",
"configure",
"done",
"log",
"start",
"tick",
])
def event_sequence(f):
if isinstance(f, models.HTTPFlow):
if f.request:
yield "requestheaders", f
yield "request", f
if f.response:
yield "responseheaders", f
yield "response", f
if f.error:
yield "error", f
elif isinstance(f, models.TCPFlow):
messages = f.messages
f.messages = []
f.reply = controller.DummyReply()
yield "tcp_start", f
while messages:
f.messages.append(messages.pop(0))
yield "tcp_message", f
if f.error:
yield "tcp_error", f
yield "tcp_end", f
else:
raise NotImplementedError

View File

@ -1,10 +1,9 @@
from mitmproxy.flow import export from mitmproxy.flow import export
from mitmproxy.flow.io import FlowWriter, FilteredFlowWriter, FlowReader, read_flows_from_paths from mitmproxy.flow.io import FlowWriter, FilteredFlowWriter, FlowReader, read_flows_from_paths
from mitmproxy.flow.master import FlowMaster
from mitmproxy.flow.state import State, FlowView from mitmproxy.flow.state import State, FlowView
__all__ = [ __all__ = [
"export", "export",
"FlowWriter", "FilteredFlowWriter", "FlowReader", "read_flows_from_paths", "FlowWriter", "FilteredFlowWriter", "FlowReader", "read_flows_from_paths",
"FlowMaster", "State", "FlowView", "State", "FlowView",
] ]

View File

@ -1,40 +1,117 @@
import os import os
import threading
import contextlib
import queue
import sys import sys
from netlib import http from mitmproxy import addons
from mitmproxy import options
from mitmproxy import controller from mitmproxy import controller
from mitmproxy import events
from mitmproxy import exceptions from mitmproxy import exceptions
from mitmproxy import models from mitmproxy import models
from mitmproxy.flow import io from mitmproxy.flow import io
from mitmproxy.protocol import http_replay from mitmproxy.protocol import http_replay
from netlib import basethread
from netlib import http
from . import ctx as mitmproxy_ctx
def event_sequence(f): class ServerThread(basethread.BaseThread):
if isinstance(f, models.HTTPFlow): def __init__(self, server):
if f.request: self.server = server
yield "requestheaders", f address = getattr(self.server, "address", None)
yield "request", f super().__init__(
if f.response: "ServerThread ({})".format(repr(address))
yield "responseheaders", f )
yield "response", f
if f.error: def run(self):
yield "error", f self.server.serve_forever()
elif isinstance(f, models.TCPFlow):
messages = f.messages
f.messages = []
f.reply = controller.DummyReply()
yield "tcp_start", f
while messages:
f.messages.append(messages.pop(0))
yield "tcp_message", f
if f.error:
yield "tcp_error", f
yield "tcp_end", f
else:
raise NotImplementedError
class FlowMaster(controller.Master): class Master:
"""
The master handles mitmproxy's main event loop.
"""
def __init__(self, opts, server):
self.options = opts or options.Options()
self.addons = addons.Addons(self)
self.event_queue = queue.Queue()
self.should_exit = threading.Event()
self.server = server
channel = controller.Channel(self.event_queue, self.should_exit)
server.set_channel(channel)
@contextlib.contextmanager
def handlecontext(self):
# Handlecontexts also have to nest - leave cleanup to the outermost
if mitmproxy_ctx.master:
yield
return
mitmproxy_ctx.master = self
mitmproxy_ctx.log = controller.Log(self)
try:
yield
finally:
mitmproxy_ctx.master = None
mitmproxy_ctx.log = None
def tell(self, mtype, m):
m.reply = controller.DummyReply()
self.event_queue.put((mtype, m))
def add_log(self, e, level):
"""
level: debug, info, warn, error
"""
with self.handlecontext():
self.addons("log", controller.LogEntry(e, level))
def start(self):
self.should_exit.clear()
ServerThread(self.server).start()
def run(self):
self.start()
try:
while not self.should_exit.is_set():
# Don't choose a very small timeout in Python 2:
# https://github.com/mitmproxy/mitmproxy/issues/443
# TODO: Lower the timeout value if we move to Python 3.
self.tick(0.1)
finally:
self.shutdown()
def tick(self, timeout):
with self.handlecontext():
self.addons("tick")
changed = False
try:
mtype, obj = self.event_queue.get(timeout=timeout)
if mtype not in events.Events:
raise exceptions.ControlException("Unknown event %s" % repr(mtype))
handle_func = getattr(self, mtype)
if not callable(handle_func):
raise exceptions.ControlException("Handler %s not callable" % mtype)
if not handle_func.__dict__.get("__handler"):
raise exceptions.ControlException(
"Handler function %s is not decorated with controller.handler" % (
handle_func
)
)
handle_func(obj)
self.event_queue.task_done()
changed = True
except queue.Empty:
pass
return changed
def shutdown(self):
self.server.shutdown()
self.should_exit.set()
self.addons.done()
def create_request(self, method, scheme, host, port, path): def create_request(self, method, scheme, host, port, path):
""" """
this method creates a new artificial and minimalist request also adds it to flowlist this method creates a new artificial and minimalist request also adds it to flowlist
@ -70,7 +147,7 @@ class FlowMaster(controller.Master):
f.request.port = self.server.config.upstream_server.address.port f.request.port = self.server.config.upstream_server.address.port
f.request.scheme = self.server.config.upstream_server.scheme f.request.scheme = self.server.config.upstream_server.scheme
f.reply = controller.DummyReply() f.reply = controller.DummyReply()
for e, o in event_sequence(f): for e, o in events.event_sequence(f):
getattr(self, e)(o) getattr(self, e)(o)
def load_flows(self, fr): def load_flows(self, fr):

View File

@ -3,7 +3,7 @@ This module provides a @concurrent decorator primitive to
offload computations from mitmproxy's main master thread. offload computations from mitmproxy's main master thread.
""" """
from mitmproxy import controller from mitmproxy import events
from netlib import basethread from netlib import basethread
@ -12,7 +12,7 @@ class ScriptThread(basethread.BaseThread):
def concurrent(fn): def concurrent(fn):
if fn.__name__ not in controller.Events - {"start", "configure", "tick"}: if fn.__name__ not in events.Events - {"start", "configure", "tick"}:
raise NotImplementedError( raise NotImplementedError(
"Concurrent decorator not supported for '%s' method." % fn.__name__ "Concurrent decorator not supported for '%s' method." % fn.__name__
) )

View File

@ -11,6 +11,7 @@ from mitmproxy import controller
from mitmproxy import exceptions from mitmproxy import exceptions
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import options from mitmproxy import options
from mitmproxy import master
from mitmproxy.web import app from mitmproxy.web import app
from netlib.http import authentication from netlib.http import authentication
@ -130,7 +131,7 @@ class Options(options.Options):
self.wauthenticator = None self.wauthenticator = None
class WebMaster(flow.FlowMaster): class WebMaster(master.Master):
def __init__(self, options, server): def __init__(self, options, server):
super().__init__(options, server) super().__init__(options, server)

View File

@ -1,6 +1,6 @@
from .. import tutils, mastertest from .. import tutils, mastertest
from mitmproxy.builtins import anticache from mitmproxy.builtins import anticache
from mitmproxy.flow import master from mitmproxy import master
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy from mitmproxy import proxy
@ -8,7 +8,7 @@ from mitmproxy import proxy
class TestAntiCache(mastertest.MasterTest): class TestAntiCache(mastertest.MasterTest):
def test_simple(self): def test_simple(self):
o = options.Options(anticache = True) o = options.Options(anticache = True)
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sa = anticache.AntiCache() sa = anticache.AntiCache()
m.addons.add(sa) m.addons.add(sa)

View File

@ -1,6 +1,6 @@
from .. import tutils, mastertest from .. import tutils, mastertest
from mitmproxy.builtins import anticomp from mitmproxy.builtins import anticomp
from mitmproxy.flow import master from mitmproxy import master
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy from mitmproxy import proxy
@ -8,7 +8,7 @@ from mitmproxy import proxy
class TestAntiComp(mastertest.MasterTest): class TestAntiComp(mastertest.MasterTest):
def test_simple(self): def test_simple(self):
o = options.Options(anticomp = True) o = options.Options(anticomp = True)
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sa = anticomp.AntiComp() sa = anticomp.AntiComp()
m.addons.add(sa) m.addons.add(sa)

View File

@ -25,7 +25,7 @@ class TestClientPlayback:
cp.keepserving = False cp.keepserving = False
cp.flows = None cp.flows = None
cp.current = None cp.current = None
with mock.patch("mitmproxy.controller.Master.shutdown") as sd: with mock.patch("mitmproxy.master.Master.shutdown") as sd:
with mastertest.mockctx(): with mastertest.mockctx():
cp.tick() cp.tick()
sd.assert_called() sd.assert_called()

View File

@ -3,7 +3,8 @@ from .. import tutils, mastertest
import os.path import os.path
from mitmproxy.builtins import filestreamer from mitmproxy.builtins import filestreamer
from mitmproxy.flow import master, FlowReader from mitmproxy import master
from mitmproxy.flow import io
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy from mitmproxy import proxy
@ -14,13 +15,13 @@ class TestStream(mastertest.MasterTest):
p = os.path.join(tdir, "foo") p = os.path.join(tdir, "foo")
def r(): def r():
r = FlowReader(open(p, "rb")) r = io.FlowReader(open(p, "rb"))
return list(r.stream()) return list(r.stream())
o = options.Options( o = options.Options(
outfile = (p, "wb") outfile = (p, "wb")
) )
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sa = filestreamer.FileStreamer() sa = filestreamer.FileStreamer()
m.addons.add(sa) m.addons.add(sa)

View File

@ -1,6 +1,6 @@
from .. import tutils, mastertest, tservers from .. import tutils, mastertest, tservers
from mitmproxy.builtins import replace from mitmproxy.builtins import replace
from mitmproxy.flow import master from mitmproxy import master
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy from mitmproxy import proxy
@ -36,7 +36,7 @@ class TestReplace(mastertest.MasterTest):
("~s", "foo", "bar"), ("~s", "foo", "bar"),
] ]
) )
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sa = replace.Replace() sa = replace.Replace()
m.addons.add(sa) m.addons.add(sa)

View File

@ -8,7 +8,7 @@ from mitmproxy import exceptions
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy from mitmproxy import proxy
from mitmproxy.builtins import script from mitmproxy.builtins import script
from mitmproxy.flow import master from mitmproxy import master
from .. import tutils, mastertest from .. import tutils, mastertest
@ -58,7 +58,7 @@ def test_load_script():
class TestScript(mastertest.MasterTest): class TestScript(mastertest.MasterTest):
def test_simple(self): def test_simple(self):
o = options.Options() o = options.Options()
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sc = script.Script( sc = script.Script(
tutils.test_data.path( tutils.test_data.path(
"data/addonscripts/recorder.py" "data/addonscripts/recorder.py"
@ -112,7 +112,7 @@ class TestScript(mastertest.MasterTest):
def test_addon(self): def test_addon(self):
o = options.Options() o = options.Options()
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sc = script.Script( sc = script.Script(
tutils.test_data.path( tutils.test_data.path(
"data/addonscripts/addon.py" "data/addonscripts/addon.py"
@ -145,7 +145,7 @@ class TestCutTraceback:
class TestScriptLoader(mastertest.MasterTest): class TestScriptLoader(mastertest.MasterTest):
def test_run_once(self): def test_run_once(self):
o = options.Options(scripts=[]) o = options.Options(scripts=[])
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sl = script.ScriptLoader() sl = script.ScriptLoader()
m.addons.add(sl) m.addons.add(sl)
@ -169,7 +169,7 @@ class TestScriptLoader(mastertest.MasterTest):
def test_simple(self): def test_simple(self):
o = options.Options(scripts=[]) o = options.Options(scripts=[])
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sc = script.ScriptLoader() sc = script.ScriptLoader()
m.addons.add(sc) m.addons.add(sc)
assert len(m.addons) == 1 assert len(m.addons) == 1
@ -184,7 +184,7 @@ class TestScriptLoader(mastertest.MasterTest):
def test_dupes(self): def test_dupes(self):
o = options.Options(scripts=["one", "one"]) o = options.Options(scripts=["one", "one"])
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sc = script.ScriptLoader() sc = script.ScriptLoader()
tutils.raises(exceptions.OptionsError, m.addons.add, o, sc) tutils.raises(exceptions.OptionsError, m.addons.add, o, sc)

View File

@ -1,6 +1,6 @@
from .. import tutils, mastertest from .. import tutils, mastertest
from mitmproxy.builtins import stickyauth from mitmproxy.builtins import stickyauth
from mitmproxy.flow import master from mitmproxy import master
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy from mitmproxy import proxy
@ -8,7 +8,7 @@ from mitmproxy import proxy
class TestStickyAuth(mastertest.MasterTest): class TestStickyAuth(mastertest.MasterTest):
def test_simple(self): def test_simple(self):
o = options.Options(stickyauth = ".*") o = options.Options(stickyauth = ".*")
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sa = stickyauth.StickyAuth() sa = stickyauth.StickyAuth()
m.addons.add(sa) m.addons.add(sa)

View File

@ -1,6 +1,6 @@
from .. import tutils, mastertest from .. import tutils, mastertest
from mitmproxy.builtins import stickycookie from mitmproxy.builtins import stickycookie
from mitmproxy.flow import master from mitmproxy import master
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy from mitmproxy import proxy
from netlib import tutils as ntutils from netlib import tutils as ntutils
@ -14,7 +14,7 @@ def test_domain_match():
class TestStickyCookie(mastertest.MasterTest): class TestStickyCookie(mastertest.MasterTest):
def mk(self): def mk(self):
o = options.Options(stickycookie = ".*") o = options.Options(stickycookie = ".*")
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sc = stickycookie.StickyCookie() sc = stickycookie.StickyCookie()
m.addons.add(sc) m.addons.add(sc)
return m, sc return m, sc

View File

@ -1,5 +1,5 @@
from .. import tutils, mastertest from .. import tutils, mastertest
from mitmproxy.flow import master from mitmproxy import master
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy from mitmproxy import proxy
@ -9,7 +9,7 @@ from mitmproxy.builtins import streambodies
class TestStreamBodies(mastertest.MasterTest): class TestStreamBodies(mastertest.MasterTest):
def test_simple(self): def test_simple(self):
o = options.Options(stream_large_bodies = 10) o = options.Options(stream_large_bodies = 10)
m = master.FlowMaster(o, proxy.DummyServer()) m = master.Master(o, proxy.DummyServer())
sa = streambodies.StreamBodies() sa = streambodies.StreamBodies()
m.addons.add(sa) m.addons.add(sa)

View File

@ -1,4 +1,5 @@
from mitmproxy import controller from mitmproxy import controller
from mitmproxy import events
from mitmproxy import ctx from mitmproxy import ctx
import sys import sys
@ -10,7 +11,7 @@ class CallLogger:
self.name = name self.name = name
def __getattr__(self, attr): def __getattr__(self, attr):
if attr in controller.Events: if attr in events.Events:
def prox(*args, **kwargs): def prox(*args, **kwargs):
lg = (self.name, attr, args, kwargs) lg = (self.name, attr, args, kwargs)
if attr != "log": if attr != "log":

View File

@ -3,7 +3,7 @@ import contextlib
from . import tutils from . import tutils
import netlib.tutils import netlib.tutils
from mitmproxy.flow import master from mitmproxy import master
from mitmproxy import flow, proxy, models, options from mitmproxy import flow, proxy, models, options
@ -39,9 +39,9 @@ class MasterTest:
f.close() f.close()
class RecordingMaster(master.FlowMaster): class RecordingMaster(master.Master):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
master.FlowMaster.__init__(self, *args, **kwargs) master.Master.__init__(self, *args, **kwargs)
self.event_log = [] self.event_log = []
def add_log(self, e, level): def add_log(self, e, level):

View File

@ -3,7 +3,7 @@ from mitmproxy import controller
from mitmproxy.builtins import script from mitmproxy.builtins import script
from mitmproxy import options from mitmproxy import options
from mitmproxy import proxy from mitmproxy import proxy
from mitmproxy.flow import master from mitmproxy import master
import time import time
@ -16,7 +16,7 @@ class Thing:
class TestConcurrent(mastertest.MasterTest): class TestConcurrent(mastertest.MasterTest):
@tutils.skip_appveyor @tutils.skip_appveyor
def test_concurrent(self): def test_concurrent(self):
m = master.FlowMaster(options.Options(), proxy.DummyServer()) m = master.Master(options.Options(), proxy.DummyServer())
sc = script.Script( sc = script.Script(
tutils.test_data.path( tutils.test_data.path(
"data/addonscripts/concurrent_decorator.py" "data/addonscripts/concurrent_decorator.py"

View File

@ -1,6 +1,6 @@
from mitmproxy import addons from mitmproxy import addons
from mitmproxy import controller
from mitmproxy import options from mitmproxy import options
from mitmproxy import master
from mitmproxy import proxy from mitmproxy import proxy
@ -14,7 +14,7 @@ class TAddon:
def test_simple(): def test_simple():
o = options.Options() o = options.Options()
m = controller.Master(o, proxy.DummyServer(o)) m = master.Master(o, proxy.DummyServer(o))
a = addons.Addons(m) a = addons.Addons(m)
a.add(TAddon("one")) a.add(TAddon("one"))
assert a.get("one") assert a.get("one")

View File

@ -8,6 +8,7 @@ import queue
from mitmproxy.exceptions import Kill, ControlException from mitmproxy.exceptions import Kill, ControlException
from mitmproxy import proxy from mitmproxy import proxy
from mitmproxy import master
from netlib.tutils import raises from netlib.tutils import raises
@ -17,7 +18,7 @@ class TMsg:
class TestMaster: class TestMaster:
def test_simple(self): def test_simple(self):
class DummyMaster(controller.Master): class DummyMaster(master.Master):
@controller.handler @controller.handler
def log(self, _): def log(self, _):
m.should_exit.set() m.should_exit.set()
@ -35,7 +36,7 @@ class TestMaster:
assert m.should_exit.is_set() assert m.should_exit.is_set()
def test_server_simple(self): def test_server_simple(self):
m = controller.Master(None, proxy.DummyServer(None)) m = master.Master(None, proxy.DummyServer(None))
m.start() m.start()
m.shutdown() m.shutdown()
m.start() m.start()
@ -45,7 +46,7 @@ class TestMaster:
class TestServerThread: class TestServerThread:
def test_simple(self): def test_simple(self):
m = Mock() m = Mock()
t = controller.ServerThread(m) t = master.ServerThread(m)
t.run() t.run()
assert m.serve_forever.called assert m.serve_forever.called

View File

@ -6,7 +6,7 @@ from mitmproxy import options
from mitmproxy import contentviews from mitmproxy import contentviews
from mitmproxy import proxy from mitmproxy import proxy
from mitmproxy.builtins import script from mitmproxy.builtins import script
from mitmproxy.flow import master from mitmproxy import master
import netlib.utils import netlib.utils
@ -23,7 +23,7 @@ class ScriptError(Exception):
pass pass
class RaiseMaster(master.FlowMaster): class RaiseMaster(master.Master):
def add_log(self, e, level): def add_log(self, e, level):
if level in ("warn", "error"): if level in ("warn", "error"):
raise ScriptError(e) raise ScriptError(e)

View File

@ -14,6 +14,7 @@ from mitmproxy.models import HTTPResponse
from mitmproxy.proxy import ProxyConfig from mitmproxy.proxy import ProxyConfig
from mitmproxy.proxy.server import DummyServer from mitmproxy.proxy.server import DummyServer
from mitmproxy.models.connections import ClientConnection from mitmproxy.models.connections import ClientConnection
from mitmproxy import master
from . import tutils from . import tutils
@ -110,7 +111,7 @@ class TestHTTPFlow:
def test_killall(self): def test_killall(self):
srv = DummyServer(None) srv = DummyServer(None)
s = flow.State() s = flow.State()
fm = flow.FlowMaster(None, srv) fm = master.Master(None, srv)
fm.addons.add(s) fm.addons.add(s)
f = tutils.tflow() f = tutils.tflow()
@ -363,7 +364,7 @@ class TestSerialize:
def test_load_flows(self): def test_load_flows(self):
r = self._treader() r = self._treader()
s = flow.State() s = flow.State()
fm = flow.FlowMaster(None, DummyServer()) fm = master.Master(None, DummyServer())
fm.addons.add(s) fm.addons.add(s)
fm.load_flows(r) fm.load_flows(r)
assert len(s.flows) == 6 assert len(s.flows) == 6
@ -376,7 +377,7 @@ class TestSerialize:
upstream_server="https://use-this-domain" upstream_server="https://use-this-domain"
) )
conf = ProxyConfig(opts) conf = ProxyConfig(opts)
fm = flow.FlowMaster(opts, DummyServer(conf)) fm = master.Master(opts, DummyServer(conf))
fm.addons.add(s) fm.addons.add(s)
fm.load_flows(r) fm.load_flows(r)
assert s.flows[0].request.host == "use-this-domain" assert s.flows[0].request.host == "use-this-domain"
@ -423,7 +424,7 @@ class TestSerialize:
class TestFlowMaster: class TestFlowMaster:
def test_replay(self): def test_replay(self):
fm = flow.FlowMaster(None, DummyServer()) fm = master.Master(None, DummyServer())
f = tutils.tflow(resp=True) f = tutils.tflow(resp=True)
f.request.content = None f.request.content = None
tutils.raises("missing", fm.replay_request, f) tutils.raises("missing", fm.replay_request, f)
@ -435,12 +436,12 @@ class TestFlowMaster:
tutils.raises("live", fm.replay_request, f) tutils.raises("live", fm.replay_request, f)
def test_create_flow(self): def test_create_flow(self):
fm = flow.FlowMaster(None, DummyServer()) fm = master.Master(None, DummyServer())
assert fm.create_request("GET", "http", "example.com", 80, "/") assert fm.create_request("GET", "http", "example.com", 80, "/")
def test_all(self): def test_all(self):
s = flow.State() s = flow.State()
fm = flow.FlowMaster(None, DummyServer()) fm = master.Master(None, DummyServer())
fm.addons.add(s) fm.addons.add(s)
f = tutils.tflow(req=None) f = tutils.tflow(req=None)
fm.clientconnect(f.client_conn) fm.clientconnect(f.client_conn)

View File

@ -1,12 +1,13 @@
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import proxy from mitmproxy import proxy
from mitmproxy import master
from . import tutils from . import tutils
class TestState: class TestState:
def test_duplicate_flow(self): def test_duplicate_flow(self):
s = flow.State() s = flow.State()
fm = flow.FlowMaster(None, proxy.DummyServer()) fm = master.Master(None, proxy.DummyServer())
fm.addons.add(s) fm.addons.add(s)
f = tutils.tflow(resp=True) f = tutils.tflow(resp=True)
fm.load_flow(f) fm.load_flow(f)

View File

@ -1,13 +1,14 @@
import tornado.testing import tornado.testing
from mitmproxy import proxy from mitmproxy import proxy
from mitmproxy.web import app, master from mitmproxy.web import app
from mitmproxy.web import master as webmaster
class TestApp(tornado.testing.AsyncHTTPTestCase): class TestApp(tornado.testing.AsyncHTTPTestCase):
def get_app(self): def get_app(self):
o = master.Options() o = webmaster.Options()
m = master.WebMaster(o, proxy.DummyServer()) m = webmaster.WebMaster(o, proxy.DummyServer())
return app.Application(m, None, None) return app.Application(m, None, None)
def test_index(self): def test_index(self):

View File

@ -6,18 +6,19 @@ import sys
from mitmproxy.proxy.config import ProxyConfig from mitmproxy.proxy.config import ProxyConfig
from mitmproxy.proxy.server import ProxyServer from mitmproxy.proxy.server import ProxyServer
from mitmproxy import master
from mitmproxy.flow import state from mitmproxy.flow import state
import pathod.test import pathod.test
import pathod.pathoc import pathod.pathoc
from mitmproxy import flow, controller, options from mitmproxy import controller, options
import netlib.exceptions import netlib.exceptions
class TestMaster(flow.FlowMaster): class TestMaster(master.Master):
def __init__(self, opts, config): def __init__(self, opts, config):
s = ProxyServer(config) s = ProxyServer(config)
flow.FlowMaster.__init__(self, opts, s) master.Master.__init__(self, opts, s)
def clear_addons(self, addons): def clear_addons(self, addons):
self.addons.clear() self.addons.clear()