From c10c3ec280cecb0058bcfd22b7a6980fee0ff696 Mon Sep 17 00:00:00 2001 From: xomf Date: Sun, 8 Sep 2024 23:54:52 +0000 Subject: [PATCH] Add server.py --- server.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 server.py diff --git a/server.py b/server.py new file mode 100644 index 0000000..64dd4da --- /dev/null +++ b/server.py @@ -0,0 +1,47 @@ +import asyncio +import json +from websockets import serve + +clients = { + "FD3ZJFHQ7r" : { + "doorOpen" : False, + "lockdown" : False, + "lastOpened" : 1, #unix timestamp + "secLevel" : 1, + "openedBy" : "user" + } +} + +async def echo(websocket): + print("Connection established") + authenticated = False + await websocket.send(json.dumps({ + "type" : "SAuthReq" + })) + + async for msg in websocket: + msg_json = json.loads(msg) + if authenticated == False: + if msg_json["type"] == "CAuth": + if msg_json["token"] in clients: + await websocket.send(json.dumps({ + "type" : "SAuthStatus", + "failed" : False, + "reason" : "Authentication Successful" + })) + authenticated = True + else: + await websocket.send(json.dumps({ + "type" : "SAuthStatus", + "failed" : True, + "reason" : "Invalid token" + })) + + + + +async def main(): + async with serve(echo, "localhost", 8765): + await asyncio.get_running_loop().create_future() + +asyncio.run(main()) \ No newline at end of file