Merge pull request #9 from sfer033/ipv6

Initial IPv6 support
master
Sfer 3 years ago committed by GitHub
commit 9b213f94a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -164,17 +164,22 @@ class Bucket:
continue
addr = peer['adr'].entries['addr'].entries
last_seen, m_ip, m_port = peer['last_seen'], addr['m_ip'], addr['m_port']
ipv4 = True if "m_ip" in addr else False
if not ipv4 and not "addr" in addr:
continue
# reinterpret m_ip as big endian
m_ip = c_uint32(m_ip.to_bytes(), endian='big')
if ipv4 and len(addr["m_ip"]) == 4:
m_ip, m_port = addr['m_ip'], addr['m_port']
m_ip = c_uint32(m_ip.to_bytes(), endian='big')
elif len(addr["addr"]) == 16:
m_ip, m_port = addr['addr'], addr["m_port"]
m_ip = c_uint64(m_ip, endian="big")
else:
continue
peers.append({
'last_seen': peer['last_seen'],
'ip': m_ip,
'port': m_port
})
# sort on last seen
peers = sorted(peers, key=lambda k: k['last_seen'], reverse=True)
return peers

@ -228,8 +228,12 @@ class c_uint32(_IntType):
super(c_uint32, self).__init__(value, endian)
@property
def ipv4(self):
return ipaddress.IPv4Address(self.value)
def ipv4(self) -> str:
return str(ipaddress.IPv4Address(self.value))
@property
def ip(self) -> str:
return self.ipv4
class c_int64(_IntType):
@ -255,6 +259,17 @@ class c_uint64(_IntType):
def __init__(self, value, endian='little'):
super(c_uint64, self).__init__(value, endian)
@property
def ipv6(self) -> str:
val = socket.inet_ntop(socket.AF_INET6, self.value)
if val.startswith("::ffff:"):
val = val[7:]
return val
@property
def ip(self) -> str:
return self.ipv6
@property
def date_utc(self):
return datetime.utcfromtimestamp(self.value)

@ -45,7 +45,10 @@ while 1:
peers = bucket.get_peers() or []
for peer in peers:
print('%s:%d' % (peer['ip'].ipv4, peer['port'].value))
try:
print('%s:%d' % (peer['ip'].ip, peer['port'].value))
except:
pass
sock.close()
break

Loading…
Cancel
Save