Listen IP and IPv6 support in proxy
plus a bit of tidying up
This commit is contained in:
parent
cc5700878a
commit
f9e5ed71f1
@ -4,6 +4,7 @@ from time import time
|
|||||||
from resettabletimer import ResettableTimer
|
from resettabletimer import ResettableTimer
|
||||||
from dmr_utils3.utils import int_id
|
from dmr_utils3.utils import int_id
|
||||||
import random
|
import random
|
||||||
|
import ipaddress
|
||||||
|
|
||||||
# Does anybody read this stuff? There's a PEP somewhere that says I should do this.
|
# Does anybody read this stuff? There's a PEP somewhere that says I should do this.
|
||||||
__author__ = 'Simon Adlem - G7RZU'
|
__author__ = 'Simon Adlem - G7RZU'
|
||||||
@ -13,6 +14,21 @@ __license__ = 'GNU GPLv3'
|
|||||||
__maintainer__ = 'Simon Adlem G7RZU'
|
__maintainer__ = 'Simon Adlem G7RZU'
|
||||||
__email__ = 'simon@gb7fr.org.uk'
|
__email__ = 'simon@gb7fr.org.uk'
|
||||||
|
|
||||||
|
def IsIPv4Address(ip):
|
||||||
|
try:
|
||||||
|
ipaddress.IPv4Address(ip)
|
||||||
|
return True
|
||||||
|
except ValueError as errorCode:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
def IsIPv6Address(ip):
|
||||||
|
try:
|
||||||
|
ipaddress.IPv6Address(ip)
|
||||||
|
return True
|
||||||
|
except ValueError as errorCode:
|
||||||
|
pass
|
||||||
|
|
||||||
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,DestportStart,DestPortEnd):
|
||||||
@ -30,7 +46,7 @@ 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, ('127.0.0.1',self.peerTrack[_peer_id]['dport']))
|
self.transport.write(b'RPTCL'+_peer_id, (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]
|
||||||
|
|
||||||
@ -57,6 +73,8 @@ class Proxy(DatagramProtocol):
|
|||||||
RPTA = b'RPTA'
|
RPTA = b'RPTA'
|
||||||
RPTO = b'RPTO'
|
RPTO = b'RPTO'
|
||||||
|
|
||||||
|
_peer_id = False
|
||||||
|
|
||||||
host,port = addr
|
host,port = addr
|
||||||
|
|
||||||
nowtime = time()
|
nowtime = time()
|
||||||
@ -124,7 +142,7 @@ 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, ('127.0.0.1',_dport))
|
self.transport.write(data, (Master,_dport))
|
||||||
self.peerTrack[_peer_id]['timer'].reset()
|
self.peerTrack[_peer_id]['timer'].reset()
|
||||||
if self.debug:
|
if self.debug:
|
||||||
print(data)
|
print(data)
|
||||||
@ -158,10 +176,12 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
Master = "127.0.0.1"
|
Master = "127.0.0.1"
|
||||||
ListenPort = 62031
|
ListenPort = 62031
|
||||||
|
# '' = all IPv4, '::' = all IPv4 and IPv6 (Dual Stack)
|
||||||
|
ListenIP = ''
|
||||||
DestportStart = 54000
|
DestportStart = 54000
|
||||||
DestPortEnd = 54100
|
DestPortEnd = 54100
|
||||||
Timeout = 30
|
Timeout = 30
|
||||||
Stats = True
|
Stats = False
|
||||||
Debug = False
|
Debug = False
|
||||||
BlackList = [1234567]
|
BlackList = [1234567]
|
||||||
|
|
||||||
@ -173,8 +193,12 @@ if __name__ == '__main__':
|
|||||||
for port in range(DestportStart,DestPortEnd+1,1):
|
for port in range(DestportStart,DestPortEnd+1,1):
|
||||||
CONNTRACK[port] = False
|
CONNTRACK[port] = False
|
||||||
|
|
||||||
|
#If we are listening IPv6 and Master is an IPv4 IPv4Address
|
||||||
|
#IPv6ify the address.
|
||||||
|
if ListenIP == '::' and IsIPv4Address(Master):
|
||||||
|
Master = '::ffff:' + Master
|
||||||
|
|
||||||
reactor.listenUDP(ListenPort,Proxy(Master,ListenPort,CONNTRACK,BlackList,Timeout,Debug,DestportStart,DestPortEnd))
|
reactor.listenUDP(ListenPort,Proxy(Master,ListenPort,CONNTRACK,BlackList,Timeout,Debug,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))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user