-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
234 lines (205 loc) · 8.43 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
import hashlib
import os
import json
import html
from datetime import datetime, timedelta
from flask import Blueprint, render_template, send_file, make_response, request, redirect, jsonify, current_app, \
url_for, Flask
from pymongo import MongoClient
from flask_socketio import SocketIO, emit
from logging.config import dictConfig
import dos as DOS
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
from LineUp.user import user_bp
from LineUp.register import register_bp
from LineUp.login import login_bp
from LineUp.ta import ta_bp
from LineUp.image import image_bp
from LineUp.ta_page import ta_page_bp
app = Flask(__name__)
client = MongoClient("mongo")
db = client["cse312-project"]
TAOnDuty_collection = db['on_duty']
student_queue = db['student_queue']
TA_collection = db['TA_collection']
TA_chat_collection = db['TA_chat_collection']
ip_collection = db['ip_collection']
socketio = SocketIO(app, cors_allowed_origins="*", transports=['websocket'], async_mode='threading')
app.register_blueprint(user_bp)
app.register_blueprint(register_bp)
app.register_blueprint(login_bp)
app.register_blueprint(ta_bp)
app.register_blueprint(image_bp)
app.register_blueprint(ta_page_bp)
@app.before_request
def dos():
return DOS.DOS_prevention()
cooldownDict = {}
@socketio.on('connect')
def connect():
client_id = request.sid
cooldownDict[client_id] = datetime.now()
emit('timer', broadcast=True)
@socketio.on('TAOnDuty')
def on_duty():
curr_auth = request.cookies.get("auth_token")
if curr_auth is not None:
hash_obj = hashlib.sha256()
hash_obj.update(curr_auth.encode())
hash_obj.digest()
hash_token = hash_obj.hexdigest()
if TA_collection.find_one({"auth_token": hash_token}) is not None:
TA_info = TA_collection.find({"auth_token": hash_token})[0]
if TA_info is not None:
if TAOnDuty_collection.find_one({"TA": TA_info.get("username")}) is None:
TAOnDuty_collection.insert_one({"TA": TA_info.get("username")})
TAOnDutyList = []
for TA in TAOnDuty_collection.find({}):
TAOnDutyList.append(TA.get("TA"))
TAOnDutyList = json.dumps(TAOnDutyList)
emit('TAOnDutyReceive', TAOnDutyList, broadcast=True)
@socketio.on('populateOnDuty')
def populate_queue():
TAOnDutyList = []
for TA in TAOnDuty_collection.find({}):
TA.pop("_id")
TAOnDutyList.append(TA.get("TA"))
TAOnDutyList = json.dumps(TAOnDutyList)
emit('TAOnDutyReceive', TAOnDutyList, broadcast=True)
@socketio.on('TAOffDuty')
def off_duty():
curr_auth = request.cookies.get("auth_token")
if curr_auth is not None:
hash_obj = hashlib.sha256()
hash_obj.update(curr_auth.encode())
hash_obj.digest()
hash_token = hash_obj.hexdigest()
if TA_collection.find_one({"auth_token": hash_token}) is not None:
TA_info = TA_collection.find({"auth_token": hash_token})[0]
if TA_info is not None:
TaInCollection = TAOnDuty_collection.find_one({"TA": TA_info.get("username")})
if TaInCollection is not None:
TAOnDuty_collection.delete_one({"TA": TA_info.get("username")})
TAOnDutyList = []
for TA in TAOnDuty_collection.find({}):
TAOnDutyList.append(TA.get("TA"))
TAOnDutyList = json.dumps(TAOnDutyList)
emit('TAOnDutyReceive', TAOnDutyList, broadcast=True)
@socketio.on('StudentQueue')
def student_enqueue(studentName):
ID = request.sid
queueCooldown = timedelta(seconds=3)
for ID in cooldownDict.keys():
timeLeft = cooldownDict[ID]
if (datetime.now() - timeLeft) < queueCooldown:
return
studentName = html.escape(studentName + " " + str(datetime.now() - timedelta(days=1) + timedelta(hours=20)))
if student_queue.find_one({"student": studentName}) is None:
student_queue.insert_one({"student": studentName, "dequeued": False})
student = [studentName]
student = json.dumps(student)
emit('studentQueue2', student, broadcast=True)
for ID in cooldownDict.keys():
cooldownDict[ID] = datetime.now()
@socketio.on('StudentDequeue')
def student_dequeue(id):
curr_auth = request.cookies.get("auth_token")
if curr_auth is not None:
hash_obj = hashlib.sha256()
hash_obj.update(curr_auth.encode())
hash_obj.digest()
hash_token = hash_obj.hexdigest()
if TA_collection.find_one({"auth_token": hash_token}) is not None:
TA_info = TA_collection.find({"auth_token": hash_token})[0]
if TA_info is not None:
allStudents = student_queue.find({})
idFinder = 0
for student in allStudents:
if idFinder == int(id):
student_queue.delete_one({"student": student.get("student")})
idFinder += 1
Queue = []
allStudentsInQueue = student_queue.find({})
for students in allStudentsInQueue:
students.pop("_id")
Queue.append(students.get("student"))
QueueJSON = json.dumps(Queue)
emit('studentQueue', QueueJSON, broadcast=True)
@socketio.on('TADequeue')
def TA_dequeue(id):
curr_auth = request.cookies.get("auth_token")
if curr_auth is not None:
hash_obj = hashlib.sha256()
hash_obj.update(curr_auth.encode())
hash_obj.digest()
hash_token = hash_obj.hexdigest()
if TA_collection.find_one({"auth_token": hash_token}) is not None:
TA_info = TA_collection.find({"auth_token": hash_token})[0]
TAWhoClickedButton = TA_info.get("username")
if TA_info is not None:
TAChats = TA_chat_collection.find({})
TAChat = []
idFinder = 0
for TAMessage in TAChats:
TAMessage.pop("_id")
GivenUsername = id.split("?")[0]
Givenid = id.split("?")[1]
if idFinder == int(Givenid) and TAWhoClickedButton == GivenUsername:
TA_chat_collection.delete_one({"chat": TAMessage.get("chat")})
else:
TAChat.append(TAMessage.get("chat"))
idFinder += 1
TAChatJSON = json.dumps(TAChat)
emit('TAChat', TAChatJSON, broadcast=True)
cooldown_duration = timedelta(seconds=10)
@socketio.on('ReceiveTAChat')
def receive_TA_annoucement(chat):
curr_auth = request.cookies.get("auth_token")
if curr_auth is not None:
hash_obj = hashlib.sha256()
hash_obj.update(curr_auth.encode())
hash_obj.digest()
hash_token = hash_obj.hexdigest()
if TA_collection.find_one({"auth_token": hash_token}) is not None:
TA_info = TA_collection.find({"auth_token": hash_token})[0]
if TA_info is not None:
TA_chat_collection.insert_one({"chat": str(TA_info["username"]) + ": " + str(html.escape(chat)),
"removed": str(False)})
TA_chat = [str(TA_info["username"]) + ":" + str(html.escape(chat))]
TA_chat = json.dumps(TA_chat)
emit('TAChatReceive', TA_chat, broadcast=True)
@socketio.on('populateQueue')
def populate_queue():
Queue = []
allStudentsInQueue = student_queue.find({})
for students in allStudentsInQueue:
students.pop("_id")
Queue.append(students.get("student"))
QueueJSON = json.dumps(Queue)
emit('studentQueue', QueueJSON, broadcast=True)
@socketio.on('ClientTAChat')
def populate_TA_chat():
chatList = []
allChats = TA_chat_collection.find({})
for chat in allChats:
chat.pop("_id")
chatList.append(chat.get("chat"))
chatJSON = json.dumps(chatList)
emit('TAChat', chatJSON, broadcast=True)
if __name__ == '__main__':
# app.run(host='0.0.0.0', port=8080, debug=True)
socketio.run(app, host='0.0.0.0', port=8080, debug=True)