concatinate socket ip output in py3

This commit is contained in:
Muzahid 2021-06-11 19:51:50 +05:30
parent cc367f7c04
commit 28fb6cf743
Signed by untrusted user: cis-muzahid
GPG Key ID: 1DC85E7D3AB613EA
2 changed files with 51 additions and 14 deletions

View File

@ -126,17 +126,51 @@ def isBitSetWithinBitfield(fourByteString, n):
def encodeHost(host):
"""Encode a given host to be used in low-level socket operations"""
if host.find('.onion') > -1:
return '\xfd\x87\xd8\x7e\xeb\x43' + base64.b32decode(
host.split(".")[0], True)
elif host.find(':') == -1:
if sys.version_info[0] == 3:
return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \
str(socket.inet_aton(host))
else:
return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \
if sys.version_info[0] == 3:
if host.find('.onion') > -1:
print("================================in if condition")
return '\xfd\x87\xd8\x7e\xeb\x43' + base64.b32decode(
host.split(".")[0], True)
elif host.find(':') == -1:
print("================================in else condition")
print("_______________________")
print("_______________________")
print("_______________________")
print("_______________________")
print("_______________________")
print(socket.inet_aton(host))
print(type(socket.inet_aton(host)))
soo = socket.inet_aton(host)
# print(base64.decodebytes(bytes(soo, 'utf-8')).decode('utf-8'))
# print(base64.b64decode(soo))
# print('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF'.encode('utf-8'))
# c = b"".join(['\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF', socket.inet_aton(host)])
# print(c)
# print(soo.decode('hex'))
print(host)
print(bytes.fromhex('01020304'))
rr = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + socket.inet_aton(host)
print(rr.decode('utf-8'))
print("_______________________")
print("_______________________")
print("_______________________")
return 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)
return socket.inet_pton(socket.AF_INET6, host)
else:
if host.find('.onion') > -1:
return '\xfd\x87\xd8\x7e\xeb\x43' + base64.b32decode(
host.split(".")[0], True)
elif host.find(':') == -1:
if sys.version_info[0] == 3:
return '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF' + \
str(socket.inet_aton(host))
else:
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):
@ -154,6 +188,7 @@ def network_group(host):
GetGroup() in src/netaddresses.cpp in bitcoin core"""
if not isinstance(host, str):
return None
# import pdb; pdb.set_trace()
network_type = networkType(host)
try:
raw_host = encodeHost(host)

View File

@ -19,10 +19,12 @@ class TestNetworkGroup(unittest.TestCase):
test_ip = '1.2.3.4'
print("network_group(test_ip)")
print(network_group(test_ip))
if isinstance(network_group(test_ip), bytes):
self.assertEqual('\x01\x02', network_group(test_ip).decode('utf-8'))
else:
self.assertEqual('\x01\x02', network_group(test_ip))
print("network_group(test_ip)")
# if isinstance(network_group(test_ip), bytes):
# self.assertEqual('\x01\x02', network_group(test_ip).decode('utf-8'))
# else:
# self.assertEqual('\x01\x02', network_group(test_ip))
self.assertEqual('\x01\x02', network_group(test_ip))
test_ip = '127.0.0.1'
self.assertEqual('IPv4', network_group(test_ip))