From 6afdcf9b87e0a6d4c0a3341af19df2af0e9f7e64 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 28 Nov 2020 20:52:22 +0000 Subject: [PATCH] add hotspot proxy --- hotspot_proxy.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 hotspot_proxy.py diff --git a/hotspot_proxy.py b/hotspot_proxy.py new file mode 100644 index 0000000..4c47715 --- /dev/null +++ b/hotspot_proxy.py @@ -0,0 +1,53 @@ +from twisted.internet.protocol import DatagramProtocol +from twisted.internet import reactor +from time import time + + +class Proxy(DatagramProtocol): + + def __init__(self,ListenPort,connTrack,Timeout): + self.connTrack = connTrack + self.timeout = Timeout + + def datagramReceived(self, data, addr): + host,port = addr + + 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'])) + return + + for dport in self.connTrack: + 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() + return + + 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)) + return + + + +if __name__ == '__main__': + + ListenPort = 62031 + DestportStart = 50000 + DestPortEnd = 50300 + Timeout = 60 + + CONNTRACK = {} + + for port in range(DestportStart,DestPortEnd,1): + CONNTRACK[port] = {'host': False,'time': False,'sport':False} + + reactor.listenUDP(ListenPort,Proxy(ListenPort,CONNTRACK,Timeout)) + + reactor.run()