2013-01-30 04:17:56 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2014-12-23 17:43:32 +01:00
|
|
|
# Generate seeds.txt from Pieter's DNS seeder
|
2013-01-30 04:17:56 +01:00
|
|
|
#
|
|
|
|
|
2014-12-23 17:43:32 +01:00
|
|
|
NSEEDS=512
|
|
|
|
|
|
|
|
MAX_SEEDS_PER_ASN=2
|
|
|
|
|
2024-02-05 23:00:22 +01:00
|
|
|
MIN_BLOCKS = 46800
|
2014-12-23 17:43:32 +01:00
|
|
|
|
|
|
|
# These are hosts that have been observed to be behaving strangely (e.g.
|
|
|
|
# aggressively connecting to every node).
|
|
|
|
SUSPICIOUS_HOSTS = set([
|
2024-03-08 20:45:05 +01:00
|
|
|
""
|
2014-12-23 17:43:32 +01:00
|
|
|
])
|
2013-01-30 04:17:56 +01:00
|
|
|
|
|
|
|
import re
|
|
|
|
import sys
|
2014-12-23 17:43:32 +01:00
|
|
|
import dns.resolver
|
2015-06-25 07:53:15 +02:00
|
|
|
import collections
|
2014-12-23 17:43:32 +01:00
|
|
|
|
2015-06-25 07:53:15 +02:00
|
|
|
PATTERN_IPV4 = re.compile(r"^((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})):(\d+)$")
|
|
|
|
PATTERN_IPV6 = re.compile(r"^\[([0-9a-z:]+)\]:(\d+)$")
|
|
|
|
PATTERN_ONION = re.compile(r"^([abcdefghijklmnopqrstuvwxyz234567]{16}\.onion):(\d+)$")
|
2016-02-02 16:28:56 +01:00
|
|
|
PATTERN_AGENT = re.compile(r"^(\/Satoshi:0\.8\.6\/|\/Satoshi:0\.9\.(2|3|4|5)\/|\/Core:0.1(0|1|2).\d{1,2}.\d{1,2}\/)$")
|
2014-12-23 17:43:32 +01:00
|
|
|
|
|
|
|
def parseline(line):
|
|
|
|
sline = line.split()
|
|
|
|
if len(sline) < 11:
|
|
|
|
return None
|
|
|
|
m = PATTERN_IPV4.match(sline[0])
|
2015-06-23 21:31:47 +02:00
|
|
|
sortkey = None
|
|
|
|
ip = None
|
2014-12-23 17:43:32 +01:00
|
|
|
if m is None:
|
2015-06-23 21:31:47 +02:00
|
|
|
m = PATTERN_IPV6.match(sline[0])
|
|
|
|
if m is None:
|
|
|
|
m = PATTERN_ONION.match(sline[0])
|
|
|
|
if m is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
net = 'onion'
|
2015-06-25 07:53:15 +02:00
|
|
|
ipstr = sortkey = m.group(1)
|
|
|
|
port = int(m.group(2))
|
2015-06-23 21:31:47 +02:00
|
|
|
else:
|
|
|
|
net = 'ipv6'
|
|
|
|
if m.group(1) in ['::']: # Not interested in localhost
|
|
|
|
return None
|
2015-06-25 07:53:15 +02:00
|
|
|
ipstr = m.group(1)
|
|
|
|
sortkey = ipstr # XXX parse IPv6 into number, could use name_to_ipv6 from generate-seeds
|
|
|
|
port = int(m.group(2))
|
2015-06-23 21:31:47 +02:00
|
|
|
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:
|
|
|
|
return None
|
|
|
|
ip = ip + (int(m.group(i+2)) << (8*(3-i)))
|
|
|
|
if ip == 0:
|
2014-12-23 17:43:32 +01:00
|
|
|
return None
|
2015-06-23 21:31:47 +02:00
|
|
|
net = 'ipv4'
|
|
|
|
sortkey = ip
|
2015-06-25 07:53:15 +02:00
|
|
|
ipstr = m.group(1)
|
|
|
|
port = int(m.group(6))
|
2014-12-23 17:43:32 +01:00
|
|
|
# Skip bad results.
|
|
|
|
if sline[1] == 0:
|
|
|
|
return None
|
|
|
|
# Extract uptime %.
|
|
|
|
uptime30 = float(sline[7][:-1])
|
|
|
|
# Extract Unix timestamp of last success.
|
|
|
|
lastsuccess = int(sline[2])
|
|
|
|
# Extract protocol version.
|
|
|
|
version = int(sline[10])
|
|
|
|
# Extract user agent.
|
|
|
|
agent = sline[11][1:-1]
|
|
|
|
# Extract service flags.
|
|
|
|
service = int(sline[9], 16)
|
|
|
|
# Extract blocks.
|
|
|
|
blocks = int(sline[8])
|
|
|
|
# Construct result.
|
|
|
|
return {
|
2015-06-23 21:31:47 +02:00
|
|
|
'net': net,
|
2015-06-25 07:53:15 +02:00
|
|
|
'ip': ipstr,
|
|
|
|
'port': port,
|
2014-12-23 17:43:32 +01:00
|
|
|
'ipnum': ip,
|
|
|
|
'uptime': uptime30,
|
|
|
|
'lastsuccess': lastsuccess,
|
|
|
|
'version': version,
|
|
|
|
'agent': agent,
|
|
|
|
'service': service,
|
|
|
|
'blocks': blocks,
|
2015-06-23 21:31:47 +02:00
|
|
|
'sortkey': sortkey,
|
2014-12-23 17:43:32 +01:00
|
|
|
}
|
|
|
|
|
2015-06-25 07:53:15 +02:00
|
|
|
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 hist.items() if len(value)==1]
|
|
|
|
|
2014-12-23 17:43:32 +01:00
|
|
|
# Based on Greg Maxwell's seed_filter.py
|
|
|
|
def filterbyasn(ips, max_per_asn, max_total):
|
2015-06-23 21:31:47 +02:00
|
|
|
# Sift out ips by type
|
|
|
|
ips_ipv4 = [ip for ip in ips if ip['net'] == 'ipv4']
|
|
|
|
ips_ipv6 = [ip for ip in ips if ip['net'] == 'ipv6']
|
|
|
|
ips_onion = [ip for ip in ips if ip['net'] == 'onion']
|
|
|
|
|
|
|
|
# Filter IPv4 by ASN
|
2014-12-23 17:43:32 +01:00
|
|
|
result = []
|
|
|
|
asn_count = {}
|
2015-06-23 21:31:47 +02:00
|
|
|
for ip in ips_ipv4:
|
2014-12-23 17:43:32 +01:00
|
|
|
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])
|
|
|
|
if asn not in asn_count:
|
|
|
|
asn_count[asn] = 0
|
|
|
|
if asn_count[asn] == max_per_asn:
|
|
|
|
continue
|
|
|
|
asn_count[asn] += 1
|
|
|
|
result.append(ip)
|
|
|
|
except:
|
|
|
|
sys.stderr.write('ERR: Could not resolve ASN for "' + ip['ip'] + '"\n')
|
2015-06-23 21:31:47 +02:00
|
|
|
|
|
|
|
# TODO: filter IPv6 by ASN
|
|
|
|
|
|
|
|
# Add back non-IPv4
|
|
|
|
result.extend(ips_ipv6)
|
|
|
|
result.extend(ips_onion)
|
2014-12-23 17:43:32 +01:00
|
|
|
return result
|
2013-01-30 04:17:56 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
lines = sys.stdin.readlines()
|
2014-12-23 17:43:32 +01:00
|
|
|
ips = [parseline(line) for line in lines]
|
|
|
|
|
2015-06-23 21:31:47 +02:00
|
|
|
# Skip entries with valid address.
|
2014-12-23 17:43:32 +01:00
|
|
|
ips = [ip for ip in ips if ip is not None]
|
|
|
|
# Skip entries from suspicious hosts.
|
|
|
|
ips = [ip for ip in ips if ip['ip'] not in SUSPICIOUS_HOSTS]
|
|
|
|
# Enforce minimal number of blocks.
|
|
|
|
ips = [ip for ip in ips if ip['blocks'] >= MIN_BLOCKS]
|
|
|
|
# Require service bit 1.
|
|
|
|
ips = [ip for ip in ips if (ip['service'] & 1) == 1]
|
|
|
|
# Require at least 50% 30-day uptime.
|
|
|
|
ips = [ip for ip in ips if ip['uptime'] > 50]
|
|
|
|
# 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)
|
|
|
|
ips.sort(key=lambda x: (x['uptime'], x['lastsuccess'], x['ip']), reverse=True)
|
2016-03-06 16:26:01 +01:00
|
|
|
# Filter out hosts with multiple ports, these are likely abusive
|
2015-06-25 07:53:15 +02:00
|
|
|
ips = filtermultiport(ips)
|
2014-12-23 17:43:32 +01:00
|
|
|
# Look up ASNs and limit results, both per ASN and globally.
|
|
|
|
ips = filterbyasn(ips, MAX_SEEDS_PER_ASN, NSEEDS)
|
|
|
|
# Sort the results by IP address (for deterministic output).
|
2015-06-23 21:31:47 +02:00
|
|
|
ips.sort(key=lambda x: (x['net'], x['sortkey']))
|
2013-01-30 04:17:56 +01:00
|
|
|
|
2014-12-23 17:43:32 +01:00
|
|
|
for ip in ips:
|
2015-06-25 07:53:15 +02:00
|
|
|
if ip['net'] == 'ipv6':
|
|
|
|
print '[%s]:%i' % (ip['ip'], ip['port'])
|
|
|
|
else:
|
|
|
|
print '%s:%i' % (ip['ip'], ip['port'])
|
2013-01-30 04:17:56 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|