diff options
31 files changed, 241 insertions, 232 deletions
diff --git a/examples/flowbasic b/examples/flowbasic index 67c6f596..0eb163a4 100755 --- a/examples/flowbasic +++ b/examples/flowbasic @@ -12,10 +12,10 @@ from mitmproxy import flow, controller, options  from mitmproxy.proxy import ProxyServer, ProxyConfig -class MyMaster(flow.FlowMaster): +class MyMaster(master.Master):      def run(self):          try: -            flow.FlowMaster.run(self) +            master.Master.run(self)          except KeyboardInterrupt:              self.shutdown() diff --git a/examples/stickycookies b/examples/stickycookies index 43e5371d..a0ee90ff 100755 --- a/examples/stickycookies +++ b/examples/stickycookies @@ -10,14 +10,14 @@ from mitmproxy import controller, proxy  from mitmproxy.proxy.server import ProxyServer -class StickyMaster(controller.Master): +class StickyMaster(master.Master):      def __init__(self, server): -        controller.Master.__init__(self, server) +        master.Master.__init__(self, server)          self.stickyhosts = {}      def run(self):          try: -            return controller.Master.run(self) +            return master.Master.run(self)          except KeyboardInterrupt:              self.shutdown() diff --git a/mitmproxy/builtins/script.py b/mitmproxy/builtins/script.py index b409dff9..d8ed0e39 100644 --- a/mitmproxy/builtins/script.py +++ b/mitmproxy/builtins/script.py @@ -6,9 +6,8 @@ import threading  import traceback  from mitmproxy import exceptions -from mitmproxy import controller  from mitmproxy import ctx -from mitmproxy.flow import master as flowmaster +from mitmproxy import events  import watchdog.events @@ -154,7 +153,7 @@ class Script:          self.last_options = None          self.should_reload = threading.Event() -        for i in controller.Events: +        for i in events.Events:              if not hasattr(self, i):                  def mkprox():                      evt = i @@ -221,7 +220,7 @@ class ScriptLoader:          sc = Script(command)          sc.load_script()          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.done()          return sc diff --git a/mitmproxy/console/master.py b/mitmproxy/console/master.py index b3752df9..b19c62a2 100644 --- a/mitmproxy/console/master.py +++ b/mitmproxy/console/master.py @@ -18,6 +18,7 @@ from mitmproxy import builtins  from mitmproxy import contentviews  from mitmproxy import controller  from mitmproxy import exceptions +from mitmproxy import master  from mitmproxy import flow  from mitmproxy import flowfilter  from mitmproxy import utils @@ -218,11 +219,11 @@ class Options(mitmproxy.options.Options):          super().__init__(**kwargs) -class ConsoleMaster(flow.FlowMaster): +class ConsoleMaster(master.Master):      palette = []      def __init__(self, options, server): -        flow.FlowMaster.__init__(self, options, server) +        master.Master.__init__(self, options, server)          self.state = ConsoleState()          self.stream_path = None          # This line is just for type hinting @@ -600,7 +601,7 @@ class ConsoleMaster(flow.FlowMaster):      def load_flows_path(self, path):          reterr = None          try: -            flow.FlowMaster.load_flows_file(self, path) +            master.Master.load_flows_file(self, path)          except exceptions.FlowReadException as e:              reterr = str(e)          signals.flowlist_change.send(self) @@ -631,7 +632,7 @@ class ConsoleMaster(flow.FlowMaster):      def shutdown(self):          self.state.killall(self) -        flow.FlowMaster.shutdown(self) +        master.Master.shutdown(self)      def clear_flows(self):          self.state.clear() diff --git a/mitmproxy/controller.py b/mitmproxy/controller.py index a88500a8..27fb4a0c 100644 --- a/mitmproxy/controller.py +++ b/mitmproxy/controller.py @@ -1,45 +1,6 @@  import functools -import threading -import contextlib  import queue - -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", -]) +from mitmproxy import exceptions  class LogEntry: @@ -83,101 +44,6 @@ class Log:          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:      """          The only way for the proxy server to communicate with the master diff --git a/mitmproxy/ctx.py b/mitmproxy/ctx.py index a54a2f21..4ecfe79b 100644 --- a/mitmproxy/ctx.py +++ b/mitmproxy/ctx.py @@ -1,2 +1,2 @@ -master = None  # type: "mitmproxy.flow.FlowMaster" +master = None  # type: "mitmproxy.master.Master"  log = None  # type: "mitmproxy.controller.Log" diff --git a/mitmproxy/dump.py b/mitmproxy/dump.py index e2167259..4731686c 100644 --- a/mitmproxy/dump.py +++ b/mitmproxy/dump.py @@ -6,6 +6,7 @@ from mitmproxy import exceptions  from mitmproxy import flow  from mitmproxy import builtins  from mitmproxy import options +from mitmproxy import master  from mitmproxy.builtins import dumper, termlog  from netlib import tcp @@ -30,10 +31,10 @@ class Options(options.Options):          super().__init__(**kwargs) -class DumpMaster(flow.FlowMaster): +class DumpMaster(master.Master):      def __init__(self, options, server): -        flow.FlowMaster.__init__(self, options, server) +        master.Master.__init__(self, options, server)          self.has_errored = False          self.addons.add(termlog.TermLog())          self.addons.add(*builtins.default_addons()) diff --git a/mitmproxy/events.py b/mitmproxy/events.py new file mode 100644 index 00000000..0e512aae --- /dev/null +++ b/mitmproxy/events.py @@ -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 diff --git a/mitmproxy/flow/__init__.py b/mitmproxy/flow/__init__.py index 8d7c26cc..2688966a 100644 --- a/mitmproxy/flow/__init__.py +++ b/mitmproxy/flow/__init__.py @@ -1,10 +1,9 @@  from mitmproxy.flow import export  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  __all__ = [      "export",      "FlowWriter", "FilteredFlowWriter", "FlowReader", "read_flows_from_paths", -    "FlowMaster", "State", "FlowView", +    "State", "FlowView",  ] diff --git a/mitmproxy/flow/master.py b/mitmproxy/master.py index e02540b1..b6286b89 100644 --- a/mitmproxy/flow/master.py +++ b/mitmproxy/master.py @@ -1,40 +1,117 @@  import os +import threading +import contextlib +import queue  import sys -from netlib import http +from mitmproxy import addons +from mitmproxy import options  from mitmproxy import controller +from mitmproxy import events  from mitmproxy import exceptions  from mitmproxy import models  from mitmproxy.flow import io  from mitmproxy.protocol import http_replay +from netlib import basethread +from netlib import http +from . import ctx as mitmproxy_ctx + + +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 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 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 - - -class FlowMaster(controller.Master):      def create_request(self, method, scheme, host, port, path):          """              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.scheme = self.server.config.upstream_server.scheme          f.reply = controller.DummyReply() -        for e, o in event_sequence(f): +        for e, o in events.event_sequence(f):              getattr(self, e)(o)      def load_flows(self, fr): diff --git a/mitmproxy/script/concurrent.py b/mitmproxy/script/concurrent.py index 11b2144e..dc72e5b7 100644 --- a/mitmproxy/script/concurrent.py +++ b/mitmproxy/script/concurrent.py @@ -3,7 +3,7 @@ This module provides a @concurrent decorator primitive to  offload computations from mitmproxy's main master thread.  """ -from mitmproxy import controller +from mitmproxy import events  from netlib import basethread @@ -12,7 +12,7 @@ class ScriptThread(basethread.BaseThread):  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(              "Concurrent decorator not supported for '%s' method." % fn.__name__          ) diff --git a/mitmproxy/web/master.py b/mitmproxy/web/master.py index 235ec70d..1f0dc709 100644 --- a/mitmproxy/web/master.py +++ b/mitmproxy/web/master.py @@ -11,6 +11,7 @@ from mitmproxy import controller  from mitmproxy import exceptions  from mitmproxy import flow  from mitmproxy import options +from mitmproxy import master  from mitmproxy.web import app  from netlib.http import authentication @@ -130,7 +131,7 @@ class Options(options.Options):              self.wauthenticator = None -class WebMaster(flow.FlowMaster): +class WebMaster(master.Master):      def __init__(self, options, server):          super().__init__(options, server) diff --git a/test/mitmproxy/builtins/test_anticache.py b/test/mitmproxy/builtins/test_anticache.py index df73bb1b..790ae97d 100644 --- a/test/mitmproxy/builtins/test_anticache.py +++ b/test/mitmproxy/builtins/test_anticache.py @@ -1,6 +1,6 @@  from .. import tutils, mastertest  from mitmproxy.builtins import anticache -from mitmproxy.flow import master +from mitmproxy import master  from mitmproxy import options  from mitmproxy import proxy @@ -8,7 +8,7 @@ from mitmproxy import proxy  class TestAntiCache(mastertest.MasterTest):      def test_simple(self):          o = options.Options(anticache = True) -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sa = anticache.AntiCache()          m.addons.add(sa) diff --git a/test/mitmproxy/builtins/test_anticomp.py b/test/mitmproxy/builtins/test_anticomp.py index 84618ba6..d5a0d6eb 100644 --- a/test/mitmproxy/builtins/test_anticomp.py +++ b/test/mitmproxy/builtins/test_anticomp.py @@ -1,6 +1,6 @@  from .. import tutils, mastertest  from mitmproxy.builtins import anticomp -from mitmproxy.flow import master +from mitmproxy import master  from mitmproxy import options  from mitmproxy import proxy @@ -8,7 +8,7 @@ from mitmproxy import proxy  class TestAntiComp(mastertest.MasterTest):      def test_simple(self):          o = options.Options(anticomp = True) -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sa = anticomp.AntiComp()          m.addons.add(sa) diff --git a/test/mitmproxy/builtins/test_clientplayback.py b/test/mitmproxy/builtins/test_clientplayback.py index 15702340..e2c5be7e 100644 --- a/test/mitmproxy/builtins/test_clientplayback.py +++ b/test/mitmproxy/builtins/test_clientplayback.py @@ -25,7 +25,7 @@ class TestClientPlayback:          cp.keepserving = False          cp.flows = 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():                  cp.tick()              sd.assert_called() diff --git a/test/mitmproxy/builtins/test_filestreamer.py b/test/mitmproxy/builtins/test_filestreamer.py index 31e607b5..35fe1ca2 100644 --- a/test/mitmproxy/builtins/test_filestreamer.py +++ b/test/mitmproxy/builtins/test_filestreamer.py @@ -3,7 +3,8 @@ from .. import tutils, mastertest  import os.path  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 proxy @@ -14,13 +15,13 @@ class TestStream(mastertest.MasterTest):              p = os.path.join(tdir, "foo")              def r(): -                r = FlowReader(open(p, "rb")) +                r = io.FlowReader(open(p, "rb"))                  return list(r.stream())              o = options.Options(                  outfile = (p, "wb")              ) -            m = master.FlowMaster(o, proxy.DummyServer()) +            m = master.Master(o, proxy.DummyServer())              sa = filestreamer.FileStreamer()              m.addons.add(sa) diff --git a/test/mitmproxy/builtins/test_replace.py b/test/mitmproxy/builtins/test_replace.py index 98e2e169..35f3d430 100644 --- a/test/mitmproxy/builtins/test_replace.py +++ b/test/mitmproxy/builtins/test_replace.py @@ -1,6 +1,6 @@  from .. import tutils, mastertest, tservers  from mitmproxy.builtins import replace -from mitmproxy.flow import master +from mitmproxy import master  from mitmproxy import options  from mitmproxy import proxy @@ -36,7 +36,7 @@ class TestReplace(mastertest.MasterTest):                  ("~s", "foo", "bar"),              ]          ) -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sa = replace.Replace()          m.addons.add(sa) diff --git a/test/mitmproxy/builtins/test_script.py b/test/mitmproxy/builtins/test_script.py index 331a7fa2..ab4343b5 100644 --- a/test/mitmproxy/builtins/test_script.py +++ b/test/mitmproxy/builtins/test_script.py @@ -8,7 +8,7 @@ from mitmproxy import exceptions  from mitmproxy import options  from mitmproxy import proxy  from mitmproxy.builtins import script -from mitmproxy.flow import master +from mitmproxy import master  from .. import tutils, mastertest @@ -58,7 +58,7 @@ def test_load_script():  class TestScript(mastertest.MasterTest):      def test_simple(self):          o = options.Options() -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sc = script.Script(              tutils.test_data.path(                  "data/addonscripts/recorder.py" @@ -112,7 +112,7 @@ class TestScript(mastertest.MasterTest):      def test_addon(self):          o = options.Options() -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sc = script.Script(              tutils.test_data.path(                  "data/addonscripts/addon.py" @@ -145,7 +145,7 @@ class TestCutTraceback:  class TestScriptLoader(mastertest.MasterTest):      def test_run_once(self):          o = options.Options(scripts=[]) -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sl = script.ScriptLoader()          m.addons.add(sl) @@ -169,7 +169,7 @@ class TestScriptLoader(mastertest.MasterTest):      def test_simple(self):          o = options.Options(scripts=[]) -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sc = script.ScriptLoader()          m.addons.add(sc)          assert len(m.addons) == 1 @@ -184,7 +184,7 @@ class TestScriptLoader(mastertest.MasterTest):      def test_dupes(self):          o = options.Options(scripts=["one", "one"]) -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sc = script.ScriptLoader()          tutils.raises(exceptions.OptionsError, m.addons.add, o, sc) diff --git a/test/mitmproxy/builtins/test_stickyauth.py b/test/mitmproxy/builtins/test_stickyauth.py index 3331a82e..22523548 100644 --- a/test/mitmproxy/builtins/test_stickyauth.py +++ b/test/mitmproxy/builtins/test_stickyauth.py @@ -1,6 +1,6 @@  from .. import tutils, mastertest  from mitmproxy.builtins import stickyauth -from mitmproxy.flow import master +from mitmproxy import master  from mitmproxy import options  from mitmproxy import proxy @@ -8,7 +8,7 @@ from mitmproxy import proxy  class TestStickyAuth(mastertest.MasterTest):      def test_simple(self):          o = options.Options(stickyauth = ".*") -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sa = stickyauth.StickyAuth()          m.addons.add(sa) diff --git a/test/mitmproxy/builtins/test_stickycookie.py b/test/mitmproxy/builtins/test_stickycookie.py index d1a535cf..c70b03d8 100644 --- a/test/mitmproxy/builtins/test_stickycookie.py +++ b/test/mitmproxy/builtins/test_stickycookie.py @@ -1,6 +1,6 @@  from .. import tutils, mastertest  from mitmproxy.builtins import stickycookie -from mitmproxy.flow import master +from mitmproxy import master  from mitmproxy import options  from mitmproxy import proxy  from netlib import tutils as ntutils @@ -14,7 +14,7 @@ def test_domain_match():  class TestStickyCookie(mastertest.MasterTest):      def mk(self):          o = options.Options(stickycookie = ".*") -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sc = stickycookie.StickyCookie()          m.addons.add(sc)          return m, sc diff --git a/test/mitmproxy/builtins/test_streambodies.py b/test/mitmproxy/builtins/test_streambodies.py index 6ff86048..4a8c2474 100644 --- a/test/mitmproxy/builtins/test_streambodies.py +++ b/test/mitmproxy/builtins/test_streambodies.py @@ -1,5 +1,5 @@  from .. import tutils, mastertest -from mitmproxy.flow import master +from mitmproxy import master  from mitmproxy import options  from mitmproxy import proxy @@ -9,7 +9,7 @@ from mitmproxy.builtins import streambodies  class TestStreamBodies(mastertest.MasterTest):      def test_simple(self):          o = options.Options(stream_large_bodies = 10) -        m = master.FlowMaster(o, proxy.DummyServer()) +        m = master.Master(o, proxy.DummyServer())          sa = streambodies.StreamBodies()          m.addons.add(sa) diff --git a/test/mitmproxy/data/addonscripts/recorder.py b/test/mitmproxy/data/addonscripts/recorder.py index 3d7a51bf..5be88e5c 100644 --- a/test/mitmproxy/data/addonscripts/recorder.py +++ b/test/mitmproxy/data/addonscripts/recorder.py @@ -1,4 +1,5 @@  from mitmproxy import controller +from mitmproxy import events  from mitmproxy import ctx  import sys @@ -10,7 +11,7 @@ class CallLogger:          self.name = name      def __getattr__(self, attr): -        if attr in controller.Events: +        if attr in events.Events:              def prox(*args, **kwargs):                  lg = (self.name, attr, args, kwargs)                  if attr != "log": diff --git a/test/mitmproxy/mastertest.py b/test/mitmproxy/mastertest.py index ae11e577..915f9501 100644 --- a/test/mitmproxy/mastertest.py +++ b/test/mitmproxy/mastertest.py @@ -3,7 +3,7 @@ import contextlib  from . import tutils  import netlib.tutils -from mitmproxy.flow import master +from mitmproxy import master  from mitmproxy import flow, proxy, models, options @@ -39,9 +39,9 @@ class MasterTest:          f.close() -class RecordingMaster(master.FlowMaster): +class RecordingMaster(master.Master):      def __init__(self, *args, **kwargs): -        master.FlowMaster.__init__(self, *args, **kwargs) +        master.Master.__init__(self, *args, **kwargs)          self.event_log = []      def add_log(self, e, level): diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py index 6b1d4a12..bebd8dea 100644 --- a/test/mitmproxy/script/test_concurrent.py +++ b/test/mitmproxy/script/test_concurrent.py @@ -3,7 +3,7 @@ from mitmproxy import controller  from mitmproxy.builtins import script  from mitmproxy import options  from mitmproxy import proxy -from mitmproxy.flow import master +from mitmproxy import master  import time @@ -16,7 +16,7 @@ class Thing:  class TestConcurrent(mastertest.MasterTest):      @tutils.skip_appveyor      def test_concurrent(self): -        m = master.FlowMaster(options.Options(), proxy.DummyServer()) +        m = master.Master(options.Options(), proxy.DummyServer())          sc = script.Script(              tutils.test_data.path(                  "data/addonscripts/concurrent_decorator.py" diff --git a/test/mitmproxy/test_addons.py b/test/mitmproxy/test_addons.py index 22d22c85..dcf14398 100644 --- a/test/mitmproxy/test_addons.py +++ b/test/mitmproxy/test_addons.py @@ -1,6 +1,6 @@  from mitmproxy import addons -from mitmproxy import controller  from mitmproxy import options +from mitmproxy import master  from mitmproxy import proxy @@ -14,7 +14,7 @@ class TAddon:  def test_simple():      o = options.Options() -    m = controller.Master(o, proxy.DummyServer(o)) +    m = master.Master(o, proxy.DummyServer(o))      a = addons.Addons(m)      a.add(TAddon("one"))      assert a.get("one") diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py index b7a8a17f..071638a9 100644 --- a/test/mitmproxy/test_controller.py +++ b/test/mitmproxy/test_controller.py @@ -8,6 +8,7 @@ import queue  from mitmproxy.exceptions import Kill, ControlException  from mitmproxy import proxy +from mitmproxy import master  from netlib.tutils import raises @@ -17,7 +18,7 @@ class TMsg:  class TestMaster:      def test_simple(self): -        class DummyMaster(controller.Master): +        class DummyMaster(master.Master):              @controller.handler              def log(self, _):                  m.should_exit.set() @@ -35,7 +36,7 @@ class TestMaster:          assert m.should_exit.is_set()      def test_server_simple(self): -        m = controller.Master(None, proxy.DummyServer(None)) +        m = master.Master(None, proxy.DummyServer(None))          m.start()          m.shutdown()          m.start() @@ -45,7 +46,7 @@ class TestMaster:  class TestServerThread:      def test_simple(self):          m = Mock() -        t = controller.ServerThread(m) +        t = master.ServerThread(m)          t.run()          assert m.serve_forever.called diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 5950fb60..ee5080e8 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -6,7 +6,7 @@ from mitmproxy import options  from mitmproxy import contentviews  from mitmproxy import proxy  from mitmproxy.builtins import script -from mitmproxy.flow import master +from mitmproxy import master  import netlib.utils @@ -23,7 +23,7 @@ class ScriptError(Exception):      pass -class RaiseMaster(master.FlowMaster): +class RaiseMaster(master.Master):      def add_log(self, e, level):          if level in ("warn", "error"):              raise ScriptError(e) diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index ab181357..2b387f5c 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -14,6 +14,7 @@ from mitmproxy.models import HTTPResponse  from mitmproxy.proxy import ProxyConfig  from mitmproxy.proxy.server import DummyServer  from mitmproxy.models.connections import ClientConnection +from mitmproxy import master  from . import tutils @@ -110,7 +111,7 @@ class TestHTTPFlow:      def test_killall(self):          srv = DummyServer(None)          s = flow.State() -        fm = flow.FlowMaster(None, srv) +        fm = master.Master(None, srv)          fm.addons.add(s)          f = tutils.tflow() @@ -363,7 +364,7 @@ class TestSerialize:      def test_load_flows(self):          r = self._treader()          s = flow.State() -        fm = flow.FlowMaster(None, DummyServer()) +        fm = master.Master(None, DummyServer())          fm.addons.add(s)          fm.load_flows(r)          assert len(s.flows) == 6 @@ -376,7 +377,7 @@ class TestSerialize:              upstream_server="https://use-this-domain"          )          conf = ProxyConfig(opts) -        fm = flow.FlowMaster(opts, DummyServer(conf)) +        fm = master.Master(opts, DummyServer(conf))          fm.addons.add(s)          fm.load_flows(r)          assert s.flows[0].request.host == "use-this-domain" @@ -423,7 +424,7 @@ class TestSerialize:  class TestFlowMaster:      def test_replay(self): -        fm = flow.FlowMaster(None, DummyServer()) +        fm = master.Master(None, DummyServer())          f = tutils.tflow(resp=True)          f.request.content = None          tutils.raises("missing", fm.replay_request, f) @@ -435,12 +436,12 @@ class TestFlowMaster:          tutils.raises("live", fm.replay_request, f)      def test_create_flow(self): -        fm = flow.FlowMaster(None, DummyServer()) +        fm = master.Master(None, DummyServer())          assert fm.create_request("GET", "http", "example.com", 80, "/")      def test_all(self):          s = flow.State() -        fm = flow.FlowMaster(None, DummyServer()) +        fm = master.Master(None, DummyServer())          fm.addons.add(s)          f = tutils.tflow(req=None)          fm.clientconnect(f.client_conn) diff --git a/test/mitmproxy/test_flow_state.py b/test/mitmproxy/test_flow_state.py index 02582f50..05f4cbb4 100644 --- a/test/mitmproxy/test_flow_state.py +++ b/test/mitmproxy/test_flow_state.py @@ -1,12 +1,13 @@  from mitmproxy import flow  from mitmproxy import proxy +from mitmproxy import master  from . import tutils  class TestState:      def test_duplicate_flow(self):          s = flow.State() -        fm = flow.FlowMaster(None, proxy.DummyServer()) +        fm = master.Master(None, proxy.DummyServer())          fm.addons.add(s)          f = tutils.tflow(resp=True)          fm.load_flow(f) diff --git a/test/mitmproxy/test_web_app.py b/test/mitmproxy/test_web_app.py index 21cd1ab2..89354960 100644 --- a/test/mitmproxy/test_web_app.py +++ b/test/mitmproxy/test_web_app.py @@ -1,13 +1,14 @@  import tornado.testing  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):      def get_app(self): -        o = master.Options() -        m = master.WebMaster(o, proxy.DummyServer()) +        o = webmaster.Options() +        m = webmaster.WebMaster(o, proxy.DummyServer())          return app.Application(m, None, None)      def test_index(self): diff --git a/test/mitmproxy/tservers.py b/test/mitmproxy/tservers.py index b09a6cf9..254af2f0 100644 --- a/test/mitmproxy/tservers.py +++ b/test/mitmproxy/tservers.py @@ -6,18 +6,19 @@ import sys  from mitmproxy.proxy.config import ProxyConfig  from mitmproxy.proxy.server import ProxyServer +from mitmproxy import master  from mitmproxy.flow import state  import pathod.test  import pathod.pathoc -from mitmproxy import flow, controller, options +from mitmproxy import controller, options  import netlib.exceptions -class TestMaster(flow.FlowMaster): +class TestMaster(master.Master):      def __init__(self, opts, config):          s = ProxyServer(config) -        flow.FlowMaster.__init__(self, opts, s) +        master.Master.__init__(self, opts, s)      def clear_addons(self, addons):          self.addons.clear()  | 
