-
Notifications
You must be signed in to change notification settings - Fork 47
/
server.py
551 lines (485 loc) · 20 KB
/
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
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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import broadlink, configparser
import sys, getopt
import time, binascii
import netaddr
import settings
import signal
import socket
import errno
import json
import shutil
from os import path
from Crypto.Cipher import AES
class Server(HTTPServer):
def get_request(self):
result = None
while result is None:
try:
result = self.socket.accept()
result[0].setblocking(0)
result[0].settimeout(self.timeout)
except socket.timeout:
pass
return result
def server_bind(self):
HTTPServer.server_bind(self)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
class Handler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin','*')
self.end_headers()
def handle(self):
self.close_connection = 0
while not self.close_connection:
try:
self.handle_one_request()
except IOError as e:
if e.errno == errno.EWOULDBLOCK:
self.close_connection=1
def do_GET(self):
try:
if GlobalPassword:
try:
if RestrictAccess and self.client_address[0] not in RestrictAccess:
return self.access_denied()
return self.messageHandler()
except NameError as e:
print ("Error: %s" % e)
self.password_required()
except NameError: #- No security specified
self.messageHandler()
def do_POST(self):
password = ''
try:
content_len = int(self.headers.getheader('content-length', 0))
password = json.loads(self.rfile.read(content_len))['password'];
except:
pass
try:
if GlobalPassword and GlobalPassword == password:
return self.messageHandler()
else:
print ("TRY %s != %s" % (GlobalPassword, password))
except NameError:
return self.password_required()
print ("LSE %s != %s" % (GlobalPassword, parameters['password']))
self.password_required()
def password_required(self):
response = "Password required from %s" % self.client_address[0]
self.wfile.write('''{ "error": "%s" }''' % response)
print (response)
self.close_connection = 1
return False
def access_denied(self):
response = "Client %s is not allowed!" % self.client_address[0]
self.wfile.write('''{ "error": "%s" }''' % response)
print (response)
self.close_connection = 1
return False
def messageHandler(self):
if 'favicon' in self.path:
return False
self._set_headers()
paths = self.path.split('/')
if 'listCommand' in self.path:
result = listCommand()
if result == False:
response = "Failed: Found no command"
else:
response = json.dumps(result)
elif 'learnCommand' in self.path:
try:
if self.client_address[0] not in LearnFrom:
print ("Won't learn commands from %s. Access Denied!" % self.client_address[0])
return False
except NameError:
pass
if paths[2] == 'learnCommand':
deviceName = paths[1]
commandName = paths[3]
else:
commandName = paths[2]
deviceName = None
result = learnCommand(commandName,deviceName)
if result == False:
response = "Failed: No command learned"
else:
response = "Learned: %s" % commandName
elif 'sendCommand' in self.path:
if paths[2] == 'sendCommand':
deviceName = paths[1]
commandName = paths[3]
else:
commandName = paths[2]
deviceName = None
if 'on' in commandName or 'off' in commandName:
status = commandName.rsplit('o', 1)[1]
realcommandName = commandName.rsplit('o', 1)[0]
print(status, realcommandName)
if 'n' in status:
setStatus(realcommandName, '1', deviceName)
elif 'ff' in status:
setStatus(realcommandName, '0', deviceName)
result = sendCommand(commandName, deviceName)
if result == False:
response = "Failed: Unknown command"
else:
response = "Sent: %s" % commandName
elif 'getStatus' in self.path:
if paths[2] == 'getStatus':
commandName = paths[3]
deviceName = paths[1]
else:
commandName = paths[2]
deviceName = None
if 'temp' in commandName: # Should likely use getSensor instead
result = getSensor("temperature",deviceName)
if result == False:
response = "Failed: Cannot get temperature"
else:
response = '''{ "temperature": %s } ''' % result
else:
status = getStatus(commandName,deviceName)
if (status):
response = status
else:
response = "Failed: Unknown command"
elif 'setStatus' in self.path:
if paths[2] == 'setStatus':
commandName = paths[3]
deviceName = paths[1]
status = paths[4]
else:
commandName = paths[2]
deviceName = None
status = paths[3]
result = setStatus(commandName, status, deviceName)
if (result):
response = '''{ "%s": "%s" }''' % (commandName, status)
else:
response = "Failed: Unknown command"
elif 'getSensor' in self.path or 'a1' in self.path:
if paths[2] == 'getSensor':
sensor = paths[3]
deviceName = paths[1]
else:
sensor = paths[2]
deviceName = None
#- Old syntax - find a compatible device
if "A1" == paths[2].upper()[:2]:
for dev in devices:
if "A1" == dev.type.upper():
deviceName = dev.hostname
break
result = getSensor(sensor, deviceName)
if result == False:
response = "Failed to get data"
else:
if sensor == 'temperature' or sensor == 'humidity':
response = '''{ "%s": %s }''' % (sensor, result)
else:
response = '''{ "%s": "%s" }''' % (sensor, result)
else:
response = "Failed"
if "Failed" in response:
self.wfile.write('''{ "error": "%s" }''' % response)
elif "Sent" in response:
self.wfile.write('''{ "ok": "%s" }''' % response)
else:
self.wfile.write (response);
print ("\t"+response)
def listCommand():
serviceName = 'Commands'
if settingsFile.has_section(serviceName):
commandFromSettings = list(settingsFile.items(serviceName))
else:
return False
# ordering commands
commandFromSettings.sort()
# get keys
commands = []
for (command, value) in commandFromSettings:
commands.append(command)
return commands
def sendCommand(commandName,deviceName):
if deviceName == None:
device = devices[0]
serviceName = 'Commands'
else:
device = DeviceByName[deviceName];
serviceName = deviceName + ' Commands'
deviceKey = device.key
deviceIV = device.iv
if settingsFile.has_option(serviceName, commandName):
commandFromSettings = settingsFile.get(serviceName, commandName)
elif settingsFile.has_option('Commands', commandName):
commandFromSettings = settingsFile.get('Commands', commandName)
else:
return False
if commandFromSettings.strip() != '':
if commandFromSettings.startswith("MACRO "):
for command in commandFromSettings.strip().split():
if command == "sleep":
time.sleep(1)
continue
if "," in command:
try:
(actualCommand, repeatAmount) = command.split(',')
for x in range(0,int(repeatAmount)):
if actualCommand == "sleep":
time.sleep(1)
else:
sendCommand(actualCommand,deviceName)
except:
print ("Skipping malformed command: %s" % command)
continue
if command.startswith("sleep"):
try:
time.sleep(int(command[5:]))
except:
print ("Invalid sleep time: %s" % command)
time.sleep(2)
else:
sendCommand(command,deviceName)
return True
decodedCommand = binascii.unhexlify(commandFromSettings)
AESEncryption = AES.new(str(deviceKey), AES.MODE_CBC, str(deviceIV))
encodedCommand = AESEncryption.encrypt(str(decodedCommand))
finalCommand = encodedCommand[0x04:]
try:
device.send_data(finalCommand)
except Exception:
print ("Probably timed out..")
return True
def learnCommand(commandName, deviceName=None):
if deviceName == None:
device = devices[0]
sectionName = 'Commands'
else:
device = DeviceByName[deviceName];
sectionName = deviceName + ' Commands'
if OverwriteProtected and settingsFile.has_option(sectionName,commandName):
print ("Command %s alreadyExists and changes are protected!" % commandName)
return False
print ("Waiting %d seconds to capture command" % GlobalTimeout)
deviceKey = device.key
deviceIV = device.iv
device.enter_learning()
time.sleep(GlobalTimeout)
LearnedCommand = device.check_data()
if LearnedCommand is None:
print('Command not received')
return False
AdditionalData = bytearray([0x00, 0x00, 0x00, 0x00])
finalCommand = AdditionalData + LearnedCommand
AESEncryption = AES.new(str(deviceKey), AES.MODE_CBC, str(deviceIV))
decodedCommand = binascii.hexlify(AESEncryption.decrypt(str(finalCommand)))
backupSettings()
try:
broadlinkControlIniFile = open(path.join(settings.applicationDir, 'settings.ini'), 'w')
if not settingsFile.has_section(sectionName):
settingsFile.add_section(sectionName)
settingsFile.set(sectionName, commandName, decodedCommand)
settingsFile.write(broadlinkControlIniFile)
broadlinkControlIniFile.close()
return True
except StandardError as e:
print("Error writing settings file: %s" % e)
restoreSettings()
return False
def setStatus(commandName, status, deviceName=None):
if deviceName == None:
sectionName = 'Status'
else:
sectionName = deviceName + ' Status'
backupSettings()
try:
if not settingsFile.has_section(sectionName):
settingsFile.add_section(sectionName)
broadlinkControlIniFile = open(path.join(settings.applicationDir, 'settings.ini'), 'w')
settingsFile.set(sectionName, commandName, status)
settingsFile.write(broadlinkControlIniFile)
broadlinkControlIniFile.close()
return True
except StandardError as e:
print ("Error writing settings file: %s" % e)
restoreSettings()
return False
def getStatus(commandName, deviceName=None):
if deviceName == None:
sectionName = 'Status'
else:
sectionName = deviceName + ' Status'
if settingsFile.has_option(sectionName,commandName):
status = settingsFile.get(sectionName, commandName)
return status
else:
return False
def getSensor(sensorName,deviceName=None):
if deviceName == None:
device = devices[0]
else:
device = DeviceByName[deviceName];
if "RM" in device.type.upper() and "temp" in sensorName:
temperature = device.check_temperature()
if temperature:
return temperature
if "A1" in device.type.upper():
result = device.check_sensors()
if result:
return result[sensorName]
return False
def start(server_class=Server, handler_class=Handler, port=8080, listen='0.0.0.0', timeout=1):
server_address = (listen, port)
httpd = server_class(server_address, handler_class)
httpd.timeout = timeout
print ('\nStarting broadlink-rest server on %s:%s ...' % (listen,port))
while not InterruptRequested:
httpd.handle_request()
def backupSettings():
shutil.copy2(settings.settingsINI,settings.settingsINI+".bak")
def restoreSettings():
if os.path.isfile(settings.settingsINI+".bak"):
shutil.copy2(settings.settingsINI+".bak",settings.settingsINI)
else:
print ("Can't find backup to restore! Refusing to make this worse!")
sys.exit()
def readSettingsFile():
global devices
global DeviceByName
global RestrictAccess
global LearnFrom
global OverwriteProtected
global GlobalPassword
global GlobalTimeout
global settingsFile
# A few defaults
serverPort = 8080
Autodetect = False
OverwriteProtected = True
listen_address = '0.0.0.0'
broadcast_address = '255.255.255.255'
settingsFile = configparser.ConfigParser()
settingsFile.optionxform = str
settingsFile.read(settings.settingsINI)
Dev = settings.Dev
GlobalTimeout = settings.GlobalTimeout
DiscoverTimeout = settings.DiscoverTimeout
# Override them
if settingsFile.has_option('General', 'password'):
GlobalPassword = settingsFile.get('General', 'password').strip()
if settingsFile.has_option('General', 'serverPort'):
serverPort = int(settingsFile.get('General', 'serverPort'))
if settingsFile.has_option('General','serverAddress'):
listen_address = settingsFile.get('General', 'serverAddress')
if listen_address.strip() == '':
listen_address = '0.0.0.0'
if settingsFile.has_option('General', 'restrictAccess'):
RestrictAccess = settingsFile.get('General', 'restrictAccess').strip()
if settingsFile.has_option('General', 'learnFrom'):
LearnFrom = settingsFile.get('General', 'learnFrom').strip();
if settingsFile.has_option('General', 'allowOverwrite'):
OverwriteProtected = False
if settingsFile.has_option('General','broadcastAddress'):
broadcast = settingsFile.get('General', 'broadcastAddress')
if broadcast_address.strip() == '':
broadcast_address = '255.255.255.255'
if settingsFile.has_option('General', 'Autodetect'):
try:
DiscoverTimeout = int(settingsFile.get('General', 'Autodetect').strip())
except:
DiscoverTimeout = 5
Autodetect = True
settingsFile.remove_option('General','Autodetect')
# Device list
DeviceByName = {}
if not settings.DevList:
Autodetect = True
if Autodetect == True:
print ("Beginning device auto-detection ... ")
# Try to support multi-homed broadcast better
try:
devices = broadlink.discover(DiscoverTimeout,listen_address,broadcast_address)
except:
devices = broadlink.discover(DiscoverTimeout,listen_address)
backupSettings()
try:
broadlinkControlIniFile = open(path.join(settings.applicationDir, 'settings.ini'), 'w')
for device in devices:
try:
device.hostname = socket.gethostbyaddr(device.host[0])[0]
if "." in device.hostname:
device.hostname = device.hostname.split('.')[0]
except:
device.hostname = "Broadlink" + device.type.upper()
if device.hostname in DeviceByName:
device.hostname = "%s-%s" % (device.hostname, str(device.host).split('.')[3])
DeviceByName[device.hostname] = device
if not settingsFile.has_section(device.hostname):
settingsFile.add_section(device.hostname)
settingsFile.set(device.hostname,'IPAddress',str(device.host[0]))
hexmac = ':'.join( [ "%02x" % ( x ) for x in reversed(device.mac) ] )
settingsFile.set(device.hostname,'MACAddress',hexmac)
settingsFile.set(device.hostname,'Device',hex(device.devtype))
settingsFile.set(device.hostname,'Timeout',str(device.timeout))
settingsFile.set(device.hostname,'Type',device.type.upper())
device.auth()
print ("%s: Found %s on %s (%s) type: %s" % (device.hostname, device.type, device.host, hexmac, hex(device.devtype)))
settingsFile.write(broadlinkControlIniFile)
broadlinkControlIniFile.close()
except StandardError as e:
print ("Error writing settings file: %s" % e)
restoreSettings()
else:
devices = []
if settings.DevList:
for devname in settings.DevList:
if Dev[devname,'Type'] == 'RM' or Dev[devname,'Type'] == 'RM2':
device = broadlink.rm((Dev[devname,'IPAddress'], 80), Dev[devname,'MACAddress'], Dev[devname,'Device'])
if Dev[devname,'Type'] == 'MP1':
device = broadlink.mp1((Dev[devname,'IPAddress'], 80), Dev[devname,'MACAddress'], Dev[devname,'Device'])
if Dev[devname,'Type'] == 'SP1':
device = broadlink.sp1((Dev[devname,'IPAddress'], 80), Dev[devname,'MACAddress'], Dev[devname,'Device'])
if Dev[devname,'Type'] == 'SP2':
device = broadlink.sp2((Dev[devname,'IPAddress'], 80), Dev[devname,'MACAddress'], Dev[devname,'Device'])
if Dev[devname,'Type'] == 'A1':
device = broadlink.a1((Dev[devname,'IPAddress'], 80), Dev[devname,'MACAddress'], Dev[devname,'Device'])
if Dev[devname,'Type'] == 'HYSEN':
device = broadlink.hysen((Dev[devname,'IPAddress'], 80), Dev[devname,'MACAddress'], Dev[devname,'Device'])
if Dev[devname,'Type'] == 'S1C':
device = broadlink.S1C((Dev[devname,'IPAddress'], 80), Dev[devname,'MACAddress'], Dev[devname,'Device'])
if Dev[devname,'Type'] == 'DOOYA':
device = broadlink.dooya((Dev[devname,'IPAddress'], 80), Dev[devname,'MACAddress'], Dev[devname,'Device'])
device.timeout = Dev[devname,'Timeout']
if not devname in DeviceByName:
device.hostname = devname
device.auth()
devices.append(device)
print ("%s: Read %s on %s (%s)" % (devname, device.type, str(device.host[0]), device.mac))
DeviceByName[devname] = device
return { "port": serverPort, "listen": listen_address, "timeout": GlobalTimeout }
def SigUsr1(signum, frame):
print ("\nReloading configuration ...")
global InterruptRequested
InterruptRequested = True
def SigInt(signum, frame):
print ("\nShuting down server ...")
global ShutdownRequested
global InterruptRequested
ShutdownRequested = True
InterruptRequested = True
if __name__ == "__main__":
global ShutdownRequested
global InteruptRequested
ShutdownRequested = False
signal.signal(signal.SIGUSR1,SigUsr1)
signal.signal(signal.SIGINT,SigInt)
while not ShutdownRequested:
serverParams = readSettingsFile()
InterruptRequested = False
start(**serverParams)
if not ShutdownRequested:
reload(settings)