This repository has been archived by the owner on Dec 22, 2024. It is now read-only.
forked from dgrant/eyefiserver2
-
Notifications
You must be signed in to change notification settings - Fork 3
/
eyefiserver.py
executable file
·673 lines (534 loc) · 27.5 KB
/
eyefiserver.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#!/usr/bin/env python
"""
* Copyright (c) 2009, Jeffrey Tchang
* Additional *pike
* All rights reserved.
*
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import BaseHTTPServer
import SocketServer
import binascii
import cgi
import errno
import hashlib
import httplib
import logging.handlers
import math
import os
import random
import select
import socket
import sys
import tarfile
import tempfile
import time
import traceback
import xml.dom.minidom
import xml.sax
from BaseHTTPServer import BaseHTTPRequestHandler
from datetime import datetime
from datetime import timedelta
from StringIO import StringIO
from xml.sax.handler import ContentHandler
"""
General architecture notes
This is a standalone Eye-Fi Server that is designed to take the place of the Eye-Fi Manager.
Starting this server creates a listener on port 59278. I use the BaseHTTPServer class included
with Python. I look for specific POST/GET request URLs and execute functions based on those
URLs.
"""
# Create the main logger
eyefi_logger = logging.Logger("eyefi_logger", logging.DEBUG)
# TODO: set logging level with env variable
# Create two handlers. One to print to the log and one to print to the console
consoleHandler = logging.StreamHandler(sys.stdout)
# Set how both handlers will print the pretty log events
eyeFiLoggingFormat = logging.Formatter("[%(asctime)s][%(funcName)s] - %(message)s", '%m/%d/%y %I:%M%p')
consoleHandler.setFormatter(eyeFiLoggingFormat)
# Append both handlers to the main Eye Fi Server logger
eyefi_logger.addHandler(consoleHandler)
# Eye Fi XML SAX ContentHandler
class EyeFiContentHandler(ContentHandler):
# These are the element names that I want to parse out of the XML
element_names_to_extract = ["macaddress", "cnonce", "transfermode", "transfermodetimestamp", "fileid", "filename",
"filesize", "filesignature"]
# For each of the element names I create a dictionary with the value to False
elements_to_extract = {}
# Where to put the extracted values
extracted_elements = {}
def __init__(self):
ContentHandler.__init__(self)
self.extracted_elements = {}
for element_name in self.element_names_to_extract:
self.elements_to_extract[element_name] = False
def startElement(self, name, attributes):
# If the name of the element is a key in the dictionary elements_to_extract
# set the value to True
eyefi_logger.debug("startElement: " + name)
if name in self.elements_to_extract:
self.elements_to_extract[name] = True
eyefi_logger.debug("Found element: " + name)
def endElement(self, name):
# If the name of the element is a key in the dictionary elements_to_extract
# set the value to False
if name in self.elements_to_extract:
self.elements_to_extract[name] = False
def characters(self, content):
for element_name in self.elements_to_extract:
if self.elements_to_extract[element_name]:
self.extracted_elements[element_name] = content
# Implements an EyeFi server
class EyeFiServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
def serve_forever(self, **kwargs):
while True:
try:
self.handle_request()
except select.error, e:
if e[0] != errno.EINTR:
raise e
def server_bind(self):
BaseHTTPServer.HTTPServer.server_bind(self)
self.socket.settimeout(None)
def get_request(self):
try:
connection, address = self.socket.accept()
eyefi_logger.debug("Incoming connection from client %s" % address[0])
connection.settimeout(None)
return connection, address
except socket.timeout:
self.socket.close()
pass
# This class is responsible for handling HTTP requests passed to it.
# It implements the two most common HTTP methods, do_GET() and do_post()
class EyeFiRequestHandler(BaseHTTPRequestHandler):
protocol_version = 'HTTP/1.1'
sys_version = ""
server_version = "Eye-Fi Agent/2.0.4.0 (Windows XP SP2)"
def do_QUIT(self):
eyefi_logger.debug("Got StopServer request .. stopping server")
self.send_response(200)
self.end_headers()
self.server.stop()
def do_GET(self):
try:
eyefi_logger.debug(self.command + " " + self.path + " " + self.request_version)
eyefi_logger.debug("Headers received in GET request:")
for headerName in self.headers.keys():
for headerValue in self.headers.getheaders(headerName):
eyefi_logger.debug(headerName + ": " + headerValue)
self.send_response(200)
self.send_header('Content-type', 'text/html')
# I should be sending a Content-Length header with HTTP/1.1 but I am being lazy
# self.send_header('Content-length', '123')
self.end_headers()
self.wfile.write(self.client_address)
self.wfile.write(self.headers)
except:
eyefi_logger.error("Got an an exception:")
eyefi_logger.error(traceback.format_exc())
raise
def do_POST(self):
try:
eyefi_logger.debug(self.command + " " + self.path + " " + self.request_version)
soap_action = ""
content_length = ""
# Loop through all the request headers and pick out ones that are relevant
eyefi_logger.debug("Headers received in POST request:")
for headerName in self.headers.keys():
for headerValue in self.headers.getheaders(headerName):
if headerName == "soapaction":
soap_action = headerValue
if headerName == "content-length":
content_length = int(headerValue)
eyefi_logger.debug(headerName + ": " + headerValue)
# Read content_length bytes worth of data
eyefi_logger.debug("Attempting to read " + str(content_length) + " bytes of data")
chunk_size = 1048576 # 1MB
mem_file = StringIO()
while 1:
remain = content_length - mem_file.tell()
if remain <= 0:
break
chunk = self.rfile.read(min(chunk_size, remain))
if not chunk:
break
mem_file.write(chunk)
post_data = mem_file.getvalue()
mem_file.close()
eyefi_logger.debug("Finished reading " + str(content_length) + " bytes of data")
# Perform action based on path and soap_action
# A soap_action of StartSession indicates the beginning of an EyeFi
# authentication request
if (self.path == "/api/soap/eyefilm/v1") and (soap_action == "\"urn:StartSession\""):
eyefi_logger.debug("Got StartSession request")
response = self.start_session(post_data)
content_length = len(response)
eyefi_logger.debug("StartSession response: " + response)
self.send_response(200)
self.send_header('Date', self.date_time_string())
self.send_header('Pragma', 'no-cache')
self.send_header('Server', 'Eye-Fi Agent/2.0.4.0 (Windows XP SP2)')
self.send_header('Content-Type', 'text/xml; charset="utf-8"')
self.send_header('Content-Length', content_length)
self.end_headers()
self.wfile.write(response)
self.wfile.flush()
self.handle_one_request()
# GetPhotoStatus allows the card to query if a photo has been uploaded
# to the server yet
if (self.path == "/api/soap/eyefilm/v1") and (soap_action == "\"urn:GetPhotoStatus\""):
eyefi_logger.debug("Got GetPhotoStatus request")
response = self.get_photo_status(post_data)
content_length = len(response)
eyefi_logger.debug("GetPhotoStatus response: " + response)
self.send_response(200)
self.send_header('Date', self.date_time_string())
self.send_header('Pragma', 'no-cache')
self.send_header('Server', 'Eye-Fi Agent/2.0.4.0 (Windows XP SP2)')
self.send_header('Content-Type', 'text/xml; charset="utf-8"')
self.send_header('Content-Length', content_length)
self.end_headers()
self.wfile.write(response)
self.wfile.flush()
# If the URL is upload and there is no soap_action the card is ready to send a picture to me
if (self.path == "/api/soap/eyefilm/v1/upload") and (soap_action == ""):
eyefi_logger.debug("Got upload request")
response = self.upload_photo(post_data)
content_length = len(response)
eyefi_logger.debug("Upload response: " + response)
self.send_response(200)
self.send_header('Date', self.date_time_string())
self.send_header('Pragma', 'no-cache')
self.send_header('Server', 'Eye-Fi Agent/2.0.4.0 (Windows XP SP2)')
self.send_header('Content-Type', 'text/xml; charset="utf-8"')
self.send_header('Content-Length', content_length)
self.end_headers()
self.wfile.write(response)
self.wfile.flush()
# If the URL is upload and soap_action is MarkLastPhotoInRoll
if (self.path == "/api/soap/eyefilm/v1") and (soap_action == "\"urn:MarkLastPhotoInRoll\""):
eyefi_logger.debug("Got MarkLastPhotoInRoll request")
response = self.mark_last_photo_in_roll()
content_length = len(response)
eyefi_logger.debug("MarkLastPhotoInRoll response: " + response)
self.send_response(200)
self.send_header('Date', self.date_time_string())
self.send_header('Pragma', 'no-cache')
self.send_header('Server', 'Eye-Fi Agent/2.0.4.0 (Windows XP SP2)')
self.send_header('Content-Type', 'text/xml; charset="utf-8"')
self.send_header('Content-Length', content_length)
self.send_header('Connection', 'Close')
self.end_headers()
self.wfile.write(response)
self.wfile.flush()
eyefi_logger.debug("Connection closed.")
except:
eyefi_logger.error("Got an an exception:")
eyefi_logger.error(traceback.format_exc())
raise
# Handles MarkLastPhotoInRoll action
@staticmethod
def mark_last_photo_in_roll():
# Create the XML document to send back
doc = xml.dom.minidom.Document()
soap_element = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "SOAP-ENV:Envelope")
soap_element.setAttribute("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
soap_body_element = doc.createElement("SOAP-ENV:Body")
mark_last_photo_in_roll_response_element = doc.createElement("MarkLastPhotoInRollResponse")
soap_body_element.appendChild(mark_last_photo_in_roll_response_element)
soap_element.appendChild(soap_body_element)
doc.appendChild(soap_element)
return doc.toxml(encoding="UTF-8")
# Handles receiving the actual photograph from the card.
# post_data will most likely contain multipart binary post data that needs to be parsed
def upload_photo(self, post_data):
# Take the post_data string and work with it as if it were a file object
post_data_in_memory_file = StringIO(post_data)
# Get the content-type header which looks something like this
# content-type: multipart/form-data; boundary=---------------------------02468ace13579bdfcafebabef00d
content_type_header = self.headers.getheaders('content-type').pop()
eyefi_logger.debug(content_type_header)
# Extract the boundary parameter in the content-type header
header_parameters = content_type_header.split(";")
eyefi_logger.debug(header_parameters)
boundary = header_parameters[-1].split("=")
boundary = boundary[1].strip()
eyefi_logger.debug("Extracted boundary: " + boundary)
# Parse the multipart/form-data
form = cgi.parse_multipart(post_data_in_memory_file, {"boundary": boundary,
"content-disposition": self.headers.getheaders(
'content-disposition')})
eyefi_logger.debug("Available multipart/form-data: " + str(form.keys()))
# Parse the SOAPENVELOPE using the EyeFiContentHandler()
soap_envelope = form['SOAPENVELOPE'][0]
eyefi_logger.debug("SOAPENVELOPE: " + soap_envelope)
handler = EyeFiContentHandler()
xml.sax.parseString(soap_envelope, handler)
eyefi_logger.debug("Extracted elements: " + str(handler.extracted_elements))
image_tarfile_name = handler.extracted_elements["filename"]
geotag_enable = int(self.server.config['geotag_enable'])
geotag_accuracy = int(self.server.config['geotag_accuracy'])
image_tar_path = os.path.join(tempfile.gettempdir(), image_tarfile_name)
eyefi_logger.debug("Generated path " + image_tar_path)
file_handle = open(image_tar_path, 'wb')
eyefi_logger.debug("Opened file " + image_tar_path + " for binary writing")
file_handle.write(form['FILENAME'][0])
eyefi_logger.debug("Wrote file " + image_tar_path)
file_handle.close()
eyefi_logger.debug("Closed file " + image_tar_path)
eyefi_logger.debug("Extracting TAR file " + image_tar_path)
try:
image_tarfile = tarfile.open(image_tar_path)
except tarfile.ReadError:
eyefi_logger.error("Failed to open %s" % image_tar_path)
raise
for member in image_tarfile.getmembers():
# If timezone is a daylight savings timezone, and we are
# currently in daylight savings time, then use the alt zone
if time.daylight != 0 and time.localtime().tm_isdst != 0:
time_offset = time.altzone
else:
time_offset = time.timezone
timezone = time_offset / 60 / 60 * -1
image_date = datetime.fromtimestamp(member.mtime) - timedelta(hours=timezone)
upload_dir = image_date.strftime(self.server.config['upload_dir'])
eyefi_logger.debug("Creating folder " + upload_dir)
if not os.path.isdir(upload_dir):
os.makedirs(upload_dir)
image_tarfile.extract(member, upload_dir)
image_path = os.path.join(upload_dir, member.name)
eyefi_logger.debug("image_path " + image_path)
os.utime(image_path, (member.mtime + time_offset, member.mtime + time_offset))
if geotag_enable > 0 and member.name.lower().endswith(".log"):
eyefi_logger.debug("Processing LOG file " + image_path)
try:
image_name = member.name[:-4]
shot_time, aps = list(self.parse_log(image_path, image_name))
aps = self.get_photo_aps(shot_time, aps)
loc = self.get_location(aps)
if loc['status'] == 'OK' and float(loc['accuracy']) <= geotag_accuracy:
xmp_name = image_name + ".xmp"
xmp_path = os.path.join(upload_dir, xmp_name)
eyefi_logger.debug("Writing XMP file " + xmp_path)
self.write_xmp(xmp_path, float(loc['location']['lat']), float(loc['location']['lng']))
except:
eyefi_logger.error("Error processing LOG file " + image_path)
eyefi_logger.debug("Closing TAR file " + image_tar_path)
image_tarfile.close()
eyefi_logger.debug("Deleting TAR file " + image_tar_path)
os.remove(image_tar_path)
# Create the XML document to send back
doc = xml.dom.minidom.Document()
soap_element = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "SOAP-ENV:Envelope")
soap_element.setAttribute("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
soap_body_element = doc.createElement("SOAP-ENV:Body")
upload_photo_response_element = doc.createElement("UploadPhotoResponse")
success_element = doc.createElement("success")
success_element_text = doc.createTextNode("true")
success_element.appendChild(success_element_text)
upload_photo_response_element.appendChild(success_element)
soap_body_element.appendChild(upload_photo_response_element)
soap_element.appendChild(soap_body_element)
doc.appendChild(soap_element)
return doc.toxml(encoding="UTF-8")
@staticmethod
def parse_log(logfile, filename):
shot_time = 0
aps = {}
for line in open(logfile):
timer, timestamp, act = line.strip().split(",", 2)
act = act.split(",")
act, args = act[0], act[1:]
if act in ("AP", "NEWAP"):
aps.setdefault(args[0], []).append({"time": int(timer), "pwr": int(args[1])})
elif act == "NEWPHOTO":
if filename == args[0]:
shot_time = int(timer)
elif act == "POWERON":
if shot_time > 0:
return shot_time, aps
shot_time = 0
aps = {}
if shot_time > 0:
return shot_time, aps
def get_photo_aps(self, timer, aps):
geotag_lag = int(self.server.config['geotag_lag'])
new_aps = []
for mac in aps:
lag = min([(abs(ap["time"] - timer), ap["pwr"]) for ap in aps[mac]], key=lambda a: a[0])
if lag[0] <= geotag_lag:
new_aps.append({"mac": mac, "pwr": lag[1]})
return new_aps
@staticmethod
def get_location(aps):
try:
# TODO: fix google map API (missing auth key?)
geo_url = 'maps.googleapis.com'
headers = {"Host": geo_url}
params = "?browser=none&sensor=false"
for ap in aps:
params += '&wifi=mac:' + '-'.join([ap['mac'][2 * d:2 * d + 2] for d in range(6)]) + '|ss:' + str(
int(math.log10(ap['pwr'] / 100.0) * 10 - 50))
conn = httplib.HTTPSConnection(geo_url)
conn.request("GET", "/maps/api/browserlocation/json" + params, "", headers)
resp = conn.getresponse()
result = resp.read()
conn.close()
except:
eyefi_logger.debug("Error connecting to geolocation service")
return None
try:
try:
import simplejson as json
except ImportError:
import json
return json.loads(result)
except:
try:
import re
result = result.replace("\n", " ")
loc = dict()
loc['location'] = {}
loc['location']['lat'] = float(re.sub(r'.*"lat"\s*:\s*([\d.]+)\s*[,}\n]+.*', r'\1', result))
loc['location']['lng'] = float(re.sub(r'.*"lng"\s*:\s*([\d.]+)\s*[,}\n]+.*', r'\1', result))
loc['accuracy'] = float(re.sub(r'.*"accuracy"\s*:\s*([\d.]+)\s*[,\}\n]+.*', r'\1', result))
loc['status'] = re.sub(r'.*"status"\s*:\s*"(.*?)"\s*[,}\n]+.*', r'\1', result)
return loc
except:
eyefi_logger.debug("Geolocation service response contains no coordinates: " + result)
return None
@staticmethod
def write_xmp(name, latitude, longitude):
if latitude > 0:
ref = "N"
else:
ref = "S"
latitude = str(abs(latitude)).split('.')
latitude[1] = str(float('0.' + latitude[1]) * 60)
latitude = ','.join(latitude) + ref
if longitude > 0:
ref = "E"
else:
ref = "W"
longitude = str(abs(longitude)).split('.')
longitude[1] = str(float('0.' + longitude[1]) * 60)
longitude = ','.join(longitude) + ref
xmp_file = open(name, "w")
xmp_file.write(
"<?xpacket begin='\xef\xbb\xbf' id='W5M0MpCehiHzreSzNTczkc9d'?>\n<x:xmpmeta xmlns:x='adobe:ns:meta/' "
"x:xmptk='EyeFiServer'>\n<rdf:RDF "
"xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n<rdf:Description rdf:about='' "
"xmlns:exif='http://ns.adobe.com/exif/1.0/'>\n<exif:GPSLatitude>" + latitude +
"</exif:GPSLatitude>\n<exif:GPSLongitude>" + longitude +
"</exif:GPSLongitude>\n<exif:GPSVersionID>2.2.0.0</exif:GPSVersionID>\n</rdf:Description>\n</rdf:RDF>\n"
"</x:xmpmeta>\n<?xpacket end='w'?>\n")
xmp_file.close()
@staticmethod
def get_photo_status(post_data):
handler = EyeFiContentHandler()
xml.sax.parseString(post_data, handler)
# Create the XML document to send back
doc = xml.dom.minidom.Document()
soap_element = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "SOAP-ENV:Envelope")
soap_element.setAttribute("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
soap_body_element = doc.createElement("SOAP-ENV:Body")
get_photo_status_response_element = doc.createElement("GetPhotoStatusResponse")
get_photo_status_response_element.setAttribute("xmlns", "http://localhost/api/soap/eyefilm")
fileid_element = doc.createElement("fileid")
fileid_element_text = doc.createTextNode("1")
fileid_element.appendChild(fileid_element_text)
offset_element = doc.createElement("offset")
offset_element_text = doc.createTextNode("0")
offset_element.appendChild(offset_element_text)
get_photo_status_response_element.appendChild(fileid_element)
get_photo_status_response_element.appendChild(offset_element)
soap_body_element.appendChild(get_photo_status_response_element)
soap_element.appendChild(soap_body_element)
doc.appendChild(soap_element)
return doc.toxml(encoding="UTF-8")
def _get_mac_upload_key_dict(self):
d = {self.server.config['mac']: self.server.config['upload_key']}
return d
def start_session(self, post_data):
eyefi_logger.debug("Delegating the XML parsing of start_session post_data to EyeFiContentHandler()")
handler = EyeFiContentHandler()
parser = xml.sax.parseString(post_data, handler)
eyefi_logger.debug("Extracted elements: " + str(handler.extracted_elements))
# Retrieve it from C:\Documents and Settings\<User>\Application Data\Eye-Fi\Settings.xml
mac_to_upload_key_map = self._get_mac_upload_key_dict()
mac = handler.extracted_elements["macaddress"]
upload_key = mac_to_upload_key_map[mac]
eyefi_logger.debug("Got MAC address of " + mac)
eyefi_logger.debug("Setting Eye-Fi upload key to " + upload_key)
credential_string = mac + handler.extracted_elements["cnonce"] + upload_key
eyefi_logger.debug("Concatenated credential string (pre MD5): " + credential_string)
# Return the binary data represented by the hexadecimal string
# resulting in something that looks like "\x00\x18V\x03\x04..."
binary_credential_string = binascii.unhexlify(credential_string)
# Now MD5 hash the binary string
md5_hash = hashlib.md5()
md5_hash.update(binary_credential_string)
# Hex encode the hash to obtain the final credential string
credential = md5_hash.hexdigest()
# Create the XML document to send back
doc = xml.dom.minidom.Document()
soap_element = doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/", "SOAP-ENV:Envelope")
soap_element.setAttribute("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
soap_body_element = doc.createElement("SOAP-ENV:Body")
start_session_response_element = doc.createElement("StartSessionResponse")
start_session_response_element.setAttribute("xmlns", "http://localhost/api/soap/eyefilm")
credential_element = doc.createElement("credential")
credential_element_text = doc.createTextNode(credential)
credential_element.appendChild(credential_element_text)
snonce_element = doc.createElement("snonce")
snonce_element_text = doc.createTextNode("%x" % random.getrandbits(128))
snonce_element.appendChild(snonce_element_text)
transfer_mode_element = doc.createElement("transfermode")
transfer_mode_element_text = doc.createTextNode(handler.extracted_elements["transfermode"])
transfer_mode_element.appendChild(transfer_mode_element_text)
transfer_mode_timestamp_element = doc.createElement("transfermodetimestamp")
transfer_mode_timestamp_element_text = doc.createTextNode(handler.extracted_elements["transfermodetimestamp"])
transfer_mode_timestamp_element.appendChild(transfer_mode_timestamp_element_text)
upsync_allowed_element = doc.createElement("upsyncallowed")
upsync_allowed_element_text = doc.createTextNode("true")
upsync_allowed_element.appendChild(upsync_allowed_element_text)
start_session_response_element.appendChild(credential_element)
start_session_response_element.appendChild(snonce_element)
start_session_response_element.appendChild(transfer_mode_element)
start_session_response_element.appendChild(transfer_mode_timestamp_element)
start_session_response_element.appendChild(upsync_allowed_element)
soap_body_element.appendChild(start_session_response_element)
soap_element.appendChild(soap_body_element)
doc.appendChild(soap_element)
return doc.toxml(encoding="UTF-8")
def run_eyefi():
config = dict()
config['mac'] = os.environ.get('CARD_MAC', '00:00:00:00:00:00').replace(':', '').replace('-', '')
config['upload_key'] = os.environ.get('CARD_UPLOAD_KEY', '00000000000000000000000000000000')
# TODO: Allow multiple mac/upload keys
config['geotag_enable'] = os.environ.get('GEOTAG_ENABLE', '0')
config['geotag_lag'] = os.environ.get('GEOTAG_LAG', '3600')
config['geotag_accuracy'] = os.environ.get('GEOTAG_ACCURACY', '140000')
config['upload_dir'] = os.environ.get('UPLOAD_DIR', 'uploads/')
server_address = ('', 59278)
# Create an instance of an HTTP server. Requests will be handled
# by the class EyeFiRequestHandler
eyefi_server = EyeFiServer(server_address, EyeFiRequestHandler)
eyefi_server.config = config
eyefi_logger.info("Eye-Fi server started listening on port " + str(server_address[1]))
eyefi_server.serve_forever()
def main():
run_eyefi()
if __name__ == "__main__":
main()