contrib: makeseeds: Factor out ASN lookup

This commit is contained in:
Wladimir J. van der Laan 2019-10-01 11:00:53 +02:00
parent 301c2b1ab5
commit 3314d87966

View file

@ -121,25 +121,18 @@ def filtermultiport(ips):
hist[ip['sortkey']].append(ip) hist[ip['sortkey']].append(ip)
return [value[0] for (key,value) in list(hist.items()) if len(value)==1] return [value[0] for (key,value) in list(hist.items()) if len(value)==1]
# Based on Greg Maxwell's seed_filter.py def lookup_asn(net, ip):
def filterbyasn(ips, max_per_asn, max_total): '''
# Sift out ips by type Look up the asn for an IP (4 or 6) address by querying cymry.com, or None
ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']] if it could not be found.
ips_onion = [ip for ip in ips if ip['net'] == 'onion'] '''
# Filter IPv46 by ASN
result = []
asn_count = {}
for ip in ips_ipv46:
if len(result) == max_total:
break
try: try:
if ip['net'] == 'ipv4': if net == 'ipv4':
ipaddr = ip['ip'] ipaddr = ip
prefix = '.origin' prefix = '.origin'
else: # http://www.team-cymru.com/IP-ASN-mapping.html else: # http://www.team-cymru.com/IP-ASN-mapping.html
res = str() # 2001:4860:b002:23::68 res = str() # 2001:4860:b002:23::68
for nb in ip['ip'].split(':')[:4]: # pick the first 4 nibbles for nb in ip.split(':')[:4]: # pick the first 4 nibbles
for c in nb.zfill(4): # right padded with '0' for c in nb.zfill(4): # right padded with '0'
res += c + '.' # 2001 4860 b002 0023 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 ipaddr = res.rstrip('.') # 2.0.0.1.4.8.6.0.b.0.0.2.0.0.2.3
@ -148,14 +141,28 @@ def filterbyasn(ips, max_per_asn, max_total):
asn = int([x.to_text() for x in dns.resolver.query('.'.join( asn = int([x.to_text() for x in dns.resolver.query('.'.join(
reversed(ipaddr.split('.'))) + prefix + '.asn.cymru.com', reversed(ipaddr.split('.'))) + prefix + '.asn.cymru.com',
'TXT').response.answer][0].split('\"')[1].split(' ')[0]) 'TXT').response.answer][0].split('\"')[1].split(' ')[0])
if asn not in asn_count: return asn
asn_count[asn] = 0 except Exception:
if asn_count[asn] == max_per_asn: sys.stderr.write('ERR: Could not resolve ASN for "' + ip + '"\n')
return None
# Based on Greg Maxwell's seed_filter.py
def filterbyasn(ips, max_per_asn, max_total):
# Sift out ips by type
ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']]
ips_onion = [ip for ip in ips if ip['net'] == 'onion']
# Filter IPv46 by ASN
result = []
asn_count = collections.defaultdict(int)
for ip in ips_ipv46:
if len(result) == max_total:
break
asn = lookup_asn(ip['net'], ip['ip'])
if asn is None or asn_count[asn] == max_per_asn:
continue continue
asn_count[asn] += 1 asn_count[asn] += 1
result.append(ip) result.append(ip)
except:
sys.stderr.write('ERR: Could not resolve ASN for "' + ip['ip'] + '"\n')
# Add back Onions # Add back Onions
result.extend(ips_onion) result.extend(ips_onion)