summary refs log tree commit diff
path: root/main.py
diff options
context:
space:
mode:
Diffstat (limited to 'main.py')
-rw-r--r--main.py51
1 files changed, 45 insertions, 6 deletions
diff --git a/main.py b/main.py
index 07eac1a..39413c2 100644
--- a/main.py
+++ b/main.py
@@ -1,20 +1,59 @@
 import asyncio

+import json

 import logging

 from websockets.asyncio.server import serve

+from websockets.asyncio.server import broadcast

 

 addr = "localhost"

 port = 3636

 

 logging.basicConfig(level=logging.INFO)

 

+ulist = {}

+ulist_connection_ids = {}

+clients = []

+

+class util:

+    def error(code, listener):

+        return json.dumps({

+            "error": True,

+            "code": code,

+            "form": "helium-util",

+            "listener": listener

+        })

+    

+    def field_check(expects, gets):

+        for i in expects:

+            if i not in gets:

+                return "malformedJson"

+            if type(gets[i]) in [str, dict, list]:

+                if len(gets[i]) not in expects[i]:

+                    return "lengthInvalid"

+        return True

+

 async def handler(websocket):

-    await websocket.send("Hello")

-    miw = 0

-    async for message in websocket:

-        miw += 1

+    clients.append(websocket)

     async for message in websocket:

-        print(str(websocket))

-        await websocket.send(message)

+        try:

+            r = json.loads(message)

+        except:

+            await websocket.send(util.error("malformedJson", None))

+            continue

+        if "listener" not in r:

+            r["listener"] = None

+        listener = r["listener"]

+        if "command" not in r:

+            await websocket.send(util.error("malformedJson", listener))

+            continue

+        if r["command"] == "register":

+            fc = util.field_check({"username": range(1,21), "password": range(8,256), "invite_code": range(16,17)}, r)

+            if fc != True:

+                await websocket.send(util.error(fc, listener))

+                continue

+        else:

+            await websocket.send(util.error("malformedJson", listener))

+    if websocket in clients:

+        clients.append(websocket)

 

 async def main():

     async with serve(handler, addr, port) as server: