- if m.group(1) in ['::']: # Not interested in localhost
+ if m.group(1) in ['::']: # Not interested in localhost
return None
ipstr = m.group(1)
- sortkey = ipstr # XXX parse IPv6 into number, could use name_to_ipv6 from generate-seeds
+ sortkey = ipstr # XXX parse IPv6 into number, could use name_to_ipv6 from generate-seeds
port = int(m.group(2))
else:
# Do IPv4 sanity check
ip = 0
- for i in range(0,4):
- if int(m.group(i+2)) < 0 or int(m.group(i+2)) > 255:
+ for i in range(0, 4):
+ if int(m.group(i + 2)) < 0 or int(m.group(i + 2)) > 255:
return None
- ip = ip + (int(m.group(i+2)) << (8*(3-i)))
+ ip = ip + (int(m.group(i + 2)) << (8 * (3 - i)))
if ip == 0:
return None
net = 'ipv4'
@@ -99,14 +103,17 @@
'sortkey': sortkey,
}
+
def filtermultiport(ips):
'''Filter out hosts with more nodes per IP'''
hist = collections.defaultdict(list)
for ip in ips:
hist[ip['sortkey']].append(ip)
- return [value[0] for (key,value) in list(hist.items()) if len(value)==1]
+ return [value[0] for (key, value) in list(hist.items()) if len(value) == 1]
# Based on Greg Maxwell's seed_filter.py
+
+
def filterbyasn(ips, max_per_asn, max_total):
# Sift out ips by type
ips_ipv4 = [ip for ip in ips if ip['net'] == 'ipv4']
@@ -120,7 +127,8 @@
if len(result) == max_total:
break
try:
- 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])
+ 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])
if asn not in asn_count:
asn_count[asn] = 0
if asn_count[asn] == max_per_asn:
@@ -128,7 +136,8 @@
asn_count[asn] += 1
result.append(ip)
except:
- sys.stderr.write('ERR: Could not resolve ASN for "' + ip['ip'] + '"\n')
+ sys.stderr.write(
+ 'ERR: Could not resolve ASN for "' + ip['ip'] + '"\n')
# TODO: filter IPv6 by ASN
@@ -137,6 +146,7 @@
result.extend(ips_onion)
return result
+
def main():
lines = sys.stdin.readlines()
ips = [parseline(line) for line in lines]
@@ -154,7 +164,8 @@
# Require a known and recent user agent.
ips = [ip for ip in ips if PATTERN_AGENT.match(ip['agent'])]
# Sort by availability (and use last success as tie breaker)