aboutsummaryrefslogtreecommitdiffstats
path: root/netlib/websockets/masker.py
diff options
context:
space:
mode:
authorThomas Kriechbaumer <Kriechi@users.noreply.github.com>2016-09-01 10:39:57 +0200
committerGitHub <noreply@github.com>2016-09-01 10:39:57 +0200
commit55d938b880fd861a22ac66da0da9a741bdd9abd5 (patch)
treed469bbd0dd5b1966591a332bf2094d4389100219 /netlib/websockets/masker.py
parent281d779fa3eb6b81ec76d046337275c0a82eff46 (diff)
parent0d0c2c788df4b60e951e6fcc13b479de8cec22c1 (diff)
downloadmitmproxy-55d938b880fd861a22ac66da0da9a741bdd9abd5.tar.gz
mitmproxy-55d938b880fd861a22ac66da0da9a741bdd9abd5.tar.bz2
mitmproxy-55d938b880fd861a22ac66da0da9a741bdd9abd5.zip
Merge pull request #1488 from mitmproxy/websockets
add WebSockets support
Diffstat (limited to 'netlib/websockets/masker.py')
-rw-r--r--netlib/websockets/masker.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/netlib/websockets/masker.py b/netlib/websockets/masker.py
new file mode 100644
index 00000000..bd39ed6a
--- /dev/null
+++ b/netlib/websockets/masker.py
@@ -0,0 +1,33 @@
+from __future__ import absolute_import
+
+import six
+
+
+class Masker(object):
+ """
+ Data sent from the server must be masked to prevent malicious clients
+ from sending data over the wire in predictable patterns.
+
+ Servers do not have to mask data they send to the client.
+ https://tools.ietf.org/html/rfc6455#section-5.3
+ """
+
+ def __init__(self, key):
+ self.key = key
+ self.offset = 0
+
+ def mask(self, offset, data):
+ result = bytearray(data)
+ for i in range(len(data)):
+ if six.PY2:
+ result[i] ^= ord(self.key[offset % 4])
+ else:
+ result[i] ^= self.key[offset % 4]
+ offset += 1
+ result = bytes(result)
+ return result
+
+ def __call__(self, data):
+ ret = self.mask(self.offset, data)
+ self.offset += len(ret)
+ return ret