From 37581ec8b619796a30da11771c5053ee1f111065 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 29 Nov 2020 12:19:04 +0000 Subject: [PATCH] Log hotspot proxy stats to console every 30 secs --- hotspot_proxy.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/hotspot_proxy.py b/hotspot_proxy.py index 4c47715..446327c 100644 --- a/hotspot_proxy.py +++ b/hotspot_proxy.py @@ -1,5 +1,5 @@ from twisted.internet.protocol import DatagramProtocol -from twisted.internet import reactor +from twisted.internet import reactor, task from time import time @@ -38,10 +38,16 @@ class Proxy(DatagramProtocol): if __name__ == '__main__': +#*** CONFIG HERE *** + ListenPort = 62031 DestportStart = 50000 - DestPortEnd = 50300 - Timeout = 60 + DestPortEnd = 50500 + Timeout = 35 + Stats = True + +#******************* + CONNTRACK = {} @@ -50,4 +56,27 @@ if __name__ == '__main__': reactor.listenUDP(ListenPort,Proxy(ListenPort,CONNTRACK,Timeout)) + 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) + reactor.run() +