Merge #15840: Contrib scripts: Filter IPv6 by ASN

316b8b2339efa131fc39f050ee0c9fe5291572b7 Filter IPv6 by ASN (Emil)

Pull request description:

  Improves IP diversity for hardcoded seednodes.

ACKs for commit 316b8b:

Tree-SHA512: ae90427efa317d59125457bf8bfd077fd115c0921e1cc13cebd855206498546a026ccc18f039d1963d64d9be9497c41f4a21214fb565d5d11a9635ad12836421
This commit is contained in:
Wladimir J. van der Laan 2019-05-20 17:53:08 +02:00 committed by Munkybooty
parent d21e550b0e
commit cf234c7894

View File

@ -91,14 +91,23 @@ def filtermultipayoutaddress(mns):
return [mn for mn in mns if len(hist[mn['state']['payoutAddress']]) == 1]
def resolveasn(resolver, ip):
asn = int([x.to_text() for x in resolver.resolve('.'.join(reversed(ip.split('.'))) + '.origin.asn.cymru.com', 'TXT').response.answer][0].split('\"')[1].split(' ')[0])
if ip['net'] == 'ipv4':
ipaddr = ip['ip']
prefix = '.origin'
else: # http://www.team-cymru.com/IP-ASN-mapping.html
res = str() # 2001:4860:b002:23::68
for nb in ip['ip'].split(':')[:4]: # pick the first 4 nibbles
for c in nb.zfill(4): # right padded with '0'
res += c + '.' # 2001 4860 b002 0023
ipaddr = res.rstrip('.') # 2.0.0.1.4.8.6.0.b.0.0.2.0.0.2.3
prefix = '.origin6'
asn = int([x.to_text() for x in resolver.resolve('.'.join(reversed(ipaddr.split('.'))) + prefix + '.asn.cymru.com', 'TXT').response.answer][0].split('\"')[1].split(' ')[0])
return asn
# Based on Greg Maxwell's seed_filter.py
def filterbyasn(ips, max_per_asn, max_total):
# Sift out ips by type
ips_ipv4 = [ip for ip in ips if ip['net'] == 'ipv4']
ips_ipv6 = [ip for ip in ips if ip['net'] == 'ipv6']
ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']]
ips_onion = [ip for ip in ips if ip['net'] == 'onion']
my_resolver = dns.resolver.Resolver()
@ -109,13 +118,12 @@ def filterbyasn(ips, max_per_asn, max_total):
my_resolver.nameservers = ['208.67.222.222', '208.67.220.220']
# Resolve ASNs in parallel
asns = [pool.apply_async(resolveasn, args=(my_resolver, ip['ip'])) for ip in ips_ipv4]
asns = [pool.apply_async(resolveasn, args=(my_resolver, ip)) for ip in ips_ipv46]
# Filter IPv4 by ASN
# Filter IPv46 by ASN
result = []
asn_count = {}
for i in range(len(ips_ipv4)):
ip = ips_ipv4[i]
for i, ip in enumerate(ips_ipv46):
if len(result) == max_total:
break
try:
@ -129,10 +137,7 @@ def filterbyasn(ips, max_per_asn, max_total):
except:
sys.stderr.write('ERR: Could not resolve ASN for "' + ip['ip'] + '"\n')
# TODO: filter IPv6 by ASN
# Add back non-IPv4
result.extend(ips_ipv6)
# Add back Onions
result.extend(ips_onion)
return result