From 2e9a34faa9fc8b0123375f220d785ebf2a10e716 Mon Sep 17 00:00:00 2001 From: Jessica Favin Date: Thu, 1 Nov 2018 19:53:37 +0100 Subject: [PATCH 1/5] Display errors on sys.stderr --- mitmproxy/addons/dumper.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mitmproxy/addons/dumper.py b/mitmproxy/addons/dumper.py index 87f45976b..4b2357f89 100644 --- a/mitmproxy/addons/dumper.py +++ b/mitmproxy/addons/dumper.py @@ -27,9 +27,10 @@ def colorful(line, styles): class Dumper: - def __init__(self, outfile=sys.stdout): + def __init__(self, outfile=sys.stdout, errfile=sys.stderr): self.filter: flowfilter.TFilter = None self.outfp: typing.io.TextIO = outfile + self.errfp: typing.io.TextIO = errfile def load(self, loader): loader.add_option( @@ -70,6 +71,13 @@ class Dumper: if self.outfp: self.outfp.flush() + def echo_error(self, text, ident=None, **style): + if ident: + text = indent(ident, text) + click.secho(text, file=self.errfp, **style) + if self.errfp: + self.errfp.flush() + def _echo_headers(self, headers): for k, v in headers.fields: k = strutils.bytes_to_escaped_str(k) @@ -243,7 +251,7 @@ class Dumper: self.echo_flow(f) def websocket_error(self, f): - self.echo( + self.echo_error( "Error in WebSocket connection to {}: {}".format( human.format_address(f.server_conn.address), f.error ), @@ -268,7 +276,7 @@ class Dumper: f.close_reason)) def tcp_error(self, f): - self.echo( + self.echo_error( "Error in TCP connection to {}: {}".format( human.format_address(f.server_conn.address), f.error ), From ade136dc4dd73b760b3ed116d5e4946753b67847 Mon Sep 17 00:00:00 2001 From: Jessica Favin Date: Sun, 4 Nov 2018 16:06:17 +0100 Subject: [PATCH 2/5] Update test_dumper.py --- test/mitmproxy/addons/test_dumper.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index c24801e4b..62dbd017d 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -155,6 +155,7 @@ class TestContentView: def test_tcp(): sio = io.StringIO() + sio_err = io.StringIO() d = dumper.Dumper(sio) with taddons.context(d) as ctx: ctx.configure(d, flow_detail=3, showhost=True) @@ -165,11 +166,12 @@ def test_tcp(): f = tflow.ttcpflow(client_conn=True, err=True) d.tcp_error(f) - assert "Error in TCP" in sio.getvalue() + assert "Error in TCP" in sio_err.getvalue() def test_websocket(): sio = io.StringIO() + sio_err = io.StringIO() d = dumper.Dumper(sio) with taddons.context(d) as ctx: ctx.configure(d, flow_detail=3, showhost=True) @@ -183,4 +185,4 @@ def test_websocket(): f = tflow.twebsocketflow(client_conn=True, err=True) d.websocket_error(f) - assert "Error in WebSocket" in sio.getvalue() + assert "Error in WebSocket" in sio_err.getvalue() From 312f9223163002d95ce4a27fa26c394df4eea9d2 Mon Sep 17 00:00:00 2001 From: Jessica Favin Date: Sun, 4 Nov 2018 16:26:51 +0100 Subject: [PATCH 3/5] Fix test_dumper.py - Dumper constructor --- test/mitmproxy/addons/test_dumper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index 62dbd017d..b1dc20b82 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -156,7 +156,7 @@ class TestContentView: def test_tcp(): sio = io.StringIO() sio_err = io.StringIO() - d = dumper.Dumper(sio) + d = dumper.Dumper(sio, sio_err) with taddons.context(d) as ctx: ctx.configure(d, flow_detail=3, showhost=True) f = tflow.ttcpflow() @@ -172,7 +172,7 @@ def test_tcp(): def test_websocket(): sio = io.StringIO() sio_err = io.StringIO() - d = dumper.Dumper(sio) + d = dumper.Dumper(sio, sio_err) with taddons.context(d) as ctx: ctx.configure(d, flow_detail=3, showhost=True) f = tflow.twebsocketflow() From 0cbbcffd89c84c00d1fbd989778a34c42f914779 Mon Sep 17 00:00:00 2001 From: Jessica Favin Date: Sun, 4 Nov 2018 17:24:34 +0100 Subject: [PATCH 4/5] test_dumper.py - Add sio_err everywhere + adjust test_simple --- test/mitmproxy/addons/test_dumper.py | 30 ++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index b1dc20b82..7a41c7b97 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -32,37 +32,50 @@ def test_configure(): def test_simple(): sio = io.StringIO() - d = dumper.Dumper(sio) + sio_err = io.StringIO() + d = dumper.Dumper(sio, sio_err) with taddons.context(d) as ctx: ctx.configure(d, flow_detail=0) d.response(tflow.tflow(resp=True)) assert not sio.getvalue() sio.truncate(0) + assert not sio_err.getvalue() + sio_err.truncate(0) ctx.configure(d, flow_detail=1) d.response(tflow.tflow(resp=True)) assert sio.getvalue() sio.truncate(0) + assert not sio_err.getvalue() + sio_err.truncate(0) ctx.configure(d, flow_detail=1) d.error(tflow.tflow(err=True)) assert sio.getvalue() sio.truncate(0) + assert not sio_err.getvalue() + sio_err.truncate(0) ctx.configure(d, flow_detail=4) d.response(tflow.tflow(resp=True)) assert sio.getvalue() sio.truncate(0) + assert not sio_err.getvalue() + sio_err.truncate(0) ctx.configure(d, flow_detail=4) d.response(tflow.tflow(resp=True)) assert "<<" in sio.getvalue() sio.truncate(0) + assert not sio_err.getvalue() + sio_err.truncate(0) ctx.configure(d, flow_detail=4) d.response(tflow.tflow(err=True)) assert "<<" in sio.getvalue() sio.truncate(0) + assert not sio_err.getvalue() + sio_err.truncate(0) ctx.configure(d, flow_detail=4) flow = tflow.tflow() @@ -75,6 +88,8 @@ def test_simple(): d.response(flow) assert sio.getvalue() sio.truncate(0) + assert not sio_err.getvalue() + sio_err.truncate(0) ctx.configure(d, flow_detail=4) flow = tflow.tflow(resp=tutils.tresp(content=b"{")) @@ -83,6 +98,8 @@ def test_simple(): d.response(flow) assert sio.getvalue() sio.truncate(0) + assert not sio_err.getvalue() + sio_err.truncate(0) ctx.configure(d, flow_detail=4) flow = tflow.tflow() @@ -92,6 +109,8 @@ def test_simple(): d.response(flow) assert "content missing" in sio.getvalue() sio.truncate(0) + assert not sio_err.getvalue() + sio_err.truncate(0) def test_echo_body(): @@ -100,7 +119,8 @@ def test_echo_body(): f.response.content = b"foo bar voing\n" * 100 sio = io.StringIO() - d = dumper.Dumper(sio) + sio_err = io.StringIO() + d = dumper.Dumper(sio, sio_err) with taddons.context(d) as ctx: ctx.configure(d, flow_detail=3) d._echo_message(f.response) @@ -110,7 +130,8 @@ def test_echo_body(): def test_echo_request_line(): sio = io.StringIO() - d = dumper.Dumper(sio) + sio_err = io.StringIO() + d = dumper.Dumper(sio, sio_err) with taddons.context(d) as ctx: ctx.configure(d, flow_detail=3, showhost=True) f = tflow.tflow(client_conn=None, server_conn=True, resp=True) @@ -146,7 +167,8 @@ class TestContentView: with mock.patch("mitmproxy.contentviews.auto.ViewAuto.__call__") as va: va.side_effect = exceptions.ContentViewException("") sio = io.StringIO() - d = dumper.Dumper(sio) + sio_err = io.StringIO() + d = dumper.Dumper(sio, sio_err) with taddons.context(d) as ctx: ctx.configure(d, flow_detail=4) d.response(tflow.tflow()) From fd1efc885236f41a2d427ede40a7f7ae272ae319 Mon Sep 17 00:00:00 2001 From: JessicaFavin Date: Sun, 4 Nov 2018 19:18:32 +0100 Subject: [PATCH 5/5] Remove indent on echo_error --- mitmproxy/addons/dumper.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mitmproxy/addons/dumper.py b/mitmproxy/addons/dumper.py index 4b2357f89..dcac6b82e 100644 --- a/mitmproxy/addons/dumper.py +++ b/mitmproxy/addons/dumper.py @@ -71,9 +71,7 @@ class Dumper: if self.outfp: self.outfp.flush() - def echo_error(self, text, ident=None, **style): - if ident: - text = indent(ident, text) + def echo_error(self, text, **style): click.secho(text, file=self.errfp, **style) if self.errfp: self.errfp.flush()