@@ -109,18 +109,30 @@ def filtermultiport(ips):
109
109
# Based on Greg Maxwell's seed_filter.py
110
110
def filterbyasn (ips , max_per_asn , max_total ):
111
111
# 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' ]]
114
113
ips_onion = [ip for ip in ips if ip ['net' ] == 'onion' ]
115
114
116
- # Filter IPv4 by ASN
115
+ # Filter IPv46 by ASN
117
116
result = []
118
117
asn_count = {}
119
- for ip in ips_ipv4 :
118
+ for ip in ips_ipv46 :
120
119
if len (result ) == max_total :
121
120
break
122
121
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 ])
124
136
if asn not in asn_count :
125
137
asn_count [asn ] = 0
126
138
if asn_count [asn ] == max_per_asn :
@@ -130,10 +142,7 @@ def filterbyasn(ips, max_per_asn, max_total):
130
142
except :
131
143
sys .stderr .write ('ERR: Could not resolve ASN for "' + ip ['ip' ] + '"\n ' )
132
144
133
- # TODO: filter IPv6 by ASN
134
-
135
- # Add back non-IPv4
136
- result .extend (ips_ipv6 )
145
+ # Add back Onions
137
146
result .extend (ips_onion )
138
147
return result
139
148
0 commit comments