from twisted.internet.protocol import DatagramProtocol from twisted.internet import reactor, task from time import time class Proxy(DatagramProtocol): def __init__(self,ListenPort,connTrack,Timeout,Debug): self.connTrack = connTrack self.sourceTrack = {} self.timeout = Timeout self.debug = Debug def datagramReceived(self, data, addr): host,port = addr nowtime = time() Debug = self.debug #If the packet comes from the master if host == '127.0.0.1' and port in self.connTrack: if int(self.connTrack[port]['time'])+self.timeout >nowtime: self.transport.write(data,(self.connTrack[port]['host'],self.connTrack[port]['sport'])) #if master refuses login, remove tracking and block fornowtimeout seconds #if data == b'MSTNAK\x00#\xbf"': #self.connTrack[port]['time'] = False #self.connTrack[port]['nacktime'] =nowtime+self.timeout if Debug: print("return path match") print(data) elif host+str(port) in self.sourceTrack: del self.sourceTrack[host+str(port)] 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)] #Find free port to map for new connection for dport in self.connTrack: if (self.connTrack[dport]['time'] == False or (int(self.connTrack[dport]['time'])+self.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) reactor.run()