Пример #1
0
    def __init__(self, opts):
        super().__init__(opts)

        self.start_err: typing.Optional[log.LogEntry] = None

        self.view: view.View = view.View()
        self.events = eventstore.EventStore()
        self.events.sig_add.connect(self.sig_add_log)

        self.stream_path = None
        self.keymap = keymap.Keymap(self)
        defaultkeys.map(self.keymap)
        self.options.errored.connect(self.options_error)

        self.view_stack = []

        self.addons.add(*addons.default_addons())
        self.addons.add(
            intercept.Intercept(),
            self.view,
            self.events,
            readfile.ReadFile(),
            consoleaddons.ConsoleAddon(self),
            keymap.KeymapConfig(),
        )

        def sigint_handler(*args, **kwargs):
            self.prompt_for_exit()

        signal.signal(signal.SIGINT, sigint_handler)

        self.window = None
Пример #2
0
 def test_configure(self):
     rf = readfile.ReadFile()
     with taddons.context(rf) as tctx:
         tctx.configure(rf, readfile_filter="~q")
         with pytest.raises(Exception, match="Invalid filter expression"):
             tctx.configure(rf, readfile_filter="~~")
         tctx.configure(rf, readfile_filter="")
Пример #3
0
    def __init__(self, options, server, with_termlog=True):
        super().__init__(options, server)
        self.view = view.View()
        self.view.sig_view_add.connect(self._sig_view_add)
        self.view.sig_view_remove.connect(self._sig_view_remove)
        self.view.sig_view_update.connect(self._sig_view_update)
        self.view.sig_view_refresh.connect(self._sig_view_refresh)

        self.events = eventstore.EventStore()
        self.events.sig_add.connect(self._sig_events_add)
        self.events.sig_refresh.connect(self._sig_events_refresh)

        self.options.changed.connect(self._sig_options_update)
        self.options.changed.connect(self._sig_settings_update)

        self.addons.add(*addons.default_addons())
        self.addons.add(
            intercept.Intercept(),
            readfile.ReadFile(),
            self.view,
            self.events,
        )
        if with_termlog:
            self.addons.add(termlog.TermLog(), termstatus.TermStatus())
        self.app = app.Application(self, self.options.web_debug)
        # This line is just for type hinting
        self.options = self.options  # type: Options
Пример #4
0
    def __init__(self, options, with_termlog=True):
        super().__init__(options)
        self.view = view.View()
        self.view.sig_view_add.connect(self._sig_view_add)
        self.view.sig_view_remove.connect(self._sig_view_remove)
        self.view.sig_view_update.connect(self._sig_view_update)
        self.view.sig_view_refresh.connect(self._sig_view_refresh)

        self.events = eventstore.EventStore()
        self.events.sig_add.connect(self._sig_events_add)
        self.events.sig_refresh.connect(self._sig_events_refresh)

        self.options.changed.connect(self._sig_options_update)
        self.options.changed.connect(self._sig_settings_update)

        self.addons.add(*addons.default_addons())
        self.addons.add(
            webaddons.WebAddon(),
            intercept.Intercept(),
            readfile.ReadFile(),
            static_viewer.StaticViewer(),
            self.view,
            self.events,
        )
        if with_termlog:
            self.addons.add(termlog.TermLog(), termstatus.TermStatus())
        self.app = app.Application(
            self, self.options.web_debug
        )
Пример #5
0
    def __init__(self, opts):
        super().__init__(opts)

        self.view = view.View()  # type: view.View
        self.stream_path = None
        self.keymap = keymap.Keymap(self)
        defaultkeys.map(self.keymap)
        self.options.errored.connect(self.options_error)

        self.view_stack = []

        signals.call_in.connect(self.sig_call_in)
        signals.sig_add_log.connect(self.sig_add_log)
        self.addons.add(consoleaddons.Logger())
        self.addons.add(*addons.default_addons())
        self.addons.add(
            intercept.Intercept(),
            self.view,
            consoleaddons.UnsupportedLog(),
            readfile.ReadFile(),
            consoleaddons.ConsoleAddon(self),
        )

        def sigint_handler(*args, **kwargs):
            self.prompt_for_exit()

        signal.signal(signal.SIGINT, sigint_handler)

        self.window = None
