-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
78 lines (63 loc) · 2.61 KB
/
Copy pathserver.py
File metadata and controls
78 lines (63 loc) · 2.61 KB
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
from functools import partial
from http.server import BaseHTTPRequestHandler, HTTPServer
import hmac
import json
import ssl
from urllib.parse import urlparse,parse_qs
from threading import Thread
import config.main as config
class RequestHandler(BaseHTTPRequestHandler):
openingLock = None
password = None
api_token = None
last_scan = None
def __init__(self, password, openingLock, api_token, last_scan, *args, **kwargs):
self.openingLock = openingLock
self.password = password
self.api_token = api_token
self.last_scan = last_scan
super().__init__(*args, **kwargs)
def sendResponse(self, code, message = None):
self.send_response(code)
self.send_header('Content-type','text/html')
self.end_headers()
if message is None:
if code == 200:
self.wfile.write(b"OK! Lock is opening.")
if code == 403:
self.wfile.write(b"Acces denied")
else:
self.wfile.write(bytes(message))
def sendJson(self, code, payload):
body = json.dumps(payload).encode()
self.send_response(code)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(body)
def do_GET(self):
urlParsed = urlparse(self.path)
paramParsed = parse_qs(urlParsed.query)
if urlParsed.path == '/last-uid':
token = paramParsed.get('token', [''])[0]
if hmac.compare_digest(token, self.api_token):
self.sendJson(200, {'uid': self.last_scan.get('uid')})
else:
self.sendResponse(code=403)
return
#Testing parameters "pass" and "password"
if ('pass' in paramParsed and paramParsed['pass'][0] == self.password) or ('password' in paramParsed and paramParsed['password'][0] == self.password):
self.sendResponse(code=200)
self.openingLock()
else:
self.sendResponse(code=403)
class Server:
def __init__(self):
self.last_scan = {'uid': None}
def set_last_uid(self, uid):
self.last_scan['uid'] = uid
def start(self, password, openingLock, api_token, addr = '', port = 8000):
httpd = HTTPServer((addr, port), partial(RequestHandler, password, openingLock, api_token, self.last_scan))
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(certfile=config.server['cert_file'], keyfile=config.server['key_file'])
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
Thread(target=httpd.serve_forever).start()