-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_server.py
366 lines (284 loc) · 8.5 KB
/
web_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
#! /usr/bin/env python3
# TODO
#
# ? make header receive timeout hardcoded
#
# ? make the file uploading and downloading into separate functions
#
# make a function for generic 1-line responses
#
# make some sort of admin interface
#
# generic `recv` function
#
# setting all async sleeps to 0 increases file transfer speed from about 90 to about 130
# see if the sleeps can be tuned for better performance
#
# add support for SSL
##########
########## determine platform
##########
import os
RP = os.uname().sysname == 'rp2'
##########
########## imports
##########
import socket
import time
import sys
if RP:
import machine
from machine import Pin
import network
import uasyncio as asyncio
import _thread
led = Pin("LED", Pin.OUT)
else:
import asyncio
import threading
##########
########## server related defines
##########
DEBUG = True
PAGE_FOLDER = f'page'
SCRIPT_FOLDER = f'script'
WIFI_SSID_FILE = f'wifi-ssid'
WIFI_PASS_FILE = f'wifi-pass'
LED_WIFI_CONNECT = 0.7
BIND_PORT = 80 if RP else 8080
SOCK_LISTEN = 5
SERVING_THREADS = 5 # setting this to 5 or 3 doesn't seem to change the download speed (on the board)
MAIN_LOOP_SLEEP = 1_000
SOCK_ACCEPT_SLEEP = 0.1
RECV_HEADER_BYTE_SLEEP = 0.01
RECV_HEADER_FIRST_LINE_TIMEOUT = 2.4
RECV_REST_OF_HEADER_TIMEOUT = 4
SEND_GENERIC_RESPONSE_MESSAGE_TIMEOUT = 1
SEND_HTTP_HEADER_DATA_TIMEOUT = 1
SEND_SLEEP = 0
FILE_READ_CHUNK = 1024 * 5
FILE_SEND_CHUNK_TIMEOUT = 1
SCRIPT_EXTENSION = 'fnc'
##########
########## generic defines
##########
if RP:
INODE_TYPE_FOLDER = 0x4000
INODE_TYPE_FILE = 0x8000
##########
########## classes
##########
class Shared_data: pass
NetworkReceiveBlockingError = OSError if RP else BlockingIOError
class MaliciousClientError(Exception): pass
##########
########## functions
##########
######
###### generic
def connect_to_internet():
if RP:
with open(WIFI_SSID_FILE, 'r') as f:
ssid = f.read()
with open(WIFI_PASS_FILE, 'r') as f:
password = f.read()
print(f'trying to connect to wifi `{ssid}`...')
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
led.toggle()
time.sleep(LED_WIFI_CONNECT)
print('connected')
ip, subnet, gateway, dns = wlan.ifconfig()
else:
# assume we already have internet
ip = '127.0.0.1'
print(f'assigned ip `{ip}`')
toggle_led = (lambda:led.toggle()) if RP else (lambda:None)
def create_lock():
if RP:
return _thread.allocate_lock()
else:
return threading.Lock()
def does_file_exist(path):
if RP:
try:
info = os.stat(path)
except OSError:
return False # such entity does not exist
type_ = info[0]
return type_ == INODE_TYPE_FILE
### Old implementation
# try:
# open(path, 'rb')
# except OSError:
# return False
# return True
else:
return os.path.isfile(path)
async def send(con, data, timeout):
start = time.time()
while data:
if time.time() - start > timeout:
raise MaliciousClientError('slow download')
if RP:
try:
sent = con.send(data)
except OSError:
await asyncio.sleep(SEND_SLEEP)
continue
else:
try:
sent = con.send(data)
except BrokenPipeError:
raise MaliciousClientError('connection dropped by client')
data = data[sent:]
await asyncio.sleep(SEND_SLEEP)
######
###### server generic
#### receive
async def recv_header_line(con, timeout, discard=False):
end = b'\r\n'
start = time.time()
data = b''
while True:
remain = timeout - (time.time() - start)
if remain <= 0:
raise MaliciousClientError('upload too slow')
try:
byte = con.recv(1)
except NetworkReceiveBlockingError:
await asyncio.sleep(RECV_HEADER_BYTE_SLEEP)
continue
data += byte
if data.endswith(end):
break
if discard and len(data) > 3:
# leave 1 character so that the caller knows if this was an empty line
data = data[-3:]
data = data[:-len(end)]
data = data.decode()
return data
#### send
async def send_http_ok(con):
await send(con, b'HTTP/1.1 200 OK\n', SEND_HTTP_HEADER_DATA_TIMEOUT)
async def send_http_not_found(con):
await send(con, b'HTTP/1.1 404 Not Found\n', SEND_HTTP_HEADER_DATA_TIMEOUT)
async def send_http_error(con):
await send(con, b'HTTP/1.1 500 Internal Server Error\n', SEND_HTTP_HEADER_DATA_TIMEOUT) # TODO it is OK to have spaces here?
async def send_http_end_of_header(con):
await send(con, b'\n', SEND_HTTP_HEADER_DATA_TIMEOUT)
######
###### server specific
async def serve_content_request(con, page):
if page == '/':
page = '/index.html'
file = PAGE_FOLDER + page
if not does_file_exist(file):
await send_http_not_found(con)
await send_http_end_of_header(con)
await send(con, b'404', SEND_GENERIC_RESPONSE_MESSAGE_TIMEOUT)
return
await send_http_ok(con)
await send_http_end_of_header(con)
with open(file, 'rb') as f:
chunk = f.read(FILE_READ_CHUNK)
await send(con, chunk, FILE_SEND_CHUNK_TIMEOUT)
async def serve_script_request(share, con, page):
script_name = page
if script_name.startswith('/'):
script_name = script_name[1:]
file = SCRIPT_FOLDER + page + '.py'
if not does_file_exist(file):
return
sys.path.insert(0, SCRIPT_FOLDER) # this seems iffy, but we have already ensured that this file exists, therefore it will be imported
try:
script = __import__(script_name)
finally:
del sys.path[0]
try:
script = script.main
except AttributeError:
print(f'ERROR: bad script: `{file}`')
return
await script(share, con) # TODO what if main is not async ?
async def __serve_requests(share, con, addr):
header = await recv_header_line(con, RECV_HEADER_FIRST_LINE_TIMEOUT)
if header.count(' ') != 2:
raise MaliciousClientError('bad header format')
method, page, proto = header.split(' ')
start = time.time()
while True:
remain = RECV_REST_OF_HEADER_TIMEOUT - (time.time() - start)
line = await recv_header_line(con, remain, discard=True)
if not line:
break
if '..' in page:
# TODO not the best solution
raise MaliciousClientError('cd')
if not page.startswith('/'):
page = '/' + page
if method == 'GET':
await serve_content_request(con, page)
elif method == 'POST':
await serve_script_request(share, con, page)
else:
raise MaliciousClientError('bad method')
async def _serve_requests(sock, share):
#print('waiting for connection')
while True:
try:
con, addr = sock.accept()
except NetworkReceiveBlockingError:
await asyncio.sleep(SOCK_ACCEPT_SLEEP)
else:
break
#print('connection!')
toggle_led()
try:
await __serve_requests(share, con, addr)
finally:
con.close()
async def serve_requests(sock, share):
while True:
try:
await _serve_requests(sock, share)
except MaliciousClientError as err:
print(f'malicious client: {err}')
if DEBUG:
asyncio.create_task(serve_requests(sock, share))
raise
except:
asyncio.create_task(serve_requests(sock, share))
raise
async def _main(sock):
#sock = ssl.wrap_socket(
#sock,
#keyfile=KEYFILE,
#certfile=CERTFILE,
#server_side=True,
#ssl_version=SSL_VERSION,
#do_handshake_on_connect=True
#)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print(f'trying to bind to port {BIND_PORT}')
sock.bind(('', BIND_PORT))
print('bound')
sock.listen(SOCK_LISTEN)
sock.setblocking(False)
share = Shared_data()
for _ in range(SERVING_THREADS):
asyncio.create_task(serve_requests(sock, share))
while True:
await asyncio.sleep(MAIN_LOOP_SLEEP)
def main():
connect_to_internet()
sock = socket.socket()
try:
asyncio.run(_main(sock))
except KeyboardInterrupt:
pass
sock.close()
if __name__ == '__main__':
main()