Пример #6
0
    def __init__(self, options, server):
        super().__init__(options, server)
        self.view = view.View()  # type: view.View
        self.view.sig_view_update.connect(signals.flow_change.send)
        self.stream_path = None
        # This line is just for type hinting
        self.options = self.options  # type: Options
        self.options.errored.connect(self.options_error)

        self.logbuffer = urwid.SimpleListWalker([])

        self.view_stack = []

        signals.call_in.connect(self.sig_call_in)
        signals.pop_view_state.connect(self.sig_pop_view_state)
        signals.replace_view_state.connect(self.sig_replace_view_state)
        signals.push_view_state.connect(self.sig_push_view_state)
        signals.sig_add_log.connect(self.sig_add_log)
        self.addons.add(Logger())
        self.addons.add(*addons.default_addons())
        self.addons.add(
            intercept.Intercept(),
            self.view,
            UnsupportedLog(),
            readfile.ReadFile(),
        )

        def sigint_handler(*args, **kwargs):
            self.prompt_for_exit()

        signal.signal(signal.SIGINT, sigint_handler)
Пример #7
0
    def __init__(self, opts):
        super().__init__(opts)

        self.view: view.View = view.View()
        self.events = eventstore.EventStore()
        self.events.sig_add.connect(self.sig_add_log)

        self.stream_path = None
        self.keymap = keymap.Keymap(self)
        defaultkeys.map(self.keymap)
        self.options.errored.connect(self.options_error)

        self.view_stack = []

        self.addons.add(*addons.default_addons())
        self.addons.add(
            intercept.Intercept(),
            self.view,
            self.events,
            readfile.ReadFile(),
            consoleaddons.ConsoleAddon(self),
            keymap.KeymapConfig(),
            errorcheck.ErrorCheck(log_to_stderr=True),
        )

        self.window = None
Пример #8
0
    async def test_corrupt(self, corrupt_data):
        rf = readfile.ReadFile()
        with taddons.context(rf) as tctx:
            with pytest.raises(exceptions.FlowReadException):
                await rf.load_flows(io.BytesIO(b"qibble"))

            tctx.master.clear()
            with pytest.raises(exceptions.FlowReadException):
                await rf.load_flows(corrupt_data)
            assert await tctx.master.await_log("file corrupted")
Пример #9
0
    def test_corrupt(self, mck, corrupt_data):
        rf = readfile.ReadFile()
        with taddons.context() as tctx:
            with pytest.raises(exceptions.FlowReadException):
                rf.load_flows(io.BytesIO(b"qibble"))
            assert not mck.called
            assert len(tctx.master.logs) == 1

            with pytest.raises(exceptions.FlowReadException):
                rf.load_flows(corrupt_data)
            assert mck.called
            assert len(tctx.master.logs) == 2
Пример #10
0
async def test_static_viewer(tmpdir):
    s = static_viewer.StaticViewer()
    rf = readfile.ReadFile()
    sa = save.Save()
    with taddons.context(rf) as tctx:
        sa.save([tflow.tflow(resp=True)], str(tmpdir.join('foo')))
        tctx.master.addons.add(s)
        tctx.configure(s,
                       web_static_viewer=str(tmpdir),
                       rfile=str(tmpdir.join('foo')))
        assert tmpdir.join('index.html').check(file=1)
        assert tmpdir.join('static').check(dir=1)
        assert tmpdir.join('flows').check(dir=1)
Пример #11
0
    async def test_corrupt(self, corrupt_data):
        rf = readfile.ReadFile()
        with taddons.context(rf) as tctx:
            with mock.patch('mitmproxy.master.Master.load_flow') as mck:
                with pytest.raises(exceptions.FlowReadException):
                    rf.load_flows(io.BytesIO(b"qibble"))
                assert not mck.called

                tctx.master.clear()
                with pytest.raises(exceptions.FlowReadException):
                    rf.load_flows(corrupt_data)
                assert await tctx.master.await_log("file corrupted")
                assert mck.called
Пример #12
0
    def test_configure(self, mck, tmpdir, data, corrupt_data):
        rf = readfile.ReadFile()
        with taddons.context() as tctx:
            tf = tmpdir.join("tfile")

            tf.write(data.getvalue())
            tctx.configure(rf, rfile=str(tf))
            assert not mck.called
            rf.running()
            assert mck.called

            tf.write(corrupt_data.getvalue())
            tctx.configure(rf, rfile=str(tf))
            with pytest.raises(exceptions.OptionsError):
                rf.running()
