Changeset View
Changeset View
Standalone View
Standalone View
contrib/seeds/makeseeds.py
Show First 20 Lines • Show All 124 Lines • ▼ Show 20 Lines | for ip in 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 | # Based on Greg Maxwell's seed_filter.py | ||||
def filterbyasn(ips, max_per_asn, max_total): | def filterbyasn(ips, max_per_asn, max_total): | ||||
# Sift out ips by type | # Sift out ips by type | ||||
ips_ipv4 = [ip for ip in ips if ip['net'] == 'ipv4'] | ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']] | ||||
ips_ipv6 = [ip for ip in ips if ip['net'] == 'ipv6'] | |||||
ips_onion = [ip for ip in ips if ip['net'] == 'onion'] | ips_onion = [ip for ip in ips if ip['net'] == 'onion'] | ||||
# Filter IPv4 by ASN | # Filter IPv46 by ASN | ||||
result = [] | result = [] | ||||
asn_count = {} | asn_count = {} | ||||
for ip in ips_ipv4: | for ip in ips_ipv46: | ||||
if len(result) == max_total: | if len(result) == max_total: | ||||
break | break | ||||
try: | try: | ||||
asn = int([x.to_text() | if ip['net'] == 'ipv4': | ||||
for x in dns.resolver.query('.'.join(reversed(ip['ip'].split('.'))) + '.origin.asn.cymru.com', 'TXT').response.answer][0].split('\"')[1].split(' ')[0]) | ipaddr = ip['ip'] | ||||
prefix = '.origin' | |||||
else: | |||||
# http://www.team-cymru.com/IP-ASN-mapping.html | |||||
# 2001:4860:b002:23::68 | |||||
res = str() | |||||
# pick the first 4 nibbles | |||||
for nb in ip['ip'].split(':')[:4]: | |||||
# right padded with '0' | |||||
for c in nb.zfill(4): | |||||
# 2001 4860 b002 0023 | |||||
res += c + '.' | |||||
# 2.0.0.1.4.8.6.0.b.0.0.2.0.0.2.3 | |||||
ipaddr = res.rstrip('.') | |||||
prefix = '.origin6' | |||||
asn = int([x.to_text() for x in dns.resolver.query('.'.join( | |||||
reversed(ipaddr.split('.'))) + prefix + '.asn.cymru.com', | |||||
'TXT').response.answer][0].split('\"')[1].split(' ')[0]) | |||||
if asn not in asn_count: | if asn not in asn_count: | ||||
asn_count[asn] = 0 | asn_count[asn] = 0 | ||||
if asn_count[asn] == max_per_asn: | if asn_count[asn] == max_per_asn: | ||||
continue | continue | ||||
asn_count[asn] += 1 | asn_count[asn] += 1 | ||||
result.append(ip) | result.append(ip) | ||||
except: | except: | ||||
sys.stderr.write( | sys.stderr.write( | ||||
'ERR: Could not resolve ASN for "' + ip['ip'] + '"\n') | 'ERR: Could not resolve ASN for "' + ip['ip'] + '"\n') | ||||
# TODO: filter IPv6 by ASN | # Add back Onions | ||||
# Add back non-IPv4 | |||||
result.extend(ips_ipv6) | |||||
result.extend(ips_onion) | result.extend(ips_onion) | ||||
return result | return result | ||||
def main(): | def main(): | ||||
lines = sys.stdin.readlines() | lines = sys.stdin.readlines() | ||||
ips = [parseline(line) for line in lines] | ips = [parseline(line) for line in lines] | ||||
Show All 32 Lines |