-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
181 lines (132 loc) · 6.22 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
# Handles JSON messages and starts the authentication server
import os
from pathlib import Path
from http.cookies import SimpleCookie
import asyncio
import logging
import ssl
import websockets, websockets.http
from http import HTTPStatus
from aiohttp import web
import aiohttp_cors
import json
import accounts
from scheduler import Scheduler
from authentication_handler import AuthenticationHandler
from room_handler import RoomHandler
from chat_handler import ChatHandler
from tasks_handler import TasksHandler
from settings_handler import SettingsHandler
from public_handler import PublicHandler
from timer_handler import TimerHandler
from stats_handler import StatsHandler
websocket_peers = set()
authenticated_peers = set()
scheduler = Scheduler()
auth_websocket = None
auth_api_key = os.environ.get("COWORK_AUTH_API_KEY")
queried_tokens = {}
authentication_handler = AuthenticationHandler(scheduler, auth_websocket)
room_handler = RoomHandler(authentication_handler, scheduler)
chat_handler = ChatHandler(authentication_handler, room_handler)
tasks_handler = TasksHandler(authentication_handler, room_handler)
settings_handler = SettingsHandler(authentication_handler, room_handler)
public_handler = PublicHandler(authentication_handler)
timer_handler = TimerHandler(authentication_handler, room_handler, scheduler)
stats_handler = StatsHandler(authentication_handler)
channels = {
"auth": authentication_handler, "room": room_handler, "settings": settings_handler, "chat": chat_handler, "tasks": tasks_handler, \
"public": public_handler, "timer": timer_handler, "stats": stats_handler
}
def add_websocket(websocket):
global websocket_peers
print("New peer connected to the main server.")
websocket_peers.add(websocket)
async def remove_websocket(websocket):
global websocket_peers
print("A peer disconnected from the main server.")
await channels["room"].remove_peer(websocket)
websocket_peers.remove(websocket)
async def consumer(websocket, message):
global channels
if "channel" in message:
if message["channel"] in channels:
if "type" in message:
if message["channel"] == "auth":
await channels[message["channel"]].consumer(websocket, message)
elif message["channel"] == "public":
await channels[message["channel"]].consumer(websocket, message)
elif websocket in authenticated_peers:
await channels[message["channel"]].consumer(websocket, message)
else:
response = {"type": "sign_in", "username_exists": False, "password_correct": False}
response_json = json.dumps(response)
await websocket.send(response_json)
else:
print("There's no valid channel indicated; I cannot route the message.")
return
async def connect_to_auth():
global auth_websocket, auth_api_key
async with websockets.connect("wss://ws.joincowork.com:4433/") as websocket:
auth_websocket = websocket
message = {"channel": "auth", "type": "register_server", "api_key": auth_api_key}
message_json = json.dumps(message, default=str)
await auth_websocket.send(message_json)
while True:
response_s = await auth_websocket.recv()
print("< {}".format(response_s))
response = json.loads(response_s)
await auth_consumer(response)
async def auth_consumer(message):
global auth_websocket
print(message)
if message["type"] == "get_session":
if message["success"]:
websocket = queried_tokens[message["token"]]
if websocket in websocket_peers:
queried_tokens.pop(message["token"])
authenticated_peers.add(websocket)
username = message["username"]
channels["auth"].make_session(websocket, username)
account_id = channels["auth"].auth_database_connector.get_id_for_username(username)
display_name = channels["auth"].auth_database_connector.get_display_name_for_id(account_id)
pro = channels["auth"].auth_database_connector.get_pro(account_id)
update_notes = channels["auth"].auth_database_connector.get_update_notes(account_id)
channels["auth"].auth_database_connector.update_last_login(account_id)
response = {"type": "sign_in", "username": username, "account_id": account_id, "display_name": display_name, "username_exists": True, "password_correct": True, "update_notes": update_notes, "pro": pro}
response_json = json.dumps(response)
await websocket.send(response_json)
elif message["type"] == "register_server":
if message["success"]:
channels["auth"].set_auth_websocket(auth_websocket)
async def request_session_is_valid(websocket, token):
global auth_websocket
message = {"channel": "auth", "type": "get_session", "token": token}
message_json = json.dumps(message, default=str)
await auth_websocket.send(message_json)
queried_tokens[token] = websocket
async def consumer_handler(websocket, path):
global channels
add_websocket(websocket)
if not websocket in authenticated_peers:
if "cookie" in websocket.request_headers:
cookies = websocket.request_headers["Cookie"]
token = cookies[6:]
await request_session_is_valid(websocket, token)
try:
async for message_json in websocket:
message = json.loads(message_json)
await consumer(websocket, message)
except websockets.exceptions.ConnectionClosedError as err:
print(err)
finally:
await remove_websocket(websocket)
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
here = os.path.dirname(os.path.abspath(__file__))
cert_pem = os.path.join(here, "fullchain.pem")
key_pem = os.path.join(here, "privkey.pem")
ssl_context.load_cert_chain(cert_pem, keyfile=key_pem)
start_server = websockets.serve(consumer_handler, "ws.joincowork.com", 4434, ssl=ssl_context)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_until_complete(connect_to_auth())
asyncio.get_event_loop().run_forever()