Пример #13
0
def test_configure(mck, tmpdir):

    rf = readfile.ReadFile()
    with taddons.context() as tctx:
        tf = str(tmpdir.join("tfile"))
        write_data(tf)
        tctx.configure(rf, rfile=str(tf))
        assert not mck.called
        rf.running()
        assert mck.called

        write_data(tf, corrupt=True)
        tctx.configure(rf, rfile=str(tf))
        with pytest.raises(exceptions.OptionsError):
            rf.running()
Пример #14
0
def test_corruption(mck, tmpdir):

    rf = readfile.ReadFile()
    with taddons.context() as tctx:
        with pytest.raises(exceptions.FlowReadException):
            rf.load_flows_file("nonexistent")
        assert not mck.called
        assert len(tctx.master.logs) == 1

        tfc = str(tmpdir.join("tfile"))
        write_data(tfc, corrupt=True)

        with pytest.raises(exceptions.FlowReadException):
            rf.load_flows_file(tfc)
        assert mck.called
        assert len(tctx.master.logs) == 2
Пример #15
0
    async def test_read(self, tmpdir, data, corrupt_data):
        rf = readfile.ReadFile()
        with taddons.context(rf) as tctx:
            tf = tmpdir.join("tfile")

            with asynctest.patch('mitmproxy.master.Master.load_flow') as mck:
                tf.write(data.getvalue())
                tctx.configure(rf, rfile=str(tf), readfile_filter=".*")
                assert not mck.awaited
                rf.running()
                await asyncio.sleep(0)
                assert mck.awaited

            tf.write(corrupt_data.getvalue())
            tctx.configure(rf, rfile=str(tf))
            rf.running()
            assert await tctx.master.await_log("corrupted")
Пример #16
0
def default_addons():
    return [
        core_option_validation.CoreOptionValidation(),
        anticache.AntiCache(),
        anticomp.AntiComp(),
        check_alpn.CheckALPN(),
        check_ca.CheckCA(),
        clientplayback.ClientPlayback(),
        disable_h2c.DisableH2C(),
        onboarding.Onboarding(),
        proxyauth.ProxyAuth(),
        replace.Replace(),
        script.ScriptLoader(),
        serverplayback.ServerPlayback(),
        setheaders.SetHeaders(),
        stickyauth.StickyAuth(),
        stickycookie.StickyCookie(),
        streambodies.StreamBodies(),
        streamfile.StreamFile(),
        readfile.ReadFile(),
        upstream_auth.UpstreamAuth(),
    ]
Пример #17
0
    def __init__(self, options, server):
        super().__init__(options, server)

        if not sys.stdout.isatty():
            print(
                "Error: mitmproxy's console interface requires a tty. "
                "Please run mitmproxy in an interactive shell environment.",
                file=sys.stderr)
            sys.exit(1)

        self.view = view.View()  # type: view.View
        self.stream_path = None
        # This line is just for type hinting
        self.options = self.options  # type: Options
        self.keymap = keymap.Keymap(self)
        defaultkeys.map(self.keymap)
        self.options.errored.connect(self.options_error)

        self.view_stack = []

        signals.call_in.connect(self.sig_call_in)
        signals.sig_add_log.connect(self.sig_add_log)
        self.addons.add(consoleaddons.Logger())
        self.addons.add(*addons.default_addons())
        self.addons.add(
            intercept.Intercept(),
            self.view,
            consoleaddons.UnsupportedLog(),
            readfile.ReadFile(),
            consoleaddons.ConsoleAddon(self),
        )

        def sigint_handler(*args, **kwargs):
            self.prompt_for_exit()

        signal.signal(signal.SIGINT, sigint_handler)

        self.window = None
Пример #18
0
 async def test_nonexistent_file(self):
     rf = readfile.ReadFile()
     with taddons.context(rf) as tctx:
         with pytest.raises(exceptions.FlowReadException):
             await rf.load_flows_from_path("nonexistent")
         assert await tctx.master.await_log("nonexistent")
Пример #19
0
 def test_nonexisting_file(self):
     rf = readfile.ReadFile()
     with taddons.context() as tctx:
         with pytest.raises(exceptions.FlowReadException):
             rf.load_flows_from_path("nonexistent")
         assert len(tctx.master.logs) == 1