def test_replay(self): o = dump.Options(server_replay=["nonexistent"], replay_kill_extra=True) tutils.raises(exceptions.OptionsError, dump.DumpMaster, o, proxy.DummyServer()) with tutils.tmpdir() as t: p = os.path.join(t, "rep") self.flowfile(p) o = dump.Options(server_replay=[p], replay_kill_extra=True) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) self.cycle(m, b"content") self.cycle(m, b"content") o = dump.Options(server_replay=[p], replay_kill_extra=False) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) self.cycle(m, b"nonexistent") o = dump.Options(client_replay=[p], replay_kill_extra=False) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer())
def mkmaster(self, flt, **options): o = dump.Options(filtstr=flt, verbosity=-1, flow_detail=0, **options) m = dump.DumpMaster(o, proxy.DummyServer(), with_termlog=False, with_dumper=False) return m
def mkmaster(self, flt, **options): if "verbosity" not in options: options["verbosity"] = 0 if "flow_detail" not in options: options["flow_detail"] = 0 o = dump.Options(filtstr=flt, tfile=io.StringIO(), **options) return dump.DumpMaster(o, proxy.DummyServer())
def test_echo_request_line(): sio = io.StringIO() d = dumper.Dumper(sio) with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, flow_detail=3, showhost=True) f = tflow.tflow(client_conn=None, server_conn=True, resp=True) f.request.is_replay = True d._echo_request_line(f) assert "[replay]" in sio.getvalue() sio.truncate(0) f = tflow.tflow(client_conn=None, server_conn=True, resp=True) f.request.is_replay = False d._echo_request_line(f) assert "[replay]" not in sio.getvalue() sio.truncate(0) f = tflow.tflow(client_conn=None, server_conn=True, resp=True) f.request.http_version = "nonstandard" d._echo_request_line(f) assert "nonstandard" in sio.getvalue() sio.truncate(0) ctx.configure(d, flow_detail=0, showhost=True) f = tflow.tflow(client_conn=None, server_conn=True, resp=True) terminalWidth = max(shutil.get_terminal_size()[0] - 25, 50) f.request.url = "http://address:22/" + ("x" * terminalWidth) + "textToBeTruncated" d._echo_request_line(f) assert "textToBeTruncated" not in sio.getvalue() sio.truncate(0)
def mitmdump(args=None): # pragma: no cover from mitmproxy.tools import dump version_check.check_pyopenssl_version() parser = cmdline.mitmdump() args = parser.parse_args(args) if args.quiet: args.flow_detail = 0 master = None try: dump_options = dump.Options(**cmdline.get_common_options(args)) dump_options.flow_detail = args.flow_detail dump_options.keepserving = args.keepserving dump_options.filtstr = " ".join(args.args) if args.args else None server = process_options(parser, dump_options, args) master = dump.DumpMaster(dump_options, server) def cleankill(*args, **kwargs): master.shutdown() signal.signal(signal.SIGTERM, cleankill) master.run() except (dump.DumpError, exceptions.OptionsError) as e: print("mitmdump: %s" % e, file=sys.stderr) sys.exit(1) except (KeyboardInterrupt, RuntimeError): pass if master is None or master.has_errored: print("mitmdump: errors occurred during run", file=sys.stderr) sys.exit(1)
def test_simple(self): t = termlog.TermLog() sio = io.StringIO() t.configure(dump.Options(tfile=sio, verbosity=2), set([])) t.log(log.LogEntry("one", "info")) assert "one" in sio.getvalue() t.log(log.LogEntry("two", "debug")) assert "two" not in sio.getvalue()
def test_setheader(self): o = dump.Options(setheaders=[(".*", "one", "two")], tfile=io.StringIO()) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) f = self.cycle(m, b"content") assert f.request.headers["one"] == "two"
def test_contentview(self, view_auto): view_auto.side_effect = exceptions.ContentViewException("") sio = io.StringIO() d = dumper.Dumper(sio) with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, flow_detail=4, verbosity=3) d.response(tflow.tflow()) assert "Content viewer failed" in ctx.master.event_log[0][1]
def test_replacements(self): o = dump.Options( replacements=[(".*", "content", "foo")], tfile=io.StringIO(), ) o.verbosity = 0 o.flow_detail = 0 m = dump.DumpMaster(o, proxy.DummyServer()) f = self.cycle(m, b"content") assert f.request.content == b"foo"
def test_echo_body(): f = tflow.tflow(client_conn=True, server_conn=True, resp=True) f.response.headers["content-type"] = "text/html" f.response.content = b"foo bar voing\n" * 100 sio = io.StringIO() d = dumper.Dumper(sio) with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, flow_detail=3) d._echo_message(f.response) t = sio.getvalue() assert "cut off" in t
def test_configure(): sa = streamfile.StreamFile() with taddons.context(options=dump.Options()) as tctx: with tutils.tmpdir() as tdir: p = os.path.join(tdir, "foo") tutils.raises( exceptions.OptionsError, tctx.configure, sa, streamfile=tdir ) tutils.raises( "invalid filter", tctx.configure, sa, streamfile=p, filtstr="~~" )
def test_configure(): sa = streamfile.StreamFile() with taddons.context(options=dump.Options()) as tctx: with tutils.tmpdir() as tdir: p = os.path.join(tdir, "foo") with pytest.raises(exceptions.OptionsError): tctx.configure(sa, streamfile=tdir) with pytest.raises("invalid filter"): tctx.configure(sa, streamfile=p, filtstr="~~") tctx.configure(sa, filtstr="foo") assert sa.filt tctx.configure(sa, filtstr=None) assert not sa.filt
def test_tcp(): sio = io.StringIO() d = dumper.Dumper(sio) with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, flow_detail=3, showhost=True) f = tflow.ttcpflow(client_conn=True, server_conn=True) d.tcp_message(f) assert "it's me" in sio.getvalue() sio.truncate(0) f = tflow.ttcpflow(client_conn=True, err=True) d.tcp_error(f) assert "Error in TCP" in sio.getvalue()
def test_contentview(self, view_auto): view_auto.side_effect = exceptions.ContentViewException("") sio = io.StringIO() o = dump.Options( flow_detail=4, verbosity=3, tfile=sio, ) m = mastertest.RecordingMaster(o, proxy.DummyServer()) d = dumper.Dumper() m.addons.add(d) m.response(tflow.tflow()) assert "Content viewer failed" in m.event_log[0][1]
def test_simple(self): d = dumper.Dumper() sio = io.StringIO() updated = {"tfile", "flow_detail"} d.configure(dump.Options(tfile=sio, flow_detail=0), updated) d.response(tflow.tflow()) assert not sio.getvalue() d.configure(dump.Options(tfile=sio, flow_detail=4), updated) d.response(tflow.tflow()) assert sio.getvalue() sio = io.StringIO() d.configure(dump.Options(tfile=sio, flow_detail=4), updated) d.response(tflow.tflow(resp=True)) assert "<<" in sio.getvalue() sio = io.StringIO() d.configure(dump.Options(tfile=sio, flow_detail=4), updated) d.response(tflow.tflow(err=True)) assert "<<" in sio.getvalue() sio = io.StringIO() d.configure(dump.Options(tfile=sio, flow_detail=4), updated) flow = tflow.tflow() flow.request = mitmproxy.test.tutils.treq() flow.request.stickycookie = True flow.client_conn = mock.MagicMock() flow.client_conn.address.host = "foo" flow.response = mitmproxy.test.tutils.tresp(content=None) flow.response.is_replay = True flow.response.status_code = 300 d.response(flow) assert sio.getvalue() sio = io.StringIO() d.configure(dump.Options(tfile=sio, flow_detail=4), updated) flow = tflow.tflow(resp=mitmproxy.test.tutils.tresp(content=b"{")) flow.response.headers["content-type"] = "application/json" flow.response.status_code = 400 d.response(flow) assert sio.getvalue() sio = io.StringIO() d.configure(dump.Options(tfile=sio), updated) flow = tflow.tflow() flow.request.content = None flow.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) flow.response.content = None d.response(flow) assert "content missing" in sio.getvalue()
def test_configure(): d = dumper.Dumper() with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, filtstr="~b foo") assert d.filter f = tflow.tflow(resp=True) assert not d.match(f) f.response.content = b"foo" assert d.match(f) ctx.configure(d, filtstr=None) assert not d.filter tutils.raises(exceptions.OptionsError, ctx.configure, d, filtstr="~~") assert not d.filter
def test_websocket(): sio = io.StringIO() d = dumper.Dumper(sio) with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, flow_detail=3, showhost=True) f = tflow.twebsocketflow() d.websocket_message(f) assert "hello text" in sio.getvalue() sio.truncate(0) d.websocket_end(f) assert "WebSocket connection closed by" in sio.getvalue() f = tflow.twebsocketflow(client_conn=True, err=True) d.websocket_error(f) assert "Error in WebSocket" in sio.getvalue()
def test_echo_request_line(): d = dumper.Dumper() sio = io.StringIO() with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, tfile=sio, flow_detail=3, showhost=True) f = tflow.tflow(client_conn=None, server_conn=True, resp=True) f.request.is_replay = True d._echo_request_line(f) assert "[replay]" in sio.getvalue() sio.truncate(0) f = tflow.tflow(client_conn=None, server_conn=True, resp=True) f.request.is_replay = False d._echo_request_line(f) assert "[replay]" not in sio.getvalue() sio.truncate(0) f = tflow.tflow(client_conn=None, server_conn=True, resp=True) f.request.http_version = "nonstandard" d._echo_request_line(f) assert "nonstandard" in sio.getvalue() sio.truncate(0)
def test_simple(): sio = io.StringIO() d = dumper.Dumper(sio) with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, flow_detail=0) d.response(tflow.tflow(resp=True)) assert not sio.getvalue() sio.truncate(0) ctx.configure(d, flow_detail=1) d.response(tflow.tflow(resp=True)) assert sio.getvalue() sio.truncate(0) ctx.configure(d, flow_detail=1) d.error(tflow.tflow(err=True)) assert sio.getvalue() sio.truncate(0) ctx.configure(d, flow_detail=4) d.response(tflow.tflow(resp=True)) assert sio.getvalue() sio.truncate(0) ctx.configure(d, flow_detail=4) d.response(tflow.tflow(resp=True)) assert "<<" in sio.getvalue() sio.truncate(0) ctx.configure(d, flow_detail=4) d.response(tflow.tflow(err=True)) assert "<<" in sio.getvalue() sio.truncate(0) ctx.configure(d, flow_detail=4) flow = tflow.tflow() flow.request = tutils.treq() flow.request.stickycookie = True flow.client_conn = mock.MagicMock() flow.client_conn.address.host = "foo" flow.response = tutils.tresp(content=None) flow.response.is_replay = True flow.response.status_code = 300 d.response(flow) assert sio.getvalue() sio.truncate(0) ctx.configure(d, flow_detail=4) flow = tflow.tflow(resp=tutils.tresp(content=b"{")) flow.response.headers["content-type"] = "application/json" flow.response.status_code = 400 d.response(flow) assert sio.getvalue() sio.truncate(0) ctx.configure(d, flow_detail=4) flow = tflow.tflow() flow.request.content = None flow.response = http.HTTPResponse.wrap(tutils.tresp()) flow.response.content = None d.response(flow) assert "content missing" in sio.getvalue() sio.truncate(0)
def test_addons_dumper(self, dumper): with mock.patch('sys.stdout'): o = dump.Options() m = dump.DumpMaster(o, proxy.DummyServer(), with_dumper=dumper) assert (m.addons.get('dumper') is not None) == dumper
def test_addons_termlog(self, termlog): with mock.patch('sys.stdout'): o = dump.Options() m = dump.DumpMaster(o, proxy.DummyServer(), with_termlog=termlog) assert (m.addons.get('termlog') is not None) == termlog
def test_error(self): o = dump.Options(tfile=io.StringIO(), flow_detail=1) m = dump.DumpMaster(o, proxy.DummyServer()) f = tutils.tflow(err=True) m.error(f) assert "error" in o.tfile.getvalue()
def mkmaster(self, flt, **options): o = dump.Options(filtstr=flt, verbosity=-1, flow_detail=0, **options) return dump.DumpMaster(o, proxy.DummyServer())
def show(flow_detail, flows): d = dumper.Dumper() with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, flow_detail=flow_detail) for f in flows: ctx.cycle(d, f)
def test_options(self): o = dump.Options(verbosity=2) assert o.verbosity == 2