contrib: makeseeds: Limit per network, instead of total

This commit is contained in:
Wladimir J. van der Laan 2019-10-01 11:25:58 +02:00
parent c254a9ef69
commit ed76299bea

View file

@ -147,25 +147,27 @@ def lookup_asn(net, ip):
return None return None
# 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_per_net):
# Sift out ips by type # Sift out ips by type
ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']] ips_ipv46 = [ip for ip in ips if ip['net'] in ['ipv4', 'ipv6']]
ips_onion = [ip for ip in ips if ip['net'] == 'onion'] ips_onion = [ip for ip in ips if ip['net'] == 'onion']
# Filter IPv46 by ASN # Filter IPv46 by ASN, and limit to max_per_net per network
result = [] result = []
net_count = collections.defaultdict(int)
asn_count = collections.defaultdict(int) asn_count = collections.defaultdict(int)
for ip in ips_ipv46: for ip in ips_ipv46:
if len(result) == max_total: if net_count[ip['net']] == max_per_net:
break continue
asn = lookup_asn(ip['net'], ip['ip']) asn = lookup_asn(ip['net'], ip['ip'])
if asn is None or asn_count[asn] == max_per_asn: if asn is None or asn_count[asn] == max_per_asn:
continue continue
asn_count[asn] += 1 asn_count[asn] += 1
net_count[ip['net']] += 1
result.append(ip) result.append(ip)
# Add back Onions # Add back Onions (up to max_per_net)
result.extend(ips_onion) result.extend(ips_onion[0:max_per_net])
return result return result
def ip_stats(ips): def ip_stats(ips):