-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay.py
53 lines (45 loc) · 1.52 KB
/
relay.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
import json
import socket
import time
#outbound = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
CLIENTS = {}
def list_clients():
now = time.time()
if now - getattr(list_clients, 'last_prune_time', 0) > 5:
stale = [addr for addr, cli in CLIENTS.items() if now - cli['last_ping'] > 15]
for addr in stale:
del CLIENTS[addr]
list_clients.last_prune_time = now
return [
{'name': cli['name'], 'addr': addr} for addr, cli in CLIENTS.items()
]
def handle_msg(body, addr):
msgtype = body['type']
if msgtype == 'enter':
name = body['from']
CLIENTS[addr] = {'name': name, 'last_ping': time.time()}
return {'youare': addr, 'clients': list_clients()}
if msgtype == 'ping':
if addr in CLIENTS:
CLIENTS[addr]['last_ping'] = time.time()
return {'type': 'pong', 'clients': list_clients()}
if msgtype == 'leave':
CLIENTS.pop(addr, None)
return {}
if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 5005))
while True:
data, addr = sock.recvfrom(1024)
print(addr, "->", data)
try:
body = json.loads(data.decode('ascii'))
reply = handle_msg(body, addr)
if reply is None:
continue
except Exception:
continue
reply['from'] = 'relay'
if 'seq' in body:
reply['seq'] = body['seq']
sock.sendto(json.dumps(reply).encode('ascii'), addr)