This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
forked from meower-media/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupporter.py
368 lines (315 loc) · 14.2 KB
/
supporter.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from datetime import datetime
from better_profanity import profanity
import time
import traceback
import sys
import string
from threading import Thread
from copy import copy
"""
Meower Supporter Module
This module provides logging, error traceback, and other miscellaneous supportive functionality.
This keeps the main.py clean and more understandable.
"""
class Supporter:
def __init__(self, cl=None, packet_callback=None):
self.filter = None
self.last_packet = dict()
self.burst_amount = dict()
self.ratelimits = dict()
self.good_ips = set()
self.known_vpns = set()
self.status = {"repair_mode": True, "is_deprecated": False}
self.cl = cl
self.profanity = profanity
self.packet_handler = packet_callback
self.listener_detected = False
self.listener_id = None
if not self.cl == None:
# Add custom status codes to CloudLink
self.cl.codes["KeyNotFound"] = "I:010 | Key Not Found"
self.cl.codes["PasswordInvalid"] = "I:011 | Invalid Password"
self.cl.codes["GettingReady"] = "I:012 | Getting ready"
self.cl.codes["ObsoleteClient"] = "I:013 | Client is out-of-date or unsupported"
self.cl.codes["Pong"] = "I:014 | Pong"
self.cl.codes["IDExists"] = "I:015 | Account exists"
self.cl.codes["2FAOnly"] = "I:016 | 2FA Required"
self.cl.codes["MissingPermissions"] = "I:017 | Missing permissions"
self.cl.codes["Banned"] = "E:018 | Account Banned"
self.cl.codes["IllegalChars"] = "E:019 | Illegal characters detected"
self.cl.codes["Kicked"] = "E:020 | Kicked"
self.cl.codes["ChatExists"] = "E:021 | Chat exists"
self.cl.codes["ChatNotFound"] = "E:022 | Chat not found"
self.cl.codes["ChatFull"] = "E:023 | Chat full"
self.cl.codes["LoggedOut"] = "I:024 | Logged out"
# Create permitted lists of characters
self.permitted_chars_username = []
self.permitted_chars_post = []
for char in string.ascii_letters:
self.permitted_chars_username.append(char)
self.permitted_chars_post.append(char)
for char in string.digits:
self.permitted_chars_username.append(char)
self.permitted_chars_post.append(char)
for char in string.punctuation:
self.permitted_chars_post.append(char)
self.permitted_chars_username.extend(["_", "-"])
self.permitted_chars_post.append(" ")
# Peak number of users logger
self.peak_users_logger = {
"count": 0,
"timestamp": {
"mo": 0,
"d": 0,
"y": 0,
"h": 0,
"mi": 0,
"s": 0,
"e": 0
}
}
if not self.cl == None:
# Specify server callbacks
self.cl.callback("on_packet", self.on_packet)
self.cl.callback("on_close", self.on_close)
self.cl.callback("on_connect", self.on_connect)
self.log("Supporter initialized!")
def full_stack(self):
exc = sys.exc_info()[0]
if exc is not None:
f = sys.exc_info()[-1].tb_frame.f_back
stack = traceback.extract_stack(f)
else:
stack = traceback.extract_stack()[:-1]
trc = 'Traceback (most recent call last):\n'
stackstr = trc + ''.join(traceback.format_list(stack))
if exc is not None:
stackstr += ' ' + traceback.format_exc().lstrip(trc)
return stackstr
def log(self, event):
print("{0}: {1}".format(self.timestamp(4), event))
def sendPacket(self, payload, listener_detected=False, listener_id=None):
if not self.cl == None:
if listener_detected:
if "id" in payload:
payload["listener"] = listener_id
self.cl.sendPacket(payload)
else:
self.cl.sendPacket(payload)
def get_client_statedata(self, client): # "steals" information from the CloudLink module to get better client data
if not self.cl:
return []
if type(client) is str:
tmp = client
client = []
for session in self.cl._get_obj_of_username(tmp):
client.append(session["id"])
elif type(client) is dict:
client = [client['id']]
elif type(client) is int:
client = [client]
else:
raise Exception("client is not a supported datatype")
statedata = []
for session in client:
if session in self.cl.statedata["ulist"]["objs"]:
statedata.append(self.cl.statedata["ulist"]["objs"][session])
return statedata
def modify_client_statedata(self, client, key, newvalue): # WARN: Use with caution: DO NOT DELETE UNNECESSARY KEYS!
if not self.cl:
return False
if type(client) is str:
tmp = client
client = []
for session in self.cl._get_obj_of_username(tmp):
client.append(session["id"])
elif type(client) is dict:
client = [client['id']]
elif type(client) is int:
client = [client]
else:
raise Exception("client is not a supported datatype")
for session in client:
if session in self.cl.statedata["ulist"]["objs"]:
self.cl.statedata["ulist"]["objs"][session][key] = newvalue
return True
def delete_client_statedata(self, client, key): # WARN: Use with caution: DO NOT DELETE UNNECESSARY KEYS!
if not self.cl == None:
if type(client) == str:
client = self.cl._get_obj_of_username(client)
if not client == None:
if client['id'] in self.cl.statedata["ulist"]["objs"]:
if key in self.cl.statedata["ulist"]["objs"][client['id']]:
try:
del self.cl.statedata["ulist"]["objs"][client['id']][key]
return True
except:
self.log("{0}".format(self.full_stack()))
return False
else:
return False
def on_close(self, client):
if not self.cl == None:
if type(client) == dict:
self.log("{0} Disconnected.".format(client["id"]))
elif type(client) == str:
self.log("{0} Logged out.".format(self.cl._get_username_of_obj(client)))
def on_connect(self, client):
if not self.cl == None:
if self.status["repair_mode"]:
self.log("Refusing connection from {0} due to repair mode being enabled".format(client["id"]))
self.cl.kickClient(client)
else:
self.log("{0} Connected.".format(client["id"]))
self.modify_client_statedata(client, "authtype", "")
self.modify_client_statedata(client, "authed", False)
# Rate limiter
self.modify_client_statedata(client, "last_packet", 0)
def on_packet(self, message):
if not self.cl == None:
# CL Turbo Support
self.listener_detected = ("listener" in message)
self.listener_id = None
if self.listener_detected:
self.listener_id = message["listener"]
# Read packet contents
id = message["id"]
val = message["val"]
clienttype = None
client = message["id"]
if type(message["id"]) == dict:
ip = self.cl.getIPofObject(client)
clienttype = 0
elif type(message["id"]) == str:
ip = self.cl.getIPofUsername(client)
clienttype = 1
# Handle packet
cmd = None
if "cmd" in message:
cmd = message["cmd"]
if not self.packet_handler == None:
self.packet_handler(cmd, ip, val, self.listener_detected, self.listener_id, client, clienttype)
def timestamp(self, ttype):
today = datetime.now()
if ttype == 1:
return {
"mo": (datetime.now()).strftime("%m"),
"d": (datetime.now()).strftime("%d"),
"y": (datetime.now()).strftime("%Y"),
"h": (datetime.now()).strftime("%H"),
"mi": (datetime.now()).strftime("%M"),
"s": (datetime.now()).strftime("%S"),
"e": (int(time.time()))
}
elif ttype == 2:
return str(today.strftime("%H%M%S"))
elif ttype == 3:
return str(today.strftime("%d%m%Y%H%M%S"))
elif ttype == 4:
return today.strftime("%m/%d/%Y %H:%M.%S")
elif ttype == 5:
return today.strftime("%d%m%Y")
def ratelimit(self, client):
# Rate limiter
self.modify_client_statedata(client, "last_packet", int(time.time()))
def wordfilter(self, message):
# Word censor
if self.filter != None:
self.profanity.load_censor_words(whitelist_words=self.filter["whitelist"])
message = self.profanity.censor(message)
self.profanity.load_censor_words(whitelist_words=self.filter["whitelist"], custom_words=self.filter["blacklist"])
message = self.profanity.censor(message)
else:
self.log("Failed loading profanity filter : Using default filter as fallback")
self.profanity.load_censor_words()
message = self.profanity.censor(message)
return message
def isAuthenticated(self, client):
if not self.cl:
return None
tmp = self.get_client_statedata(client)
if len(tmp) > 1:
# this will only occur if there are multiple valid sessions
return True
# this will occur if there is only one session present
return tmp[0]["authed"]
def setAuthenticatedState(self, client, value):
if not self.cl == None:
self.modify_client_statedata(client, "authed", value)
def checkForBadCharsUsername(self, value):
# Check for profanity in username, will return '*' if there's profanity which will be blocked as an illegal character
value = self.wordfilter(value)
badchars = False
for char in value:
if not char in self.permitted_chars_username:
badchars = True
break
return badchars
def checkForBadCharsPost(self, value):
badchars = False
for char in value:
if not char in self.permitted_chars_post:
badchars = True
break
return badchars
def autoID(self, client, username):
if not self.cl == None:
# really janky code that automatically sets user ID
self.modify_client_statedata(client, "username", username)
# multisession
if not username in self.cl.statedata["ulist"]["usernames"]:
self.cl.statedata["ulist"]["usernames"][username] = []
self.cl.statedata["ulist"]["usernames"][username].append(client["id"])
self.sendPacket({"cmd": "ulist", "val": self.cl._get_ulist()})
self.log("{0} autoID given".format(username))
def kickUser(self, username, status="Kicked"):
if not self.cl == None:
if username in self.cl.getUsernames():
self.log("Kicking {0}".format(username))
# Unauthenticate client
sessions = copy(self.cl.statedata["ulist"]["usernames"][username])
self.log("{0}'s sessions: {1}".format(username, sessions))
# Process multi session
for session in sessions:
self.log("Closing {0}'s session: {1}".format(username, session))
# Grab object
client = self.cl.statedata["ulist"]["objs"][session]["object"]
# Thread final closing
def run(client):
# Tell client it's going to get kicked
self.sendPacket({"cmd": "direct", "val": self.cl.codes[status], "id": client})
time.sleep(1)
try:
client["handler"].send_close(1000, bytes('', encoding='utf-8'))
except Exception as e:
self.log("Client {0} Broken pipe error: {1}".format(client['id'], e))
self.cl._closed_connection_server(client, self.cl)
Thread(target=run, args=(client,)).start()
# Update userlists
self.sendPacket({"cmd": "ulist", "val": self.cl._get_ulist()})
def check_for_spam(self, type, client, burst=1, seconds=1):
# Check if type and client are in ratelimit dictionary
if not (type in self.last_packet):
self.last_packet[type] = {}
self.burst_amount[type] = {}
self.ratelimits[type] = {}
if client not in self.last_packet[type]:
self.last_packet[type][client] = 0
self.burst_amount[type][client] = 0
self.ratelimits[type][client] = 0
# Check if user is currently ratelimited
if self.ratelimits[type][client] > time.time():
return True
# Check if max burst has expired
if (self.last_packet[type][client] + seconds) < time.time():
self.burst_amount[type][client] = 0
# Set last packet time and add to burst amount
self.last_packet[type][client] = time.time()
self.burst_amount[type][client] += 1
# Check if burst amount is over max burst
if self.burst_amount[type][client] > burst:
self.ratelimits[type][client] = (time.time() + seconds)
self.burst_amount[type][client] = 0
return True
else:
return False