Skip to content

Commit 316b8b2

Browse files
committed
Filter IPv6 by ASN
1 parent 0c9de67 commit 316b8b2

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

contrib/seeds/makeseeds.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,30 @@ def filtermultiport(ips):
109109
# Based on Greg Maxwell's seed_filter.py
110110
def filterbyasn(ips, max_per_asn, max_total):
111111
# Sift out ips by type
112-
ips_ipv4 = [ip for ip in ips if ip['net'] == 'ipv4']
113-
ips_ipv6 = [ip for ip in ips if ip['net'] == 'ipv6']
112+
ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']]
114113
ips_onion = [ip for ip in ips if ip['net'] == 'onion']
115114

116-
# Filter IPv4 by ASN
115+
# Filter IPv46 by ASN
117116
result = []
118117
asn_count = {}
119-
for ip in ips_ipv4:
118+
for ip in ips_ipv46:
120119
if len(result) == max_total:
121120
break
122121
try:
123-
asn = int([x.to_text() for x in dns.resolver.query('.'.join(reversed(ip['ip'].split('.'))) + '.origin.asn.cymru.com', 'TXT').response.answer][0].split('\"')[1].split(' ')[0])
122+
if ip['net'] == 'ipv4':
123+
ipaddr = ip['ip']
124+
prefix = '.origin'
125+
else: # http://www.team-cymru.com/IP-ASN-mapping.html
126+
res = str() # 2001:4860:b002:23::68
127+
for nb in ip['ip'].split(':')[:4]: # pick the first 4 nibbles
128+
for c in nb.zfill(4): # right padded with '0'
129+
res += c + '.' # 2001 4860 b002 0023
130+
ipaddr = res.rstrip('.') # 2.0.0.1.4.8.6.0.b.0.0.2.0.0.2.3
131+
prefix = '.origin6'
132+
133+
asn = int([x.to_text() for x in dns.resolver.query('.'.join(
134+
reversed(ipaddr.split('.'))) + prefix + '.asn.cymru.com',
135+
'TXT').response.answer][0].split('\"')[1].split(' ')[0])
124136
if asn not in asn_count:
125137
asn_count[asn] = 0
126138
if asn_count[asn] == max_per_asn:
@@ -130,10 +142,7 @@ def filterbyasn(ips, max_per_asn, max_total):
130142
except:
131143
sys.stderr.write('ERR: Could not resolve ASN for "' + ip['ip'] + '"\n')
132144

133-
# TODO: filter IPv6 by ASN
134-
135-
# Add back non-IPv4
136-
result.extend(ips_ipv6)
145+
# Add back Onions
137146
result.extend(ips_onion)
138147
return result
139148

0 commit comments

Comments
 (0)