Merge branch 'master' of github.com:hacknix/FreeDMR

This commit is contained in:
Simon 2021-09-27 22:09:06 +01:00
commit f74593a10a

View File

@ -50,12 +50,13 @@ def IsIPv6Address(ip):
class Proxy(DatagramProtocol): class Proxy(DatagramProtocol):
def __init__(self,Master,ListenPort,connTrack,blackList,Timeout,Debug,DestportStart,DestPortEnd): def __init__(self,Master,ListenPort,connTrack,blackList,Timeout,Debug,ClientInfo,DestportStart,DestPortEnd):
self.master = Master self.master = Master
self.connTrack = connTrack self.connTrack = connTrack
self.peerTrack = {} self.peerTrack = {}
self.timeout = Timeout self.timeout = Timeout
self.debug = Debug self.debug = Debug
self.clientinfo = ClientInfo
self.blackList = blackList self.blackList = blackList
self.destPortStart = DestportStart self.destPortStart = DestportStart
self.destPortEnd = DestPortEnd self.destPortEnd = DestPortEnd
@ -65,7 +66,9 @@ class Proxy(DatagramProtocol):
def reaper(self,_peer_id): def reaper(self,_peer_id):
if self.debug: if self.debug:
print("dead",_peer_id) print("dead",_peer_id)
self.transport.write(b'RPTCL'+_peer_id, (Master,self.peerTrack[_peer_id]['dport'])) if self.clientinfo and _peer_id != b'\x00m@\xd7':
print(f"Client: ID:{str(int_id(_peer_id)).rjust(9)} IP:{self.peerTrack[_peer_id]['shost'].rjust(15)} Port:{self.peerTrack[_peer_id]['sport']} Removed.")
self.transport.write(b'RPTCL'+_peer_id, (self.master,self.peerTrack[_peer_id]['dport']))
self.connTrack[self.peerTrack[_peer_id]['dport']] = False self.connTrack[self.peerTrack[_peer_id]['dport']] = False
del self.peerTrack[_peer_id] del self.peerTrack[_peer_id]
@ -118,18 +121,16 @@ class Proxy(DatagramProtocol):
elif _command == MSTC: elif _command == MSTC:
_peer_id = data[5:9] _peer_id = data[5:9]
# _peer_id = self.connTrack[port]
if self.debug: if self.debug:
print(data) print(data)
if _peer_id in self.peerTrack: if _peer_id in self.peerTrack:
self.transport.write(data,(self.peerTrack[_peer_id]['shost'],self.peerTrack[_peer_id]['sport'])) self.transport.write(data,(self.peerTrack[_peer_id]['shost'],self.peerTrack[_peer_id]['sport']))
#self.peerTrack[_peer_id]['timer'].reset()
# Remove the client after send a MSTN or MSTC packet # Remove the client after send a MSTN or MSTC packet
if _command in (MSTN,MSTC): if _command in (MSTN,MSTC):
self.peerTrack[_peer_id]['timer'].cancel() # Give time to the client for a reply to prevent port reassignment
self.reaper(_peer_id) self.peerTrack[_peer_id]['timer'].reset(15)
return
return
else: else:
@ -159,21 +160,21 @@ class Proxy(DatagramProtocol):
_dport = self.peerTrack[_peer_id]['dport'] _dport = self.peerTrack[_peer_id]['dport']
self.peerTrack[_peer_id]['sport'] = port self.peerTrack[_peer_id]['sport'] = port
self.peerTrack[_peer_id]['shost'] = host self.peerTrack[_peer_id]['shost'] = host
self.transport.write(data, (Master,_dport)) self.transport.write(data, (self.master,_dport))
self.peerTrack[_peer_id]['timer'].reset(self.timeout) self.peerTrack[_peer_id]['timer'].reset(self.timeout)
if self.debug: if self.debug:
print(data) print(data)
return return
else:
else:
if int_id(_peer_id) in self.blackList: if int_id(_peer_id) in self.blackList:
return return
#for _dport in self.connTrack: # Make a list with the available ports
while True: _ports_avail = [port for port in self.connTrack if not self.connTrack[port]]
_dport = random.randint(1,(self.numPorts - 1)) if len(_ports_avail) > 0:
_dport = _dport + self.destPortStart _dport = random.choice(_ports_avail)
if not self.connTrack[_dport]: else:
break return
self.connTrack[_dport] = _peer_id self.connTrack[_dport] = _peer_id
self.peerTrack[_peer_id] = {} self.peerTrack[_peer_id] = {}
self.peerTrack[_peer_id]['dport'] = _dport self.peerTrack[_peer_id]['dport'] = _dport
@ -181,6 +182,9 @@ class Proxy(DatagramProtocol):
self.peerTrack[_peer_id]['shost'] = host self.peerTrack[_peer_id]['shost'] = host
self.peerTrack[_peer_id]['timer'] = reactor.callLater(self.timeout,self.reaper,_peer_id) self.peerTrack[_peer_id]['timer'] = reactor.callLater(self.timeout,self.reaper,_peer_id)
self.transport.write(data, (self.master,_dport)) self.transport.write(data, (self.master,_dport))
if self.clientinfo and _peer_id != b'\x00m@\xd7':
print(f'New client: ID:{str(int_id(_peer_id)).rjust(9)} IP:{host.rjust(15)} Port:{port}, assigned to port:{_dport}.')
if self.debug: if self.debug:
print(data) print(data)
return return
@ -199,6 +203,7 @@ if __name__ == '__main__':
Timeout = 30 Timeout = 30
Stats = False Stats = False
Debug = False Debug = False
ClientInfo = False
BlackList = [1234567] BlackList = [1234567]
#******************* #*******************
@ -221,7 +226,7 @@ if __name__ == '__main__':
if ListenIP == '::' and IsIPv4Address(Master): if ListenIP == '::' and IsIPv4Address(Master):
Master = '::ffff:' + Master Master = '::ffff:' + Master
reactor.listenUDP(ListenPort,Proxy(Master,ListenPort,CONNTRACK,BlackList,Timeout,Debug,DestportStart,DestPortEnd),interface=ListenIP) reactor.listenUDP(ListenPort,Proxy(Master,ListenPort,CONNTRACK,BlackList,Timeout,Debug,ClientInfo,DestportStart,DestPortEnd),interface=ListenIP)
def loopingErrHandle(failure): def loopingErrHandle(failure):
print('(GLOBAL) STOPPING REACTOR TO AVOID MEMORY LEAK: Unhandled error innowtimed loop.\n {}'.format(failure)) print('(GLOBAL) STOPPING REACTOR TO AVOID MEMORY LEAK: Unhandled error innowtimed loop.\n {}'.format(failure))