-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps_tracker.py
535 lines (394 loc) · 10 KB
/
gps_tracker.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/usr/bin/python
from socket import error as socket_error
from socket import timeout as socket_timeout
from Crypto.Random.random import StrongRandom
from Crypto.Cipher import AES
import datetime
import hashlib
import hmac
import os
import random
import signal
import socket
import sqlite3
import struct
import sys
import time
database_file = 'gps_coords_database.sqlite'
aesKey = '0123456789abdef0123456789abdef'
shaKeys = [
'0123456789abcdef',
'123456789abcdef0',
'23456789abcdef01',
'3456789abcdef012',
'456789abcdef0123',
'56789abcdef01234',
'6789abcdef012345',
'789abcdef0123456',
'89abcdef01234567',
'9abcdef012345678',
'abcdef0123456789',
'bcdef0123456789a',
'cdef0123456789ab',
'def0123456789abc',
'ef0123456789abcd',
'f0123456789abcde'
]
hexdigits = {
'0' : 0,
'1' : 1,
'2' : 2,
'3' : 3,
'4' : 4,
'5' : 5,
'6' : 6,
'7' : 7,
'8' : 8,
'9' : 9,
'a' : 10,
'b' : 11,
'c' : 12,
'd' : 13,
'e' : 14,
'f' : 15
}
cbcblock = None
aescomkey = None
myHost = ''
myPort = 7000
datalength = 1024
timeout = 60
conn = None
connInfo = None
connStep = 1
def readSocketLine():
data = ''
while True:
data += conn.recv(datalength)
if len(data) == 0:
break
elif data[-1] == '\n':
break
elif data[-1] == '\r':
break
elif len(data) == datalength:
break
return data.strip()
def doCommandRECVJPG():
fileindex = 1
filename = None
jpgchrarray = []
while True:
filename = 'jpgfile' + str(fileindex).zfill(3) + '.jpg'
if not os.path.exists(filename):
break
fileindex += 1
jpgout = open(filename, 'wb')
conn.send('OK\n')
print 'Receiving JPEG.'
while True:
data = readSocketLine()
if data == 'ENDRECVJPG':
break
if (len(data) > 0) and (not (len(data) % 2)):
for dataindex in range(0, (len(data)/2), 1):
byteval = (hexdigits[data[(dataindex*2)]]*16)+(hexdigits[data[(dataindex*2)+1]])
jpgout.write(chr(byteval))
print 'Receive JPEG complete.'
jpgout.close()
def doCommandRECVCOORDS():
data_tokens = None
conn.send('OK\n')
data = readSocketLine()
data_tokens = data.split(',')
if len(data_tokens) == 8:
gps_date = '000000'
gps_time = '000000'
gps_latitude_degrees = None
gps_latitude_minutes = None
gps_latitude_direction = None
gps_longitude_degrees = None
gps_longitude_minutes = None
gps_longitude_direction = None
gps_altitude_meters = None
try:
gps_date = str(int(data_tokens[0])).zfill(6)
except ValueError:
pass
try:
gps_time = str(int(float(data_tokens[1]))).zfill(6)
except ValueError:
pass
try:
gps_latitude_degrees = float(data_tokens[2])
gps_latitude_degrees = int(gps_latitude_degrees/100)
gps_latitude_minutes = float(data_tokens[2]) - float(gps_latitude_degrees*100)
except ValueError:
pass
try:
gps_latitude_direction = str(data_tokens[3]).strip()
except ValueError:
pass
try:
gps_longitude_degrees = float(data_tokens[4])
gps_longitude_degrees = int(gps_longitude_degrees/100)
gps_longitude_minutes = float(data_tokens[4]) - float(gps_longitude_degrees*100)
except ValueError:
pass
try:
gps_longitude_direction = str(data_tokens[5]).strip()
except ValueError:
pass
try:
gps_altitude_meters = float(data_tokens[6])
except ValueError:
pass
if (
gps_latitude_degrees != None and
gps_latitude_minutes != None and
gps_latitude_direction != None and
gps_longitude_degrees != None and
gps_longitude_minutes != None and
gps_longitude_direction != None
):
dbconn = sqlite3.connect(database_file)
dbconn.cursor().execute("""
INSERT INTO gps_readings (
gps_date_year,
gps_date_month,
gps_date_day,
gps_time_hour,
gps_time_minute,
gps_time_seconds,
gps_latitude_degrees,
gps_latitude_minutes,
gps_latitude_direction,
gps_longitude_degrees,
gps_longitude_minutes,
gps_longitude_direction,
gps_altitude_meters,
server_time
) VALUES (
?,?,?,?,?,?,?,?,?,?,?,?,?,?
);
""", (
str(gps_date).zfill(6)[4:6],
str(gps_date).zfill(6)[2:4],
str(gps_date).zfill(6)[0:2],
str(gps_time).zfill(6)[0:2],
str(gps_time).zfill(6)[2:4],
str(gps_time).zfill(6)[4:6],
gps_latitude_degrees,
gps_latitude_minutes,
gps_latitude_direction,
gps_longitude_degrees,
gps_longitude_minutes,
gps_longitude_direction,
gps_altitude_meters,
datetime.datetime.now()
))
print data
dbconn.commit()
dbconn.close()
dbconn = None
conn.send('OK\n')
else:
conn.send('ERROR\n')
def doClientCommands():
global connStep
data = readSocketLine()
datatokens = data.split(' ')
if datatokens[0] == 'RECVCOORDS':
doCommandRECVCOORDS()
elif datatokens[0] == 'RECVJPG':
doCommandRECVJPG()
elif datatokens[0] == 'BYE':
connStep = 0
def doCommandHELLO():
global connStep, cbcblock, aescomkey
cbcblock = []
aescomkey = []
conn.send('HELLO\n')
print 'Sent HELLO'
print datetime.datetime.now()
data = readSocketLine()
if data == 'HELLO':
connStep += 1
return 1
connStep = 0
return 0
def doCommandSENDCOORDS():
global connStep
conn.send('SENDCOORDS\n')
print 'Requested SENDCOORDS'
data = readSocketLine()
if data == 'RECVCOORDS':
doCommandRECVCOORDS()
data = readSocketLine()
if data == 'OK':
connStep += 1
return 1
connStep = 0
return 0
def doCommandCHALLENGE():
global connStep, cbcblock, aescomkey
challengeFail = False
strrand = StrongRandom()
sysrand = random.SystemRandom()
cipher = AES.new(aesKey, AES.MODE_ECB)
for challengeCounter in range(0, 32, 1):
aesblock1 = ''
aesblock2 = ''
msge = ''
for i in range(0, AES.block_size, 1):
aesblock1 += struct.pack('B', (strrand.getrandbits(8)^sysrand.randint(0, 255)))
aesblock2 += struct.pack('B', (strrand.getrandbits(8)^sysrand.randint(0, 255)))
randsalt = aesblock1 + aesblock2
if challengeCounter < 16:
cbcblock.append(aesblock1[(ord(aesblock2[0])%16)])
aescomkey.append(aesblock2[(ord(aesblock1[0])%16)])
aesblock1 = cipher.encrypt(aesblock1)
aesblocktemp = aesblock2
aesblock2 = ''
for i in range(0, AES.block_size, 1):
aesblock2 += chr(ord(aesblock1[i])^ord(aesblocktemp[i]))
aesblock2 = cipher.encrypt(aesblock2)
msge += aesblock1.encode('hex')
msge += aesblock2.encode('hex')
challengeMsg = None
challengeMsg = 'CHALLENGE ' + msge + '\n'
conn.send(challengeMsg)
hashchal = None
hashchal = hmac.new(shaKeys[int(challengeCounter/2)], randsalt, hashlib.sha256)
data = readSocketLine()
datatokens = data.split(' ')
if len(datatokens) != 2 or datatokens[0] != 'RESPONSE':
datatokens = (None, None)
aesblocktemp = aesblock2
aesblock1 = ''
aesblock2 = ''
for i in range(0, AES.block_size, 1):
aesblock1 += chr(ord(hashchal.digest()[i])^ord(aesblocktemp[i]))
aesblock1 = cipher.encrypt(aesblock1)
for i in range(0, AES.block_size, 1):
aesblock2 += chr(ord(hashchal.digest()[AES.block_size+i])^ord(aesblock1[i]))
aesblock2 = cipher.encrypt(aesblock2)
aesblocktemp = aesblock1 + aesblock2
if datatokens[1] != aesblocktemp.encode('hex'):
print
print 'Failed challenge ' + str(challengeCounter+1) + '.'
print shaKeys[int(challengeCounter/2)] + '\t' + randsalt.encode('hex')
print data
print
challengeFail = True
else:
print 'Passed challenge ' + str(challengeCounter+1) + '.'
challengeCounter += 1
if challengeFail:
print 'Authentication failed.'
conn.send('CHALLENGEFAIL\n')
data = readSocketLine()
else:
print 'Authentication successful.'
#print ''.join(cbcblock).encode('hex')
#print ''.join(aescomkey).encode('hex')
conn.send('CHALLENGEPASS\n')
data = readSocketLine()
if data == 'OK':
connStep += 1
return 1
connStep = 0
return 0
def doCommandOKCLIENT():
conn.send('OKCLIENT\n')
doClientCommands()
def doCommandBYE():
global connStep
conn.send('BYE\n')
conn.close()
print 'Connection closed.'
time.sleep(1)
connStep = 1
return 1
def serverCommands():
global connStep
if connStep == 1:
return doCommandHELLO()
elif connStep == 2:
#return doCommandSENDCOORDS()
connStep += 1
elif connStep == 3:
#return doCommandCHALLENGE()
connStep += 1
elif connStep == 4:
return doCommandOKCLIENT()
elif connStep == 0:
return doCommandBYE()
else:
connStep = 0
def clientCommands():
pass
if __name__ == '__main__':
def sigHandler(signum = None, frame = None):
print 'Signal handler called with signal', signum
conn.close()
time.sleep(1)
sys.exit(0)
dbconn = sqlite3.connect(database_file)
try:
dbconn.cursor().execute("""
CREATE TABLE gps_readings (
gps_date_year INTEGER,
gps_date_month INTEGER,
gps_date_day INTEGER,
gps_time_hour INTEGER,
gps_time_minute INTEGER,
gps_time_seconds INTEGER,
gps_latitude_degrees REAL,
gps_latitude_minutes REAL,
gps_latitude_direction TEXT,
gps_longitude_degrees REAL,
gps_longitude_minutes REAL,
gps_longitude_direction TEXT,
gps_altitude_meters REAL,
server_time TEXT
);
""")
dbconn.commit()
except sqlite3.OperationalError, oer:
if oer.args[0] == 'table gps_readings already exists':
print('Create table skipped')
else:
print(oer.args[0])
dbconn.close()
dbconn = None
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create a TCP socket
socket.bind((myHost, myPort)) # bind it to the server port
socket.listen(1) # pending connections
for sigIn in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
signal.signal(sigIn, sigHandler)
while True:
try:
# wait for next client to connect
conn, connInfo = socket.accept() # connection is a new socket
conn.settimeout(timeout)
print "Received connection from " + connInfo[0]
while True:
serverCommands()
if connStep == 1:
break
except socket_timeout, stim:
print 'Socket timed out.'
#doCommandBYE()
conn.close()
connStep = 1
except socket_error, serr:
if serr[0] == 104:
print 'Received socket.error: (104, "Connection reset by peer")'
connStep = 0
elif serr[0] == 32:
print 'Received socket.error: (32, "Broken pipe")'
conn.close()
connStep = 1
else:
raise serr