aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ignore_websocket.py
diff options
context:
space:
mode:
authorMarcelo Glezer <mg@tekii.com.ar>2014-12-11 14:54:14 -0300
committerMarcelo Glezer <mg@tekii.com.ar>2014-12-11 14:54:14 -0300
commit4952643a0d76eb1e9bd51cbbe95c565ae48b97a2 (patch)
treef43fc647bdfabb522bdef32e21ea4a36404cc311 /examples/ignore_websocket.py
parent83b1d4e0e0490e5be05943da459c925a3ee3ff14 (diff)
parentffb95a1db742d71d7671f9e9c6db552774bb0ead (diff)
downloadmitmproxy-4952643a0d76eb1e9bd51cbbe95c565ae48b97a2.tar.gz
mitmproxy-4952643a0d76eb1e9bd51cbbe95c565ae48b97a2.tar.bz2
mitmproxy-4952643a0d76eb1e9bd51cbbe95c565ae48b97a2.zip
Merge remote-tracking branch 'base/master'
Diffstat (limited to 'examples/ignore_websocket.py')
-rw-r--r--examples/ignore_websocket.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/ignore_websocket.py b/examples/ignore_websocket.py
new file mode 100644
index 00000000..48093951
--- /dev/null
+++ b/examples/ignore_websocket.py
@@ -0,0 +1,34 @@
+# 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
+
+
+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