-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.py
executable file
·235 lines (196 loc) · 7.56 KB
/
websocket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
import socket
import ctypes
import struct
import hashlib
import base64
c_uint8 = ctypes.c_uint8
c_uint16 = ctypes.c_uint16
OPCODES = [
'continuation',
'text',
'binary',
'res1',
'res2',
'res3',
'res4',
'res5',
'close',
'ping',
'pong',
'res6',
'res7',
'res8',
'res9',
'resa'
]
class FrameHeaderBitField(ctypes.BigEndianStructure):
_fields_ = [
("mask", c_uint8, 1),
("len_16", c_uint8, 7),
("fin", c_uint8, 1),
("rsv1", c_uint8, 1),
("rsv2", c_uint8, 1),
("rsv3", c_uint8, 1),
("opcode", c_uint8, 4)
]
class FrameHeader(ctypes.Union):
_fields_ = [
("bitfield", FrameHeaderBitField),
("short", c_uint16)
]
def __init__(self, frame_header_bytes=0):
self.short = frame_header_bytes
def mask_payload(payload, mask_key=b'\xaa\xbb\xcc\xdd'):
processed = bytearray(payload)
for i, byte in enumerate(processed):
processed[i] = byte ^ mask_key[i % 4]
return (bytes(processed), mask_key)
def decode_complete_frame(frame):
frame_header_bytes = struct.unpack('!H', frame[0:2])[0]
frame_header = FrameHeader(frame_header_bytes)
payload = b''
print('-'*20)
print(f'fin: {frame_header.bitfield.fin}')
print(f'opcode: {frame_header.bitfield.opcode} '\
f'({OPCODES[frame_header.bitfield.opcode]})')
print(f'mask: {frame_header.bitfield.mask}')
print(f'len_16: {frame_header.bitfield.len_16}')
if frame_header.bitfield.len_16:
if frame_header.bitfield.len_16 < 126:
data_offset = 2
payload_len = frame_header.bitfield.len_16
elif frame_header.bitfield.len_16 == 126:
data_offset = 4
payload_len = struct.unpack('!H', frame[2:4])[0]
elif frame_header.bitfield.len_16 == 127:
data_offset = 10
payload_len = struct.unpack('!Q', frame[2:10])[0]
if frame_header.bitfield.mask:
masking_key = frame[data_offset: data_offset + 4]
print(f'masking_key:\t{masking_key}')
payload = frame[data_offset + 4: data_offset + 4 + payload_len]
payload = mask_payload(payload)[0]
else:
payload = frame[data_offset: data_offset + payload_len]
close_reason = ''
if payload and frame_header.bitfield.opcode == OPCODES.index('close'):
close_reason = struct.unpack('!H', payload[0:2])[0]
payload = payload[2:]
print(f'payload_len: {payload_len}')
if close_reason:
print(f'close_reason: {close_reason}')
print(f'payload: {payload if len(payload) <= 100 else payload[:101]}')
print('-'*20)
return (frame_header.bitfield.opcode, payload.decode(),
frame_header.bitfield.fin)
def decode_frame_from_sock(sock):
frame_header_bytes = b''
while not frame_header_bytes:
frame_header_bytes = sock.recv(2)
frame_header_bytes = struct.unpack('!H', frame_header_bytes[0:2])[0]
frame_header = FrameHeader(frame_header_bytes)
payload = b''
print('-'*20)
print('Received frame:\n')
print(f'fin: {frame_header.bitfield.fin}')
print(f'opcode: {frame_header.bitfield.opcode} '\
f'({OPCODES[frame_header.bitfield.opcode]})')
print(f'mask: {frame_header.bitfield.mask}')
print(f'len_16: {frame_header.bitfield.len_16}')
if frame_header.bitfield.len_16:
if frame_header.bitfield.len_16 < 126:
data_offset = 2
payload_len = frame_header.bitfield.len_16
elif frame_header.bitfield.len_16 == 126:
data_offset = 4
payload_len = struct.unpack('!H', sock.recv(2))[0]
elif frame_header.bitfield.len_16 == 127:
data_offset = 10
payload_len = struct.unpack('!Q', sock.recv(8))[0]
if frame_header.bitfield.mask:
masking_key = sock.recv(4)
print(f'masking_key:\t{masking_key}')
payload = sock.recv(payload_len)
payload = mask_payload(payload, masking_key)[0]
else:
payload = sock.recv(payload_len)
close_reason = ''
if payload and frame_header.bitfield.opcode == OPCODES.index('close'):
close_reason = struct.unpack('!H', payload[0:2])[0]
payload = payload[2:]
print(f'payload_len: {payload_len}')
if close_reason:
print(f'close_reason: {close_reason}')
print(f'payload: {payload if len(payload) <= 100 else payload[:101]}')
print('-'*20)
return (frame_header.bitfield.opcode, payload,
frame_header.bitfield.fin)
def encode_frame(opcode=OPCODES.index('text'), payload=None, fragment=False,
mask=True):
frame = b''
frame_header = FrameHeader()
if fragment:
frame_header.bitfield.fin = 0
else:
frame_header.bitfield.fin = 1
frame_header.bitfield.opcode = opcode
if payload:
payload_len = len(payload)
payload = payload.encode()
if mask:
frame_header.bitfield.mask = 1
if payload_len < 126:
frame_header.bitfield.len_16 = payload_len
frame += struct.pack('!H', frame_header.short)
elif payload_len > 125 and payload_len < 2 ** 16:
frame_header.bitfield.len_16 = 126
frame += struct.pack('!H', frame_header.short)
frame += struct.pack('!H', payload_len)
elif payload_len < 2 ** 64:
frame_header.bitfield.len_16 = 127
frame += struct.pack('!H', frame_header.short)
frame += struct.pack('!Q', payload_len)
if mask:
payload, mask_key = mask_payload(payload)
frame += mask_key
frame += payload
else:
frame += struct.pack('!H', frame_header.short)
return frame
def client_handshake(sock, host):
client_handshake = \
'GET / HTTP/1.1\r\n'\
f'Host: {host}\r\n'\
'Sec-WebSocket-Version: 13\r\n'\
'Sec-WebSocket-Key: 3F9yYO/NTk78G+4MKUm5rA==\r\n'\
'Connection: Upgrade\r\n'\
'Upgrade: websocket\r\n\r\n'
sock.sendall(client_handshake.encode())
handshake_response = sock.recv(2048)
return handshake_response
def generate_server_handshake(client_handshake):
request_line, headers = parse_client_handshake(client_handshake)
handshake_response = 'HTTP/1.1 101 Switching Protocols\r\nUpgrade: '\
'WebSocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: '\
'{}\r\n\r\n'
sec_websocket_key = headers[b'Sec-WebSocket-Key']
sec_websocket_accept = generate_sec_websocket_accept(sec_websocket_key)
handshake_response = handshake_response.format(sec_websocket_accept.decode())
return handshake_response.encode()
def generate_sec_websocket_accept(sec_websocket_key):
guid = b'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
sec_websocket_accept = sec_websocket_key + guid
sha1 = hashlib.sha1()
sha1.update(sec_websocket_accept)
sec_websocket_accept = base64.b64encode(sha1.digest())
return sec_websocket_accept
def parse_client_handshake(client_handshake):
client_handshake = client_handshake.replace(b'\r',b'').split(b'\n')
request_line = client_handshake[0].split(b' ')
headers = {}
for header in client_handshake[1:]:
if header:
header = header.split(b': ')
headers[header[0]] = header[1]
return request_line, headers