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):
147147 return None
148148
149149# 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 ):
151151 # Sift out ips by type
152152 ips_ipv46 = [ip for ip in ips if ip ['net' ] in ['ipv4' , 'ipv6' ]]
153153 ips_onion = [ip for ip in ips if ip ['net' ] == 'onion' ]
154154
155- # Filter IPv46 by ASN
155+ # Filter IPv46 by ASN, and limit to max_per_net per network
156156 result = []
157+ net_count = collections .defaultdict (int )
157158 asn_count = collections .defaultdict (int )
158159 for ip in ips_ipv46 :
159- if len ( result ) == max_total :
160- break
160+ if net_count [ ip [ 'net' ]] == max_per_net :
161+ continue
161162 asn = lookup_asn (ip ['net' ], ip ['ip' ])
162163 if asn is None or asn_count [asn ] == max_per_asn :
163164 continue
164165 asn_count [asn ] += 1
166+ net_count [ip ['net' ]] += 1
165167 result .append (ip )
166168
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 ] )
169171 return result
170172
171173def ip_stats (ips ):
You can’t perform that action at this time.
0 commit comments