-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.py
executable file
·58 lines (43 loc) · 1.76 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
#!/usr/bin/env python
"""UDP hole punching server."""
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
import sys
class ServerProtocol(DatagramProtocol):
"""
Server protocol implementation.
Server listens for UDP messages. Once it receives a message it registers
this client for a peer link in the waiting list.
As soon as a second client connects, information about one client (public
IP address and port) is sent to the other and vice versa.
Those clients are now considered linked and removed from the waiting list.
"""
def __init__(self):
"""Initialize with empy address list."""
self.addresses = []
def addressString(self, address):
"""Return a string representation of an address."""
ip, port = address
return ':'.join([ip, str(port)])
def datagramReceived(self, datagram, address):
"""Handle incoming datagram messages."""
if datagram == '0':
print 'Registration from %s:%d' % address
self.transport.write('ok', address)
self.addresses.append(address)
if len(self.addresses) >= 2:
msg_0 = self.addressString(self.addresses[1])
msg_1 = self.addressString(self.addresses[0])
self.transport.write(msg_0, self.addresses[0])
self.transport.write(msg_1, self.addresses[1])
self.addresses.pop(0)
self.addresses.pop(0)
print 'Linked peers'
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: ./server.py PORT"
sys.exit(1)
port = int(sys.argv[1])
reactor.listenUDP(port, ServerProtocol())
print 'Listening on *:%d' % (port)
reactor.run()