forked from lunixbochs/uberserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NATServer.py
executable file
·46 lines (38 loc) · 1.26 KB
/
NATServer.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
import SocketServer, sys
class CustomUDPServer(SocketServer.UDPServer):
def Bind(self, root):
self._root = root
def finish_request(self, request, client_address):
if '_root' in dir(self):
self.RequestHandlerClass(request, client_address, self, self._root)
else:
pass # not bound to _root yet, no point in handling UDP
class handler(SocketServer.DatagramRequestHandler):
def __init__(self, request, client_address, server, root):
self._root = root
self.request = request
self.client_address = client_address
self.server = server
try:
self.setup()
self.handle()
self.finish()
finally:
sys.exc_traceback = None # Help garbage collection
def handle(self):
addr = self.client_address
msg = self.rfile.readline().rstrip()
#print "%s from %s(%d)" % (msg, addr[0], addr[1])
self.wfile.write('PONG')
if msg in self._root.usernames:
self._root.usernames[msg]._protocol._udp_packet(msg, addr[0], addr[1])
class NATServer:
def __init__(self, port):
self.s = CustomUDPServer(('',port), handler)
print "Awaiting UDP messages on port %d" % port
def bind(self, root):
self.s.Bind(root)
def start(self):
self.s.serve_forever()
def close(self):
self.s.close()