-
Notifications
You must be signed in to change notification settings - Fork 15
/
webmapper_http_server.py
executable file
·414 lines (363 loc) · 13.6 KB
/
webmapper_http_server.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import socketserver
import http.server
import socket, errno
import urllib
#import urllib.request, urllib.parse, urllib.error
#import urllib.parse
import cgi
import threading, queue
import time
import json
from select import select
import sys
import struct
import hashlib
from io import BytesIO
import pdb
message_pipe = queue.Queue()
tracing = False
done = False
class RequestCounter(object):
def __init__(self):
self.count = 1
self.started = False
def start(self):
if not self.started:
self.started = True
self.count -= 1
def inc(self):
if (not self.started):
self.start()
self.count += 1
def dec(self):
if (self.started):
self.count -= 1
ref = RequestCounter()
class ReuseTCPServer(socketserver.ThreadingTCPServer):
allow_reuse_address = True
class MprHTTPServer(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
query = cgi.parse_multipart(self.rfile, pdict)
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.end_headers()
fn = query['filename'][0].split('\\')[-1]
er = 0
try:
er = 1
if 'load' in cmd_handlers:
er = 2
sources = query['sources'][0].split(',')
destinations = query['destinations'][0].split(',')
msg = {'sources': sources, 'destinations': destinations,
'loading': query['mapping_json'][0]}
cmd_handlers['load'](msg)
self.wfile.write(("Success: %s loaded successfully."%fn).encode('utf-8'))
except Exception as e:
self.wfile.write(("Error: loading %s (%d)."%(fn,er)).encode('utf-8'))
raise e
def do_GET(self):
command = self.path
args = []
try:
parsed = urllib.parse.urlparse(self.path)
command = parsed.path
args = dict(urllib.parse.parse_qsl(parsed.query))
except Exception as e:
print('exception:', e)
contenttype = { 'html': 'text/html; charset=UTF-8',
'js': 'text/javascript',
'css': 'text/css',
'json': 'text/javascript',
'png': 'image/png',
'dl': None}
def found(type=''):
if (type=='socket'):
if tracing: print('websocket requested')
return self.do_websocket()
self.send_response(200)
if type=='dl': return
elif type in contenttype:
self.send_header('Content-Type', contenttype[type])
self.end_headers()
def notfound(type=''):
self.send_response(404)
self.send_header('Content-Type', contenttype['html'])
self.end_headers()
self.wfile.write(b'404 Not Found:'+self.path.encode('utf-8'))
try:
found(handlers[command][1])
if command=='/send_cmd':
if tracing: print('hxr_recv:', args['msg'])
handlers[command][0](self.wfile, args)
except KeyError:
try:
type = self.path.rsplit('.',1)[-1]
if type in contenttype:
self.send_response(200)
f = open(self.path[1:], 'rb')
found(type)
self.copyfile(f, self.wfile)
except IOError:
notfound(self.path)
def do_websocket(self):
ref.inc()
ws_version = self.websocket_handshake()
try:
if ws_version < 8:
self.do_websocket_0()
else:
self.do_websocket_8()
except socket.error as e:
if e.errno == errno.EPIPE or e.errno == errno.ECONNRESET:
# Avoid reporting a huge stack trace for broken pipe
# exception, it just means that the websocket closed
# because the browser window was closed for example.
print('[ws]', e)
else:
raise e
finally:
ref.dec()
def do_websocket_0(self):
msg = ""
while not done:
time.sleep(0.1)
if not message_pipe.empty():
sendmsg = message_pipe.get()
if tracing: print('ws0_send:', sendmsg)
self.wfile.write(bytes([0])+json.dumps({"cmd": sendmsg[0],
"args": sendmsg[1]})
+ bytes([0xFF]));
self.wfile.flush()
while len(select([self.rfile],[],[],0)[0])>0:
msg += self.rfile.read(1)
if len(msg)==0:
break
if msg[-1]==0x00:
msg = "";
elif msg[-1]==0xFF:
break;
if len(msg)>0 and msg[-1]==0xFF:
out = BytesIO()
if tracing: print('ws0_recv:', msg[:-1])
handler_send_command(out, {'msg':msg[:-1]})
msg = ""
r = out.getvalue()
if len(r) > 0:
if tracing: print('ws0_send2:', r)
self.wfile.write(bytes([0])+r.encode('utf-8')+bytes([0xFF]))
self.wfile.flush()
def do_websocket_8(self):
def send_string(s):
if isinstance(s, str):
s = s.encode('utf-8')
l = len(s)
header = bytearray()
first = 1
while l > 0:
temp = None
if l >= 32767:
header = bytearray([first, 126, (32767>>8)&0xFF, 32767&0xFF])
temp = s[32767:l]
s = s[0:32767]
first = 0
else:
if l<126:
header = bytearray([(1<<7)|first, l])
else:
header = bytearray([(1<<7)|first, 126, (l>>8)&0xFF, l&0xFF])
l = 0
self.request.sendall(header+s)
self.wfile.flush()
if temp != None:
s = temp
l = len(s)
def decrypt_and_forward(buffer, key, length):
m = bytearray([buffer[i] ^ key[i%4] for i in range(length)])
out = BytesIO()
if tracing: print('ws8_recv:', m)
handler_send_command(out, {'msg':m})
length = offset = -1
r = out.getvalue()
if len(r) > 0:
if tracing: print('ws8_send2:', r)
send_string(r)
def check_backend():
n = 0
while not message_pipe.empty() and n < 30:
sendmsg = message_pipe.get()
if tracing: print('ws8_send:', sendmsg)
s = json.dumps({"cmd": sendmsg[0],
"args": sendmsg[1]})
send_string(s)
n += 1
msg = b""
length = -1
offset = -1
while not done:
to_read = len(select([self.rfile],[],[],0.1)[0]) > 0
check_backend()
while True:
if to_read:
msg += self.rfile.read1()
if len(msg) < 8:
break
opcode=msg[0] # TODO check FIN
# opcode&0x7F should be 1 for text, 2 for binary
if (opcode&0x7F) == 8:
# Connection close
print('[ws] connection close!')
return
if (opcode&0x7F)!=1 and (opcode&0x7F)!=2:
print('[ws] unknown opcode %#x'%(opcode&0x7F))
if length == -1:
mask = msg[1] & 0x80
length = msg[1] & 0x7F
offset = 2 + 4*(mask!=0)
if length == 126:
length = (msg[2]<<8) | msg[3]
offset = 4 + 4*(mask!=0)
elif length == 127:
print('TODO extended message length')
if length<126:
key = msg[2:6]
elif mask!=0:
key = msg[4:8]
else:
print('error: unknown key')
if len(msg) >= length+offset:
decrypt_and_forward(msg[offset:offset+length], key, length)
msg = msg[offset+length:]
length = offset = -1
to_read = len(select([self.rfile],[],[],0)[0]) > 0
def websocket_handshake(self):
self.send_response(101, 'Web Socket Protocol Handshake')
self.send_header('Upgrade', 'websocket')
self.send_header('Connection', 'Upgrade')
import hashlib
if ('Sec-WebSocket-Version' not in self.headers
or int(self.headers['Sec-WebSocket-Version'])<8):
self.send_header('Sec-WebSocket-Origin', self.headers['Origin'])
self.send_header('Sec-WebSocket-Location',
'ws://%s%s'%(self.headers['Host'], self.path))
key1 = self.headers['Sec-WebSocket-Key1']
key2 = self.headers['Sec-WebSocket-Key2']
code = self.rfile.read(8)
def websocket_key_calc(key1,key2,code):
i1=int([x for x in key1 if x.isdigit()])/key1.count(' ')
i2=int([x for x in key2 if x.isdigit()])/key2.count(' ')
return hashlib.md5(struct.pack('!II',i1,i2)+code).digest()
self.end_headers()
self.wfile.write(websocket_key_calc(key1,key2,code))
elif int(self.headers['Sec-WebSocket-Version'])>=8:
key = self.headers['Sec-WebSocket-Key']
key = key.encode('utf-8')
magic_guid = b'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
import base64
sha1 = hashlib.sha1(key+magic_guid)
digest = sha1.digest()
result = base64.b64encode(digest)
self.send_header('Sec-WebSocket-Accept', result.decode())
if 'Sec-WebSocket-Protocol' in self.headers:
self.send_header('Sec-WebSocket-Protocol', 'webmapper')
self.end_headers()
self.wfile.flush()
if 'Sec-WebSocket-Version' not in self.headers:
return 0
else:
return int(self.headers['Sec-WebSocket-Version'])
def handler_page(out, args):
htmlfile = open('html/webmapper.html')
htmltext = htmlfile.read()
out.write(htmltext.encode('utf-8'))
def handler_wait_command(out, args):
i=0
ref.inc()
while message_pipe.empty()==False:
time.sleep(0.1)
i = i + 1
if (i>50):
r, w, e=select([out],[],[out], 0)
ref.dec()
if len(r)>0 or len(e)>0:
return
out.write(json.dumps({"id": int(args['id'])}));
return
ref.dec()
r, w, e=select([out],[],[out], 0)
if len(r)>0 or len(e)>0:
return
# Receive command from back-end
msg = message_pipe.get()
if tracing: print('hxr_send:', msg)
out.write(json.dumps({"id": int(args['id']),
"cmd": msg[0],
"args": msg[1]}))
def handler_send_command(out, args):
try:
msgstring = args['msg']
vals = json.loads(msgstring)
h = cmd_handlers[vals['cmd']]
except KeyError:
print('send_command: no message found in "%s"'%str(msgstring))
return
except ValueError as e:
print('send_command: bad embedded JSON "%s"'%msgstring)
raise e
return
res = h(vals['args'])
if res:
out.write(json.dumps({"cmd": res[0],
"args": res[1]}).encode('utf-8'))
def handler_sock(out, args):
pass
def handler_save(out, args):
if not 'save' in cmd_handlers:
out.write(b'\r')
out.write(b'Error, no save handler registered.')
return
f = cmd_handlers['save']
result = f(args)
if result==None:
out.write(b'\r')
out.write(b'Error saving'+args.encode('utf-8'))
return
fn, content = result
out.write(b'Expires: 0')
out.write(b'Cache-Control: no-store')
out.write(b'Content-Description: File Transfer')
out.write(('Content-Disposition: attachment; filename="%s"'%fn).encode('utf-8'))
out.write(b'Content-Type: text/javascript')
out.write(b'Content-Transfer-Encoding: binary')
out.write(('Content-Length: %d'%len(content)).encode('utf-8'))
out.write(b'\r')
out.write(content.encode('utf-8'))
handlers = {'/': [handler_page, 'html'],
'/wait_cmd': [handler_wait_command, 'json'],
'/send_cmd': [handler_send_command, 'json'],
'/chat': [handler_sock, 'socket'],
'/save': [handler_save, 'dl']}
cmd_handlers = {}
def send_command(cmd, args):
message_pipe.put((cmd, args))
def add_command_handler(cmd, handler):
cmd_handlers[cmd] = handler
def serve(port=8000, poll=lambda: time.sleep(10), on_open=lambda: (),
quit_on_disconnect=True):
httpd = ReuseTCPServer(('', port), MprHTTPServer)
on_open()
http_thread = threading.Thread(target=httpd.serve_forever, daemon=True)
http_thread.start()
print("serving at port", port)
try:
while ref.count > 0 or not quit_on_disconnect:
for i in range(100):
poll()
print("Lost connection.")
except KeyboardInterrupt:
pass
print("shutting down...")
httpd.shutdown()
print('bye.')