RYSEN/hotspot_proxy.py

105 lines
3.6 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.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-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 > time():
self.transport.write(data,(self.connTrack[port]['host'],self.connTrack[port]['sport']))
2020-11-30 17:09:51 -05:00
#if master refuses login, remove tracking and block for timeout seconds
if data == b'MSTNAK\x00#\xbf"':
self.connTrack[port]['time'] = False
self.connTrack[port]['nacktime'] = time()+self.timeout
if Debug:
print(data)
2020-11-28 15:52:22 -05:00
return
for dport in self.connTrack:
2020-11-30 17:09:51 -05:00
#If blocked from refused login, ignore the packet if its been less than nacktime
if int(self.connTrack[dport]['nacktime']) + self.timeout > time():
if Debug:
print("NACK\n")
return
#If we have a conntrack for this connect and the timeout has not expired, forward to tracked port
2020-11-28 15:52:22 -05:00
if self.connTrack[dport]['host'] == host and self.connTrack[dport]['sport'] == port and (int(self.connTrack[dport]['time'])+self.timeout > time()):
self.connTrack[dport]['time'] = time()
self.connTrack[dport]['host'] = host
self.connTrack[dport]['sport'] = port
self.transport.write(data, ('127.0.0.1',dport))
self.connTrack[dport]['time'] = time()
2020-11-30 17:09:51 -05:00
if Debug:
print(data)
2020-11-28 15:52:22 -05:00
return
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:
if (self.connTrack[dport]['time'] == False or (int(self.connTrack[dport]['time'])+self.timeout < time())):
self.connTrack[dport]['sport'] = port
self.connTrack[dport]['host'] = host
self.connTrack[dport]['time'] = time()
self.transport.write(data, ('127.0.0.1',dport))
2020-11-30 17:09:51 -05:00
if Debug:
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
2020-11-30 17:09:51 -05:00
Debug = False
#*******************
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 in timed loop.\n {}'.format(failure))
reactor.stop()
def stats():
count = 0
for port in CONNTRACK:
if int(CONNTRACK[port]['time'])+Timeout > time():
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()