Fixed errors & Py2/3 compatibility
This commit is contained in:
parent
c41cff67ac
commit
bf6426376e
130
src/protocol.py
130
src/protocol.py
|
@ -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,25 +165,46 @@ 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 host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
|
if not isinstance(host, str):
|
||||||
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:])
|
if host[0:12] == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
|
||||||
return checkIPv4Address(host[12:], hostStandardFormat, private)
|
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:])
|
||||||
elif host[0:6] == '\xfd\x87\xd8\x7e\xeb\x43':
|
return checkIPv4Address(host[12:], hostStandardFormat, private)
|
||||||
# Onion, based on BMD/bitcoind
|
elif host[0:6] == b'\xfd\x87\xd8\x7e\xeb\x43':
|
||||||
hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion"
|
# Onion, based on BMD/bitcoind
|
||||||
if private:
|
hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion"
|
||||||
return False
|
if private:
|
||||||
return hostStandardFormat
|
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:
|
else:
|
||||||
try:
|
if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF':
|
||||||
hostStandardFormat = socket.inet_ntop(socket.AF_INET6, host)
|
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:])
|
||||||
except ValueError:
|
return checkIPv4Address(host[12:], hostStandardFormat, private)
|
||||||
return False
|
elif host[0:6] == '\xfd\x87\xd8\x7e\xeb\x43':
|
||||||
if hostStandardFormat == "":
|
# Onion, based on BMD/bitcoind
|
||||||
# This can happen on Windows systems which are
|
hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion"
|
||||||
# not 64-bit compatible so let us drop the IPv6 address.
|
if private:
|
||||||
return False
|
return False
|
||||||
return checkIPv6Address(host, hostStandardFormat, private)
|
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)
|
||||||
|
|
||||||
|
|
||||||
def checkIPv4Address(host, hostStandardFormat, private=False):
|
def checkIPv4Address(host, hostStandardFormat, private=False):
|
||||||
|
@ -192,28 +212,52 @@ 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 host[0] == '\x7F': # 127/8
|
if not isinstance(host, str):
|
||||||
if not private:
|
if host[0] == 127: # 127/8
|
||||||
logger.debug(
|
if not private:
|
||||||
'Ignoring IP address in loopback range: %s',
|
logger.debug(
|
||||||
hostStandardFormat)
|
'Ignoring IP address in loopback range: %s',
|
||||||
return hostStandardFormat if private else False
|
hostStandardFormat)
|
||||||
if host[0] == '\x0A': # 10/8
|
return hostStandardFormat if private else False
|
||||||
if not private:
|
if host[0] == 10: # 10/8
|
||||||
logger.debug(
|
if not private:
|
||||||
'Ignoring IP address in private range: %s', hostStandardFormat)
|
logger.debug(
|
||||||
return hostStandardFormat if private else False
|
'Ignoring IP address in private range: %s', hostStandardFormat)
|
||||||
if host[0:2] == '\xC0\xA8': # 192.168/16
|
return hostStandardFormat if private else False
|
||||||
if not private:
|
if host[0:2] == b'\xC0\xA8': # 192.168/16
|
||||||
logger.debug(
|
if not private:
|
||||||
'Ignoring IP address in private range: %s', hostStandardFormat)
|
logger.debug(
|
||||||
return hostStandardFormat if private else False
|
'Ignoring IP address in private range: %s', hostStandardFormat)
|
||||||
if host[0:2] >= '\xAC\x10' and host[0:2] < '\xAC\x20': # 172.16/12
|
return hostStandardFormat if private else False
|
||||||
if not private:
|
if host[0:2] >= b'\xAC\x10' and host[0:2] < '\xAC\x20': # 172.16/12
|
||||||
logger.debug(
|
if not private:
|
||||||
'Ignoring IP address in private range: %s', hostStandardFormat)
|
logger.debug(
|
||||||
return hostStandardFormat if private else False
|
'Ignoring IP address in private range: %s', hostStandardFormat)
|
||||||
return False if private else hostStandardFormat
|
return hostStandardFormat if private else False
|
||||||
|
return False if private else hostStandardFormat
|
||||||
|
else:
|
||||||
|
if host[0] == '\x7F': # 127/8
|
||||||
|
if not private:
|
||||||
|
logger.debug(
|
||||||
|
'Ignoring IP address in loopback range: %s',
|
||||||
|
hostStandardFormat)
|
||||||
|
return hostStandardFormat if private else False
|
||||||
|
if host[0] == '\x0A': # 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] == '\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] >= '\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
|
||||||
|
|
||||||
|
|
||||||
def checkIPv6Address(host, hostStandardFormat, private=False):
|
def checkIPv6Address(host, hostStandardFormat, private=False):
|
||||||
|
|
|
@ -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')
|
||||||
|
|
Reference in New Issue
Block a user