forked from EstevanTH/CAN-Monitoring_ELM327
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CANCaptureHTTPServer.py
422 lines (394 loc) · 12.4 KB
/
CANCaptureHTTPServer.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
415
416
417
418
419
420
421
422
import http.server
import threading
from io import BytesIO
from utility import simpleDictionaryToJSON
from utility import printT
from utility import bytes_hex
from time import time
from socketserver import ThreadingMixIn
from websocket import WebSocketBadRequest, WebSocket
from json import dumps as json_dumps
from json import loads as json_loads
from urllib.parse import parse_qs
from traceback import format_exc
try:
from http import HTTPStatus
except ImportError:
import http.client as HTTPStatus # Python36 -> Python34
class WebSocket_frames(WebSocket):
maxReceivedLen = 125 # only short frames
allowFramesBinary = False
allowFramesText = False
def handleMessage( self, data ):
""" Incoming message handler: nothing allowed """
pass
@classmethod
def broadcastFrame( self, frame ):
""" Broadcast a new frame (as JSON) - possible bottleneck """
self.broadcastMessageText( simpleDictionaryToJSON( {
b"t": frame.time,
b"i": frame.identifier,
b"e": frame.isExtended,
b"r": frame.isRTR,
b"l": frame.DLC,
b"d": bytes_hex( frame.data ),
} ) )
class Status( Exception ):
status = 0
text = "Something occurred"
def __init__( self ):
Exception.__init__( self, self.text )
class StatusBadRequest( Status ):
status = HTTPStatus.BAD_REQUEST
text = "Bad Request"
class StatusNotFound( Status ):
status = HTTPStatus.NOT_FOUND
text = "Not Found"
class StatusMethodNotAllowed( Status ):
status = HTTPStatus.METHOD_NOT_ALLOWED
text = "Method Not Allowed"
class StatusLengthRequired( Status ):
status = HTTPStatus.LENGTH_REQUIRED
text = "Length Required"
class StatusPayloadTooLarge( Status ):
status = HTTPStatus.REQUEST_ENTITY_TOO_LARGE
text = "Payload Too Large"
class CANCaptureHTTPRequestHandler( http.server.BaseHTTPRequestHandler ):
# Based on http.server.SimpleHTTPRequestHandler
server_version = "ELM327 CAN bus monitoring"
protocol_version = "HTTP/1.1" # mandatory for WebSocket
runWebSocket = False
webSocketClass = None
staticFiles = {
"/show_dataframes.css": u"www/show_dataframes.css",
"/show_dataframes.htm": u"www/show_dataframes.htm",
"/show_dataframes.setup.js": u"www/show_dataframes.setup.js",
"/show_dataframes.frames.js": u"www/show_dataframes.frames.js",
}
maxPostLength = 65536
def do_GET( self ):
f = None
try:
f = self.send_head()
self.copyfile( f, self.wfile )
except ConnectionError:
return
finally:
if f is not None:
f.close()
del f
if self.runWebSocket:
try:
ws = self.webSocketClass( self )
ws.run()
except ConnectionError:
pass
except StopIteration:
pass
except WebSocketBadRequest as e:
printT( repr( e ) )
return
def do_HEAD( self ):
f = None
try:
f = self.send_head( headersOnly=True )
except ConnectionError:
return
finally:
if f is not None:
f.close()
del f
do_POST = do_GET
@classmethod
def postFieldToIdentifier( cls, identifier ):
try:
if isinstance( identifier, str ):
if not len( identifier ):
# empty string
identifier = None
else:
# hexadecimal string
identifier = int( identifier, base=16 )
if identifier>0x1FFFFFFF or identifier<0:
raise ValueError()
else:
# should be a number
identifier = int( identifier )
except TypeError:
raise StatusBadRequest()
except ValueError:
raise StatusBadRequest()
return identifier
@classmethod
def postFieldToIdentifiersSet( cls, postData, fieldName ):
identifiers = postData[fieldName]
if isinstance( identifiers, list ):
# list of identifiers
identifiers_temp = set( identifiers )
elif not len( identifiers ):
# empty string => None
identifiers_temp = None
else:
# single identifier
identifiers_temp = {identifiers}
if identifiers_temp is None:
identifiers = None
else:
identifiers = set()
for identifier in identifiers_temp:
identifiers.add( cls.postFieldToIdentifier( identifier ) )
return identifiers
def send_head( self, headersOnly=False ):
headers = {}
currentThread = threading.currentThread()
currentThread.name = "%s %s"%(self.command, self.path)
path = self.path.split( "?", 1 )[0]
if path=="/frames.ws":
self.webSocketClass = WebSocket_frames
info = WebSocket.prepareHeaders( self )
encoded = info["encoded"]
response = info["response"]
contentType = info["contentType"]
headers = info["headers"]
elif path=="/threads.txt":
contentLines = [b"List of known running threads:"]
currentThread = threading.currentThread()
for thread in threading.enumerate():
if thread is currentThread:
contentLines.append( ( "%5u: This request"%(
thread.ident or 0,
) ).encode( "utf_8", errors="replace" ) )
else:
threadModuleName = str( thread.run.__func__.__module__ )
threadClassName = type( thread ).__name__
threadTargetClassName = None
if type( thread ) is threading.Thread: # is not of a meaningful class
try:
threadTargetClass = type( thread._target.__self__ )
threadModuleName = str( threadTargetClass.__module__ )
threadClassName = str( threadTargetClass.__name__ )
except:
pass
contentLines.append( ( "%5u: %s.%s(%s, %s) [%s]"%(
thread.ident or 0,
threadModuleName,
threadClassName,
str( thread._args )[1:-1],
str( thread._kwargs )[1:-1],
thread.name,
) ).encode( "utf_8", errors="replace" ) )
contentLines.append( b"\r\nOn Windows the CPU usage can be monitored for each thread using SysInternals Autoruns." )
encoded = b"\r\n".join( contentLines )
response = HTTPStatus.OK
contentType = "text/plain"
elif path[:5]=="/api/":
contentType = "application/json"
response = HTTPStatus.OK
errorString = None
data = {}
canBus = self.server.thread.canBus
try:
postData = None
if self.command=="POST":
postContentLength = None
try:
postContentLength = int( self.headers.get( "Content-Length" ) )
except TypeError:
pass
except ValueError:
pass
if postContentLength is None or postContentLength<0:
raise StatusLengthRequired()
if postContentLength>self.maxPostLength:
raise StatusPayloadTooLarge()
postPayload = self.rfile.read( postContentLength )
postContentType = self.headers.get( "Content-Type", "application/x-www-form-urlencoded" )
if postContentType[:16]=="application/json":
try:
postData = json_loads( postPayload.decode( "utf_8" ) )
except ValueError:
pass
elif postContentType[:33]=="application/x-www-form-urlencoded":
postData_ = None
try:
postData_ = parse_qs( postPayload.decode( "ascii" ), keep_blank_values=True, strict_parsing=True )
except ValueError:
pass
if postData_ is not None:
postData = {}
for pair in postData_.items():
# dict of lists => dict of {str and lists}
if pair[0][-2:]=="[]":
# explicit list
postData[pair[0][:-2]] = pair[1]
elif len( pair[1] )==1:
# single str
postData[pair[0]] = pair[1][0]
else:
# implicit list
postData[pair[0]] = pair[1]
del postData_
if postData is None:
raise StatusBadRequest()
# Note: each POST data can be str or list or int!
if path=="/api/filter1/installByMask":
if postData is None:
raise StatusMethodNotAllowed()
try:
mask = self.postFieldToIdentifier( postData["mask"] )
maskingResult = self.postFieldToIdentifier( postData["maskingResult"] )
except KeyError:
raise StatusBadRequest()
canBus.setFilter1Remote( mask, maskingResult )
elif path=="/api/filter1/installByIds":
if postData is None:
raise StatusMethodNotAllowed()
try:
whitelist = self.postFieldToIdentifiersSet( postData, "whitelist" )
except KeyError:
raise StatusBadRequest()
if whitelist is None:
# only vanish whitelist
canBus.setFilter1Local( None )
else:
# set filter as well
canBus.setFilter1( whitelist )
elif path=="/api/filter1/getInstalled":
maskInfo = canBus.getFilter1Remote()
data["mask"] = maskInfo[0]
data["maskingResult"] = maskInfo[1]
del maskInfo
whitelist = canBus.getFilter1Local()
whitelist = ( whitelist is not None ) and list( whitelist ) or None
data["whitelist"] = whitelist
del whitelist
elif path=="/api/filter1/reset":
if postData is None:
raise StatusMethodNotAllowed()
canBus.setFilter1( None )
elif path=="/api/filter2/setExcluded":
if postData is None:
raise StatusMethodNotAllowed()
try:
blacklist = self.postFieldToIdentifiersSet( postData, "blacklist" )
except KeyError:
raise StatusBadRequest()
canBus.setFilter2( blacklist )
elif path=="/api/filter2/getExcluded":
blacklist = canBus.getFilter2()
blacklist = ( blacklist is not None ) and list( blacklist ) or None
data["blacklist"] = blacklist
del blacklist
elif path=="/api/filter2/reset":
if postData is None:
raise StatusMethodNotAllowed()
canBus.setFilter2( None )
elif path=="/api/setInactivityTimeout":
if postData is None:
raise StatusMethodNotAllowed()
try:
inactivityTimeout = float( postData["timeout"] )
except TypeError:
raise StatusBadRequest()
except ValueError:
raise StatusBadRequest()
if inactivityTimeout<=0.:
raise StatusBadRequest()
canBus.inactivityTimeout = inactivityTimeout
elif path=="/api/getInactivityTimeout":
data["timeout"] = canBus.inactivityTimeout
else:
raise StatusNotFound()
except Status as e:
response = e.status
errorString = e.text
except Exception as e:
printT( format_exc() )
response = HTTPStatus.INTERNAL_SERVER_ERROR
errorString = repr( e )
del canBus
if response==HTTPStatus.OK:
data["status"] = 200
else:
data = {
"status": int( response ),
"error": errorString,
}
encoded = json_dumps( data ).encode( "utf_8", errors="replace" )
elif path in self.staticFiles:
diskFileExcept = None
try:
diskFile = open( self.staticFiles[path], "rb" )
encoded = diskFile.read()
diskFile.close()
response = HTTPStatus.OK
ext_4 = path[-5:].lower()
ext_3 = ext_4[-4:]
ext_2 = ext_4[-3:]
contentType = "application/octet-stream"
if ext_3==".htm" or ext_4==".html":
contentType = "text/html; charset=utf-8"
elif ext_2==".js":
contentType = "text/javascript"
elif ext_3==".css":
contentType = "text/css"
except FileNotFoundError as e:
diskFileExcept = e
response = HTTPStatus.NOT_FOUND
except PermissionError as e:
diskFileExcept = e
response = HTTPStatus.FORBIDDEN
except Exception as e:
printT( format_exc() )
diskFileExcept = e
response = HTTPStatus.INTERNAL_SERVER_ERROR
if diskFileExcept:
encoded = repr( diskFileExcept ).encode( "utf_8", "replace" )
contentType = "text/plain"
else:
encoded = b"Not found"
response = HTTPStatus.NOT_FOUND
contentType = "text/plain"
f = BytesIO()
f.write( encoded )
f.seek( 0 )
self.send_response( response )
if contentType is not None:
self.send_header( "Content-type", contentType )
if headersOnly:
self.send_header( "Content-Length", "0" )
else:
self.send_header( "Content-Length", str( len( encoded ) ) )
for header in headers.items():
self.send_header( header[0], header[1] )
self.send_header( "Access-Control-Allow-Origin", "*" )
self.end_headers()
return f
copyfile = http.server.SimpleHTTPRequestHandler.copyfile
def log_request( self, code='-', size='-' ):
pass # no logging for successful requests
class ThreadedHTTPServer(ThreadingMixIn, http.server.HTTPServer):
daemon_threads = True
class CANCaptureHTTPServerThread( threading.Thread ):
daemon = True # exit immediatly on program exit
def __init__( self, vehicleData, canBus, ipAddress="127.0.0.1", tcpPort=8327 ):
threading.Thread.__init__( self )
if ":" in ipAddress:
# IPv6
self.name = "[%s]:%u"%(ipAddress, tcpPort)
else:
# IPv4
self.name = "%s:%u"%(ipAddress, tcpPort)
self.canBus = canBus
self.ipAddress = ipAddress
self.tcpPort = tcpPort
def getParameters( self ):
return {
"ipAddress": self.ipAddress,
"tcpPort": self.tcpPort,
}
def run( self ):
httpd = ThreadedHTTPServer( (self.ipAddress, self.tcpPort), CANCaptureHTTPRequestHandler )
httpd.thread = self
printT( "CANCaptureHTTPServerThread started:", self.ipAddress, self.tcpPort )
httpd.serve_forever()