2016-08-18 15:31:43 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
|
2016-10-17 04:11:21 +00:00
|
|
|
class Masker():
|
2016-08-18 15:31:43 +00:00
|
|
|
"""
|
|
|
|
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)):
|
2016-10-17 02:15:22 +00:00
|
|
|
result[i] ^= self.key[offset % 4]
|
2016-08-18 15:31:43 +00:00
|
|
|
offset += 1
|
|
|
|
result = bytes(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def __call__(self, data):
|
|
|
|
ret = self.mask(self.offset, data)
|
|
|
|
self.offset += len(ret)
|
|
|
|
return ret
|