Fixed errors & Py2/3 compatibility

This commit is contained in:
cis-kuldeep 2021-07-20 16:44:43 +05:30
parent c41cff67ac
commit bf6426376e
No known key found for this signature in database
GPG Key ID: 67B47D8A06FA45E4
2 changed files with 89 additions and 45 deletions

View File

@ -114,10 +114,9 @@ def encodeHost(host):
return '\xfd\x87\xd8\x7e\xeb\x43' + base64.b32decode( return '\xfd\x87\xd8\x7e\xeb\x43' + base64.b32decode(
host.split(".")[0], True) host.split(".")[0], True)
elif host.find(':') == -1: elif host.find(':') == -1:
import pdb; pdb.set_trace()
if PY3: if PY3:
return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \ return b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \
socket.inet_aton(host).decode('utf-8') socket.inet_aton(host)
else: else:
return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \ return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \
socket.inet_aton(host) socket.inet_aton(host)
@ -166,6 +165,27 @@ def checkIPAddress(host, private=False):
Returns hostStandardFormat if it is a valid IP address, Returns hostStandardFormat if it is a valid IP address,
otherwise returns False otherwise returns False
""" """
if not isinstance(host, str):
if host[0:12] == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:])
return checkIPv4Address(host[12:], hostStandardFormat, private)
elif host[0:6] == b'\xfd\x87\xd8\x7e\xeb\x43':
# Onion, based on BMD/bitcoind
hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion"
if private:
return False
return hostStandardFormat
else:
try:
hostStandardFormat = socket.inet_ntop(socket.AF_INET6, host)
except ValueError:
return False
if hostStandardFormat == "":
# This can happen on Windows systems which are
# not 64-bit compatible so let us drop the IPv6 address.
return False
return checkIPv6Address(host, hostStandardFormat, private)
else:
if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF': if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:]) hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:])
return checkIPv4Address(host[12:], hostStandardFormat, private) return checkIPv4Address(host[12:], hostStandardFormat, private)
@ -192,6 +212,30 @@ def checkIPv4Address(host, hostStandardFormat, private=False):
Returns hostStandardFormat if it is an IPv4 address, Returns hostStandardFormat if it is an IPv4 address,
otherwise returns False otherwise returns False
""" """
if not isinstance(host, str):
if host[0] == 127: # 127/8
if not private:
logger.debug(
'Ignoring IP address in loopback range: %s',
hostStandardFormat)
return hostStandardFormat if private else False
if host[0] == 10: # 10/8
if not private:
logger.debug(
'Ignoring IP address in private range: %s', hostStandardFormat)
return hostStandardFormat if private else False
if host[0:2] == b'\xC0\xA8': # 192.168/16
if not private:
logger.debug(
'Ignoring IP address in private range: %s', hostStandardFormat)
return hostStandardFormat if private else False
if host[0:2] >= b'\xAC\x10' and host[0:2] < '\xAC\x20': # 172.16/12
if not private:
logger.debug(
'Ignoring IP address in private range: %s', hostStandardFormat)
return hostStandardFormat if private else False
return False if private else hostStandardFormat
else:
if host[0] == '\x7F': # 127/8 if host[0] == '\x7F': # 127/8
if not private: if not private:
logger.debug( logger.debug(

View File

@ -14,9 +14,9 @@ class TestProtocol(unittest.TestCase):
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
self.assertTrue( self.assertTrue(
protocol.checkIPAddress(protocol.encodeHost('127.0.0.1').decode('ISO-8859-1'), True)) protocol.checkIPAddress(protocol.encodeHost('127.0.0.1'), True))
self.assertTrue( self.assertTrue(
protocol.checkIPAddress(protocol.encodeHost('192.168.0.1').decode('ISO-8859-1'), True)) protocol.checkIPAddress(protocol.encodeHost('192.168.0.1'), True))
self.assertTrue( self.assertTrue(
not protocol.checkSocksIP('127.0.0.1') not protocol.checkSocksIP('127.0.0.1')