RYSEN/hotspot_proxy.py

111 lines
4.0 KiB
Python
Raw Normal View History

2020-11-28 15:52:22 -05:00
from twisted.internet.protocol import DatagramProtocol
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
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
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:
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']))
#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:
print("return path match")
2020-11-30 17:09:51 -05:00
print(data)
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']]
return
#If we have a sourcetrack for this connect and thenowtimeout has not expired, forward to tracked port
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
if Debug:
print("Tracked inbound match")
print(data)
return
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
self.connTrack[dport]['time'] =nowtime
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:
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__':
#*** CONFIG HERE ***
2020-11-28 15:52:22 -05:00
ListenPort = 62031
2020-11-30 17:09:51 -05:00
DestportStart = 54001
DestPortEnd = 54002
Timeout = 35
Stats = True
Debug = True
#*******************
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
def loopingErrHandle(failure):
print('(GLOBAL) STOPPING REACTOR TO AVOID MEMORY LEAK: Unhandled error innowtimed loop.\n {}'.format(failure))
reactor.stop()
def stats():
count = 0
nowtime = time()
for port in CONNTRACK:
if int(CONNTRACK[port]['time'])+Timeout > nowtime:
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()