-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
96 lines (75 loc) · 2.79 KB
/
api.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
import sys
import html
import asyncio
import websockets
from datetime import datetime, timedelta
MAX_CONNECTIONS = 1000000 # Maximum allowed concurrent connections
CONNECTION_TIMEOUT = 300 # Connection timeout in seconds
MAX_CONNECTIONS_PER_IP = 3 # Maximum allowed connections per IP address
MAX_MESSAGES_PER_MINUTE = 20 # Maximum allowed messages per minute per IP address
connected = set()
ip_connections = {}
ip_last_message = {}
username_mapping = {}
async def chat(websocket, path):
# Check maximum concurrent connections
if len(connected) >= MAX_CONNECTIONS:
await websocket.close()
return
# Check maximum connections per IP
client_ip = websocket.remote_address[0]
ip_connections.setdefault(client_ip, 0)
if ip_connections[client_ip] >= MAX_CONNECTIONS_PER_IP:
await websocket.close()
return
connected.add(websocket)
ip_connections[client_ip] += 1
try:
is_first_message = True
while True:
message = await websocket.recv()
if is_first_message:
is_first_message = False
if message in username_mapping.values():
await websocket.send("re::re")
is_first_message = True
continue
else:
username_mapping[websocket] = message
await broadcast(f"join::{message}")
else:
if not check_rate_limit(client_ip):
await websocket.close()
return
await broadcast(f"msg::{username_mapping[websocket]}::{message}")
update_last_message(client_ip)
except websockets.exceptions.ConnectionClosed:
if websocket in username_mapping:
username = username_mapping.pop(websocket)
await broadcast(f"left::{username}")
finally:
connected.remove(websocket)
ip_connections[client_ip] -= 1
def check_rate_limit(client_ip):
# Check if rate limit exceeded
if client_ip in ip_last_message:
last_message_time = ip_last_message[client_ip]
elapsed_time = datetime.now() - last_message_time
if elapsed_time < timedelta(minutes=1) and ip_connections[client_ip] > MAX_MESSAGES_PER_MINUTE:
return False
return True
def update_last_message(client_ip):
# Update the last message time for the IP address
ip_last_message[client_ip] = datetime.now()
async def broadcast(message):
message = html.escape(message)
for client in connected:
try:
await client.send(message)
except:
pass
ip = sys.argv[1]
port = sys.argv[2]
start_server = websockets.serve(chat, ip , int(port))
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()