2020-11-28 15:52:22 -05:00
|
|
|
from twisted.internet.protocol import DatagramProtocol
|
2020-11-29 07:19:04 -05:00
|
|
|
from twisted.internet import reactor, task
|
2020-11-28 15:52:22 -05:00
|
|
|
from time import time
|
|
|
|
|
|
|
|
|
|
|
|
class Proxy(DatagramProtocol):
|
|
|
|
|
2020-11-30 17:09:51 -05:00
|
|
|
def __init__(self,ListenPort,connTrack,Timeout,Debug):
|
2020-11-28 15:52:22 -05:00
|
|
|
self.connTrack = connTrack
|
2020-12-03 15:28:23 -05:00
|
|
|
self.sourceTrack = {}
|
2020-11-28 15:52:22 -05:00
|
|
|
self.timeout = Timeout
|
2020-11-30 17:09:51 -05:00
|
|
|
self.debug = Debug
|
2020-11-28 15:52:22 -05:00
|
|
|
|
|
|
|
def datagramReceived(self, data, addr):
|
|
|
|
host,port = addr
|
|
|
|
|
2020-12-03 15:28:23 -05:00
|
|
|
nowtime = time()
|
|
|
|
|
2020-11-30 17:09:51 -05:00
|
|
|
Debug = self.debug
|
|
|
|
|
|
|
|
#If the packet comes from the master
|
2020-11-28 15:52:22 -05:00
|
|
|
if host == '127.0.0.1' and port in self.connTrack:
|
2020-12-03 15:28:23 -05:00
|
|
|
if int(self.connTrack[port]['time'])+self.timeout >nowtime:
|
2020-11-28 15:52:22 -05:00
|
|
|
self.transport.write(data,(self.connTrack[port]['host'],self.connTrack[port]['sport']))
|
2020-12-28 06:17:59 -05:00
|
|
|
#if master refuses login, remove tracking
|
2020-12-28 06:32:43 -05:00
|
|
|
if data[0:6] == b'MSTNAK':
|
2020-12-28 09:33:11 -05:00
|
|
|
if self.sourceTrack[self.connTrack[port]['host']+":"+self.connTrack[port]['sport']]:
|
|
|
|
del self.sourceTrack[self.connTrack[port]['host']+":"+self.connTrack[port]['sport']]
|
2020-11-30 17:09:51 -05:00
|
|
|
if Debug:
|
2020-12-03 15:28:23 -05:00
|
|
|
print("return path match")
|
2020-11-30 17:09:51 -05:00
|
|
|
print(data)
|
2020-12-27 17:00:07 -05:00
|
|
|
elif host+":"+str(port) in self.sourceTrack:
|
2020-12-28 09:33:11 -05:00
|
|
|
del self.sourceTrack[self.connTrack[port]['host']+":"+self.connTrack[port]['sport']]
|
2020-12-03 15:28:23 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
#If we have a sourcetrack for this connect and thenowtimeout has not expired, forward to tracked port
|
2020-12-27 17:00:07 -05:00
|
|
|
if host+":"+str(port) in self.sourceTrack and (int(self.sourceTrack[host+":"+str(port)]['time'])+self.timeout >nowtime):
|
|
|
|
self.transport.write(data, ('127.0.0.1',self.sourceTrack[host+":"+str(port)]['dport']))
|
|
|
|
self.connTrack[self.sourceTrack[host+":"+str(port)]['dport']]['time'] =nowtime
|
|
|
|
self.sourceTrack[host+":"+str(port)]['time'] =nowtime
|
2020-12-03 15:28:23 -05:00
|
|
|
if Debug:
|
|
|
|
print("Tracked inbound match")
|
|
|
|
print(data)
|
|
|
|
return
|
2020-12-27 17:00:07 -05:00
|
|
|
elif host+":"+str(port) in self.sourceTrack:
|
|
|
|
del self.sourceTrack[host+":"+str(port)]
|
2020-11-30 17:09:51 -05:00
|
|
|
|
|
|
|
#Find free port to map for new connection
|
2020-11-28 15:52:22 -05:00
|
|
|
for dport in self.connTrack:
|
2020-12-28 09:33:11 -05:00
|
|
|
if (self.connTrack[dport]['time'] == False or (int(self.connTrack[dport]['time'])+self.timeout < nowtime)):
|
2020-11-28 15:52:22 -05:00
|
|
|
self.connTrack[dport]['sport'] = port
|
|
|
|
self.connTrack[dport]['host'] = host
|
2020-12-03 15:28:23 -05:00
|
|
|
self.connTrack[dport]['time'] =nowtime
|
2020-12-27 17:00:07 -05:00
|
|
|
self.sourceTrack[host+":"+str(port)] = {}
|
|
|
|
self.sourceTrack[host+":"+str(port)]['dport'] = dport
|
2020-12-28 09:33:11 -05:00
|
|
|
self.sourceTrack[host+":"+str(port)]['time'] = nowtime
|
2020-11-28 15:52:22 -05:00
|
|
|
self.transport.write(data, ('127.0.0.1',dport))
|
2020-11-30 17:09:51 -05:00
|
|
|
if Debug:
|
2020-12-03 15:28:23 -05:00
|
|
|
print("New connection")
|
2020-11-30 17:09:51 -05:00
|
|
|
print(data)
|
2020-11-28 15:52:22 -05:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2020-11-29 07:19:04 -05:00
|
|
|
#*** CONFIG HERE ***
|
|
|
|
|
2020-11-28 15:52:22 -05:00
|
|
|
ListenPort = 62031
|
2020-11-30 17:09:51 -05:00
|
|
|
DestportStart = 54001
|
|
|
|
DestPortEnd = 54002
|
2020-11-29 07:19:04 -05:00
|
|
|
Timeout = 35
|
|
|
|
Stats = True
|
2020-12-03 15:28:23 -05:00
|
|
|
Debug = True
|
2020-11-29 07:19:04 -05:00
|
|
|
|
|
|
|
#*******************
|
|
|
|
|
2020-11-28 15:52:22 -05:00
|
|
|
|
|
|
|
CONNTRACK = {}
|
|
|
|
|
|
|
|
for port in range(DestportStart,DestPortEnd,1):
|
2020-11-30 17:09:51 -05:00
|
|
|
CONNTRACK[port] = {'host': False,'time': False,'sport':False, 'nacktime': False}
|
2020-11-28 15:52:22 -05:00
|
|
|
|
2020-11-30 17:09:51 -05:00
|
|
|
reactor.listenUDP(ListenPort,Proxy(ListenPort,CONNTRACK,Timeout,Debug))
|
2020-11-28 15:52:22 -05:00
|
|
|
|
2020-11-29 07:19:04 -05:00
|
|
|
def loopingErrHandle(failure):
|
2020-12-03 15:28:23 -05:00
|
|
|
print('(GLOBAL) STOPPING REACTOR TO AVOID MEMORY LEAK: Unhandled error innowtimed loop.\n {}'.format(failure))
|
2020-11-29 07:19:04 -05:00
|
|
|
reactor.stop()
|
|
|
|
|
|
|
|
def stats():
|
|
|
|
count = 0
|
2020-12-03 15:28:23 -05:00
|
|
|
nowtime = time()
|
2020-11-29 07:19:04 -05:00
|
|
|
for port in CONNTRACK:
|
2020-12-03 15:28:23 -05:00
|
|
|
if int(CONNTRACK[port]['time'])+Timeout > nowtime:
|
2020-11-29 07:19:04 -05:00
|
|
|
count = count+1
|
|
|
|
|
|
|
|
totalPorts = DestPortEnd - DestportStart
|
|
|
|
freePorts = totalPorts - count
|
|
|
|
|
|
|
|
print("{} ports out of {} in use ({} free)".format(count,totalPorts,freePorts))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if Stats == True:
|
|
|
|
stats_task = task.LoopingCall(stats)
|
|
|
|
statsa = stats_task.start(30)
|
|
|
|
statsa.addErrback(loopingErrHandle)
|
|
|
|
|
2020-11-28 15:52:22 -05:00
|
|
|
reactor.run()
|
2020-11-29 07:19:04 -05:00
|
|
|
|