formatted, refactored & cleaned code

This commit is contained in:
cis-kuldeep 2021-07-28 18:32:27 +05:30
parent 4a5645d9aa
commit 078c183ae9
No known key found for this signature in database
GPG Key ID: 67B47D8A06FA45E4

View File

@ -9,6 +9,7 @@ import hashlib
import random import random
import socket import socket
import sys import sys
import six
import time import time
from binascii import hexlify from binascii import hexlify
from struct import Struct, pack, unpack from struct import Struct, pack, unpack
@ -36,8 +37,6 @@ except ImportError:
from .helper_sql import sqlExecute from .helper_sql import sqlExecute
from .version import softwareVersion from .version import softwareVersion
PY3 = sys.version_info[0] >= 3
# Service flags # Service flags
#: This is a normal network node #: This is a normal network node
NODE_NETWORK = 1 NODE_NETWORK = 1
@ -107,27 +106,21 @@ def isBitSetWithinBitfield(fourByteString, n):
# ip addresses # ip addresses
if PY3: def encodeHost(host):
def encodeHost(host): """Encode a given host to be used in low-level socket operations"""
"""Encode a given host to be used in low-level socket operations""" if six.PY2:
if host.find('.onion') > -1: ONION_PREFIX = '\xfd\x87\xd8\x7e\xeb\x43'
return b'\xfd\x87\xd8\x7e\xeb\x43' + base64.b32decode( IP_PREFIX = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'
host.split(".")[0], True) else:
elif host.find(':') == -1: ONION_PREFIX = b'\xfd\x87\xd8\x7e\xeb\x43'
return b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \ IP_PREFIX = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'
socket.inet_aton(host)
return socket.inet_pton(socket.AF_INET6, host)
else: if host.find('.onion') > -1:
def encodeHost(host): return ONION_PREFIX + base64.b32decode(
"""Encode a given host to be used in low-level socket operations""" host.split(".")[0], True)
if host.find('.onion') > -1: elif host.find(':') == -1:
return '\xfd\x87\xd8\x7e\xeb\x43' + base64.b32decode( return IP_PREFIX + socket.inet_aton(host)
host.split(".")[0], True) return socket.inet_pton(socket.AF_INET6, host)
elif host.find(':') == -1:
return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \
socket.inet_aton(host)
return socket.inet_pton(socket.AF_INET6, host)
def networkType(host): def networkType(host):
@ -167,116 +160,79 @@ def network_group(host):
return network_type return network_type
if PY3: def checkIPAddress(host, private=False):
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 six.PY3:
if host[0:12] == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF': IP_PREFIX1 = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:]) IP_PREFIX2 = b'\xfd\x87\xd8\x7e\xeb\x43'
return checkIPv4Address(host[12:], hostStandardFormat, private) else:
elif host[0:6] == b'\xfd\x87\xd8\x7e\xeb\x43': IP_PREFIX1 = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'
# Onion, based on BMD/bitcoind IP_PREFIX2 = '\xfd\x87\xd8\x7e\xeb\x43'
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] == IP_PREFIX1:
def checkIPAddress(host, private=False): hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:])
""" return checkIPv4Address(host[12:], hostStandardFormat, private)
Returns hostStandardFormat if it is a valid IP address, elif host[0:6] == IP_PREFIX2:
otherwise returns False # Onion, based on BMD/bitcoind
""" hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion"
if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF': if private:
hostStandardFormat = socket.inet_ntop(socket.AF_INET, host[12:]) return False
return checkIPv4Address(host[12:], hostStandardFormat, private) return hostStandardFormat
elif host[0:6] == '\xfd\x87\xd8\x7e\xeb\x43': else:
# Onion, based on BMD/bitcoind try:
hostStandardFormat = base64.b32encode(host[6:]).lower() + ".onion" hostStandardFormat = socket.inet_ntop(socket.AF_INET6, host)
if private: except ValueError:
return False return False
return hostStandardFormat if hostStandardFormat == "":
else: # This can happen on Windows systems which are
try: # not 64-bit compatible so let us drop the IPv6 address.
hostStandardFormat = socket.inet_ntop(socket.AF_INET6, host) return False
except ValueError: return checkIPv6Address(host, hostStandardFormat, private)
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)
if PY3: def checkIPv4Address(host, hostStandardFormat, private=False):
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 six.PY2:
if host[0] == 127: # 127/8 IP_OCT1 = '\x7F' # 127/8
if not private: IP_OCT2 = '\x0A' # 10/8
logger.debug( IP_OCT3 = '\xC0\xA8' # 192.168/16
'Ignoring IP address in loopback range: %s', IP_OCT4 = '\xAC\x10' # 172.16
hostStandardFormat) IP_OCT5 = '\xAC\x20' # 12
return hostStandardFormat if private else False else:
if host[0] == 10: # 10/8 IP_OCT1 = 127 # 127/8
if not private: IP_OCT2 = 10 # 10/8
logger.debug( IP_OCT3 = b'\xC0\xA8' # 192.168/16
'Ignoring IP address in private range: %s', hostStandardFormat) IP_OCT4 = b'\xAC\x10' # 172.16
return hostStandardFormat if private else False IP_OCT5 = b'\xAC\x20' # 12
if host[0:2] == b'\xC0\xA8': # 192.168/16
if not private: if host[0] == IP_OCT1:
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 loopback range: %s',
if host[0:2] >= b'\xAC\x10' and host[0:2] < '\xAC\x20': # 172.16/12 hostStandardFormat)
if not private: return hostStandardFormat if private else False
logger.debug( if host[0] == IP_OCT2:
'Ignoring IP address in private range: %s', hostStandardFormat) if not private:
return hostStandardFormat if private else False logger.debug(
return False if private else hostStandardFormat 'Ignoring IP address in private range: %s', hostStandardFormat)
return hostStandardFormat if private else False
else: if host[0:2] == IP_OCT3:
def checkIPv4Address(host, hostStandardFormat, private=False): if not private:
""" logger.debug(
Returns hostStandardFormat if it is an IPv4 address, 'Ignoring IP address in private range: %s', hostStandardFormat)
otherwise returns False return hostStandardFormat if private else False
""" if host[0:2] >= IP_OCT4 and host[0:2] < IP_OCT5:
if host[0] == '\x7F': # 127/8 if not private:
if not private: logger.debug(
logger.debug( 'Ignoring IP address in private range: %s', hostStandardFormat)
'Ignoring IP address in loopback range: %s', return hostStandardFormat if private else False
hostStandardFormat) return False if private else 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):