-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
508 lines (421 loc) · 17.8 KB
/
server.py
File metadata and controls
508 lines (421 loc) · 17.8 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import base64
import datetime
import socket
import threading
import os
import sys
import importlib
import site
import msvcrt
import time
class PackageManager:
@staticmethod
def safe_import(modules: set[str]):
needed = []
for module in modules:
try:
for m in modules[module]:
importlib.import_module(m)
except ModuleNotFoundError:
needed.append(module)
if len(needed) != 0:
os.system("cls" if os.name == "nt" else "clear")
print("Installing missing modules... please wait.")
os.system(f"{sys.executable} -m pip install " + " ".join(needed))
importlib.reload(site)
for module in needed:
for m in modules[module]:
importlib.import_module(m)
modules = {
"keyboard": ["keyboard"],
"pywin32": ["win32gui", "win32process"]
}
PackageManager.safe_import(modules)
import keyboard
import win32gui
import win32process
client_lock = threading.Lock()
class Terminal:
curr_input_buff = ""
@staticmethod
def clear():
os.system("cls" if os.name == "nt" else "clear")
@staticmethod
def log_info(message):
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"{Colors.INFO_COLOR}[INFO] {current_time}: {message}{Colors.RESET}")
Terminal.print_input_buff()
@staticmethod
def log_warning(message):
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"{Colors.WARNING_COLOR}[WARNING] {current_time}: {message}{Colors.RESET}")
Terminal.print_input_buff()
@staticmethod
def log_error(message):
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"{Colors.ERROR_COLOR}[ERROR] {current_time}: {message}{Colors.RESET}")
Terminal.print_input_buff()
@staticmethod
def log_debug(message):
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"{Colors.DEBUG_COLOR}[DEBUG] {current_time}: {message}{Colors.RESET}")
Terminal.print_input_buff()
@staticmethod
def print_input_buff(length=None):
length = length if length else len(Terminal.curr_input_buff)
print(" " * (length + 4), end="\r")
print(f">>> {Terminal.curr_input_buff} ", end="\r", flush=True)
@staticmethod
def on_key_press(key_event):
if not chat_obj: return
if not is_window_focused(): return
with client_lock:
key = key_event.name
if len(key) == 1:
Terminal.curr_input_buff += key
elif key == "enter":
chat_obj.handle_command(Terminal.curr_input_buff)
prev_len = len(Terminal.curr_input_buff)
Terminal.curr_input_buff = ""
Terminal.print_input_buff(prev_len)
elif key == "backspace":
Terminal.curr_input_buff = Terminal.curr_input_buff[:-1]
elif key == "space":
Terminal.curr_input_buff += " "
else:
return
Terminal.print_input_buff()
class Colors():
RESET = "\033[0m"
INFO_COLOR = "\033[94m"
WARNING_COLOR = "\033[93m"
ERROR_COLOR = "\033[91m"
DEBUG_COLOR = "\033[90m"
class ChatOptions():
SEP = b"\0"
PORT = 5432
DISCOVER_PORT = 5431
MSG_CUTOFF = 20
SPAM_TIME_WINDOW_SEC = 3
MAX_MESSAGES_IN_SPAM_WINDOW = 3
Terminal.clear()
CURR_WINDOW_PID = win32process.GetWindowThreadProcessId(win32gui.GetForegroundWindow())
chat_obj = None
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
s.connect(('10.254.254.254', 1))
ip = s.getsockname()[0]
except Exception:
ip = '127.0.0.1'
finally:
s.close()
return ip
def is_window_focused():
if CURR_WINDOW_PID == win32process.GetWindowThreadProcessId(win32gui.GetForegroundWindow()):
return True
return False
class ChatServer:
def __init__(self):
Terminal.log_info("Starting up server...")
code = get_ip().split(".")[-1]
Terminal.log_info(f"SERVER JOIN CODE: {Colors.ERROR_COLOR}{code}{Colors.RESET}")
mpart = int(input("Please enter the max participants: "))
self.server_name = input("Please enter server name (Press Enter to make private server): ")
self.server = socket.socket()
self.server.bind(("0.0.0.0", ChatOptions.PORT))
self.server.listen(mpart)
self.clients = {}
self.chat = []
self.banned_client_ips = []
self.msg_times_window = []
self.open = True
def close_server(self):
if not is_window_focused(): return
try:
self.server.shutdown(socket.SHUT_RDWR)
self.server.close()
except OSError:
print("Faild to shutdown server socket...")
with client_lock:
self.open = False
print("Closing clients...")
print("Closing threads...")
for c in self.clients.values():
c['socket'].shutdown(socket.SHUT_RDWR)
c['socket'].close()
self.clients = {}
print("Closing server...")
print("Shutting down...")
print("Releasing listening thread...")
s = socket.socket()
s.connect(("127.0.0.1", ChatOptions.PORT))
s.close()
print("Flushing input buffer...")
while msvcrt.kbhit():
try:
msvcrt.getch()
except:
pass
def send_message(self, to, parts):
body = ChatOptions.SEP.join([p.encode() for p in parts])
msg = f"{len(body):04}"
if to == "brd":
for c in self.clients.values():
try:
c["socket"].sendall(msg.encode() + body)
except Exception:
pass
else:
if type(to) == str:
client = self.clients.get(to)["socket"]
else:
client = to
if not client:
Terminal.log_warning("Tried sending a message to an IP that is not registered!")
return False
try:
client.sendall(msg.encode() + body)
except Exception:
pass
def recieve_message(self, from_ip):
try:
client = self.clients.get(from_ip)
soc = client['socket'] if client else from_ip
client_ip = from_ip if client else from_ip.getpeername()
if client and client["disconnect_c"] == 3:
Terminal.log_debug(f"{from_ip} has sent 3 empty packets")
return None
if not client:
Terminal.log_warning("Tried sending a message to an IP that is not registered!")
msg_len = soc.recv(4)
if len(msg_len) == 0:
Terminal.log_debug(f"{client_ip} sent an empty packet")
if client:
self.clients[from_ip]["disconnect_c"] += 1
else:
return None
return False
msg_len = msg_len.decode()
if not msg_len.isdigit():
Terminal.log_error(f"Got an invalid packet length from {client_ip} - {client['name']} ({msg_len})!")
return False
data = soc.recv(int(msg_len))
if len(data) == 0:
Terminal.log_debug(f"{client_ip} sent an empty packet")
if client:
self.clients[from_ip]["disconnect_c"] += 1
else:
return None
return False
if client: self.clients[from_ip]["disconnect_c"] = 0
return [d.decode() for d in data.split(ChatOptions.SEP)]
except (ConnectionResetError, BrokenPipeError, ConnectionAbortedError):
return None
def __is_name_in_use(self, name):
for c in self.clients.values():
if c["name"] == name:
return True
return False
def register_client(self, ip, port, soc):
tries = 0
while tries < 3 and self.open:
tries += 1
Terminal.log_info(f"Got connection from {ip}:{port}, waiting for name ({tries}/3)...")
data = self.recieve_message(soc)
if data is None:
return None
if data == False: continue
if data[0] != "NAME":
Terminal.log_error("Invalid name packet! skipping...")
continue
name = data[1]
if self.__is_name_in_use(name) or name == "SERVER" or ' ' in name:
self.send_message(soc, ["CHNM"])
Terminal.log_warning(f"{ip} tried using the name \"{name}\", requested change.")
continue
self.send_message(soc, ["OKNM"])
self.clients[ip] = {
"name": name,
"socket": soc,
"disconnect_c": 0
}
Terminal.log_info(f"Successfully registered a new client ({ip} - {name})")
return True
Terminal.log_error("Failed to register user, disconnecting...")
soc.close()
return None
def __is_spam(self):
# Advance window
curr_sec = time.time() - ChatOptions.SPAM_TIME_WINDOW_SEC
new_times = []
for msg_time in self.msg_times_window:
if msg_time > curr_sec:
new_times.append(msg_time)
new_times.append(curr_sec+1)
self.msg_times_window = new_times
if len(new_times) >= ChatOptions.MAX_MESSAGES_IN_SPAM_WINDOW:
return True
return False
def handle_client(self, ip, port, soc):
if ip == "127.0.0.1":
return
succ = self.register_client(ip, port, soc)
if not succ:
Terminal.log_error(f"{ip} failed to connect.")
soc.shutdown(socket.SHUT_RDWR)
soc.close()
return
client_data = self.clients.get(ip)
if client_data is None:
Terminal.log_error(f"Invalid client! ignoring.")
return
while self.open and ip in self.clients:
data = self.recieve_message(ip)
try:
if data is None:
Terminal.log_warning(f"Client disconnected ({ip}) successfully!")
self.clients[ip]["socket"].close()
if ip in self.clients: del self.clients[ip]
self.send_message("brd", ["LEFT", client_data['name']])
self.chat.append({
"event": "left",
"name": client_data['name']
})
break
if data == False:
continue
action = data[0]
if action == "SEND":
if self.__is_spam():
continue
try:
if data[1].startswith("/msg ") or data[1].startswith("/send "): # Whisper functionality
to = data[1].split(" ", 2)[1]
msg = ' '.join(data[1].split(" ")[2:])
for (i, c) in self.clients.items():
if to == c['name']:
self.send_message(i, ["WSPR", client_data['name'], msg])
self.send_message(ip, ["WSRC", c['name'], msg])
Terminal.log_debug(f"{client_data['name']} -> {c['name']}: {msg}")
break
else:
self.send_message(ip, ["WSNO"])
Terminal.log_warning(
f"{client_data['name']} - {ip} tried to whisper to {to} which is not a valid client")
continue
except Exception:
pass
if data[1].strip() == "": continue
self.chat.append({
"event": "message",
"name": client_data["name"],
"message": data[1]
})
self.send_message("brd", ["RECV", client_data["name"], data[1]])
Terminal.log_debug(f"Message by {ip} - {client_data["name"]}: {data[1]}")
elif action == "CTUP":
chat_serialized = self.chat if len(self.chat) <= ChatOptions.MSG_CUTOFF else self.chat[
:ChatOptions.MSG_CUTOFF]
chat_b64 = base64.b64encode(str(chat_serialized).encode())
self.send_message(ip, ["CTUP", chat_b64.decode()])
self.send_message("brd", ["JOIN", client_data['name']])
self.chat.append({
"event": "join",
"name": client_data['name']
})
except Exception:
return None
def accept_clients(self):
s = self.server
while self.open:
soc, addr = s.accept()
if addr[0] in self.banned_client_ips:
Terminal.log_warning(f"{addr[0]} tried joining the server while banned.")
self.send_message(soc, ["RECV", "SERVER", "You are IP banned for this session."])
soc.shutdown(socket.SHUT_RDWR)
soc.close()
continue
thread = threading.Thread(target=self.handle_client, args=(addr[0], addr[1], soc,), daemon=True)
thread.start()
def handle_command(self, cmd):
parts = cmd.split(" ")
if parts[0] == "kick" and len(parts) == 2:
for (ip, c) in self.clients.items():
if ip == parts[1] or c["name"] == parts[1]:
Terminal.log_warning(f"Kicked {c["name"]} - {ip}")
c["socket"].shutdown(socket.SHUT_RDWR)
c["socket"].close()
if ip in self.clients: del self.clients[ip]
break
else:
Terminal.log_error(f"Could not find client with name/ip of {parts[1]}")
elif parts[0] == "ban" and len(parts) == 2:
for (ip, c) in self.clients.items():
if ip == parts[1] or c["name"] == parts[1]:
Terminal.log_warning(f"Ban Ip {c["name"]} - {ip}")
c["socket"].shutdown(socket.SHUT_RDWR)
c["socket"].close()
if ip in self.clients: del self.clients[ip]
self.banned_client_ips.append(ip)
break
else:
Terminal.log_error(f"Could not find client with name/ip of {parts[1]}")
elif parts[0] == "unban" and len(parts) == 2:
if parts[1] in self.banned_client_ips:
Terminal.log_warning(f"Unbanned ip {parts[1]}")
self.banned_client_ips.remove(parts[1])
else:
Terminal.log_error(f"Could not find client with name/ip of {parts[1]}")
elif parts[0] == "say":
msg = " ".join(parts[1:])
self.chat.append({
"event": "message",
"name": "SERVER",
"message": msg
})
Terminal.log_info(f"Brodcasted message: {msg}")
self.send_message("brd", ["RECV", "SERVER", msg])
elif parts[0] == "list":
Terminal.log_info(f"{len(self.clients.keys())} client(s) connected:")
for (ip, c) in self.clients.items():
Terminal.log_info(f"\t-{ip} - {c['name']}")
elif parts[0] == "tell":
to = parts[1]
msg = ' '.join(parts[2:])
for (ip, c) in self.clients.items():
if to == c['name'] or to == ip:
self.send_message(ip, ["WSPR", "SERVER", msg])
Terminal.log_debug(f"Whispered to {c['name']}: {msg}")
break
else:
Terminal.log_error(f"Could not find client with name/ip of {parts[1]}")
elif parts[0] == "clear":
Terminal.log_info(f"Cleared chat!")
self.chat[:] = []
elif parts[0] == "exit":
self.close_server()
else:
Terminal.log_error("Unknown Command!")
def discover_process(self):
if self.server_name == "": return
self.server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server.bind(("0.0.0.0", ChatOptions.DISCOVER_PORT))
while self.open:
try:
data, addr = self.server.recvfrom(8)
if data == b"DISCOVER":
self.server.sendto(self.server_name.encode(), addr)
except (KeyboardInterrupt, Exception):
return
if __name__ == "__main__":
chat_obj = ChatServer()
Terminal.print_input_buff()
client_accept_thread = threading.Thread(target=chat_obj.accept_clients, daemon=True)
client_accept_thread.start()
server_discover_thread = threading.Thread(target=chat_obj.discover_process, daemon=True)
server_discover_thread.start()
keyboard.on_press(Terminal.on_key_press)
server_discover_thread.join()
client_accept_thread.join()