From 5b1fefee9bf8564b32a1137975cb181d54ef6dff Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 1 Dec 2014 03:04:48 +0100 Subject: add inline script example for websocket passthrough, fix #340 --- examples/ignore_websocket.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 examples/ignore_websocket.py (limited to 'examples/ignore_websocket.py') diff --git a/examples/ignore_websocket.py b/examples/ignore_websocket.py new file mode 100644 index 00000000..1ee81d38 --- /dev/null +++ b/examples/ignore_websocket.py @@ -0,0 +1,27 @@ +# This script makes mitmproxy switch to passthrough mode for all HTTP +# responses with "Connection: Upgrade" header. This is useful to make +# WebSockets work in untrusted environments. +# +# Note: Chrome (and possibly other browsers), when explicitly configured +# to use a proxy (i.e. mitmproxy's regular mode), send a CONNECT request +# to the proxy before they initiate the websocket connection. +# To make WebSockets work in these cases, supply +# `--ignore :80$` as an additional parameter. +# (see http://mitmproxy.org/doc/features/passthrough.html) + +from libmproxy.protocol.http import HTTPRequest +from libmproxy.protocol.tcp import TCPHandler +from libmproxy.protocol import KILL +from libmproxy.script import concurrent + +HTTPRequest._headers_to_strip_off.remove("Connection") +HTTPRequest._headers_to_strip_off.remove("Upgrade") + +@concurrent +def response(context, flow): + if flow.response.headers.get_first("Connection", None) == "Upgrade": + # We need to send the response manually now... + flow.client_conn.send(flow.response.assemble()) + # ...and then delegate to tcp passthrough. + TCPHandler(flow.live.c, log=False).handle_messages() + flow.reply(KILL) \ No newline at end of file -- cgit v1.2.3 From a7a9ef826c206ca93c20ae26c20f5e5a2d5de8e6 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 1 Dec 2014 03:36:04 +0100 Subject: fix tests --- examples/ignore_websocket.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'examples/ignore_websocket.py') diff --git a/examples/ignore_websocket.py b/examples/ignore_websocket.py index 1ee81d38..48093951 100644 --- a/examples/ignore_websocket.py +++ b/examples/ignore_websocket.py @@ -14,14 +14,21 @@ from libmproxy.protocol.tcp import TCPHandler from libmproxy.protocol import KILL from libmproxy.script import concurrent -HTTPRequest._headers_to_strip_off.remove("Connection") -HTTPRequest._headers_to_strip_off.remove("Upgrade") + +def start(context, argv): + HTTPRequest._headers_to_strip_off.remove("Connection") + HTTPRequest._headers_to_strip_off.remove("Upgrade") + + +def done(context): + HTTPRequest._headers_to_strip_off.append("Connection") + HTTPRequest._headers_to_strip_off.append("Upgrade") @concurrent def response(context, flow): - if flow.response.headers.get_first("Connection", None) == "Upgrade": - # We need to send the response manually now... - flow.client_conn.send(flow.response.assemble()) - # ...and then delegate to tcp passthrough. - TCPHandler(flow.live.c, log=False).handle_messages() - flow.reply(KILL) \ No newline at end of file + if flow.response.headers.get_first("Connection", None) == "Upgrade": + # We need to send the response manually now... + flow.client_conn.send(flow.response.assemble()) + # ...and then delegate to tcp passthrough. + TCPHandler(flow.live.c, log=False).handle_messages() + flow.reply(KILL) \ No newline at end of file -- cgit v1.2.3