File tree Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Original file line number Diff line number Diff line change @@ -147,25 +147,27 @@ def lookup_asn(net, ip):
147
147
return None
148
148
149
149
# Based on Greg Maxwell's seed_filter.py
150
- def filterbyasn (ips , max_per_asn , max_total ):
150
+ def filterbyasn (ips , max_per_asn , max_per_net ):
151
151
# Sift out ips by type
152
152
ips_ipv46 = [ip for ip in ips if ip ['net' ] in ['ipv4' , 'ipv6' ]]
153
153
ips_onion = [ip for ip in ips if ip ['net' ] == 'onion' ]
154
154
155
- # Filter IPv46 by ASN
155
+ # Filter IPv46 by ASN, and limit to max_per_net per network
156
156
result = []
157
+ net_count = collections .defaultdict (int )
157
158
asn_count = collections .defaultdict (int )
158
159
for ip in ips_ipv46 :
159
- if len ( result ) == max_total :
160
- break
160
+ if net_count [ ip [ 'net' ]] == max_per_net :
161
+ continue
161
162
asn = lookup_asn (ip ['net' ], ip ['ip' ])
162
163
if asn is None or asn_count [asn ] == max_per_asn :
163
164
continue
164
165
asn_count [asn ] += 1
166
+ net_count [ip ['net' ]] += 1
165
167
result .append (ip )
166
168
167
- # Add back Onions
168
- result .extend (ips_onion )
169
+ # Add back Onions (up to max_per_net)
170
+ result .extend (ips_onion [ 0 : max_per_net ] )
169
171
return result
170
172
171
173
def ip_stats (ips ):
You can’t perform that action at this time.
0 commit comments