Merge #10199: Better fee estimates
[bitcoinplatinum.git] / contrib / seeds / makeseeds.py
blob34f0f576716ed1f1c5bb166477f8df3174ee59f9
1 #!/usr/bin/env python3
2 # Copyright (c) 2013-2017 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 # Generate seeds.txt from Pieter's DNS seeder
9 NSEEDS=512
11 MAX_SEEDS_PER_ASN=2
13 MIN_BLOCKS = 337600
15 # These are hosts that have been observed to be behaving strangely (e.g.
16 # aggressively connecting to every node).
17 SUSPICIOUS_HOSTS = {
18 "130.211.129.106", "178.63.107.226",
19 "83.81.130.26", "88.198.17.7", "148.251.238.178", "176.9.46.6",
20 "54.173.72.127", "54.174.10.182", "54.183.64.54", "54.194.231.211",
21 "54.66.214.167", "54.66.220.137", "54.67.33.14", "54.77.251.214",
22 "54.94.195.96", "54.94.200.247"
25 import re
26 import sys
27 import dns.resolver
28 import collections
30 PATTERN_IPV4 = re.compile(r"^((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})):(\d+)$")
31 PATTERN_IPV6 = re.compile(r"^\[([0-9a-z:]+)\]:(\d+)$")
32 PATTERN_ONION = re.compile(r"^([abcdefghijklmnopqrstuvwxyz234567]{16}\.onion):(\d+)$")
33 PATTERN_AGENT = re.compile(r"^(/Satoshi:0.12.(0|1|99)/|/Satoshi:0.13.(0|1|2|99)/)$")
35 def parseline(line):
36 sline = line.split()
37 if len(sline) < 11:
38 return None
39 m = PATTERN_IPV4.match(sline[0])
40 sortkey = None
41 ip = None
42 if m is None:
43 m = PATTERN_IPV6.match(sline[0])
44 if m is None:
45 m = PATTERN_ONION.match(sline[0])
46 if m is None:
47 return None
48 else:
49 net = 'onion'
50 ipstr = sortkey = m.group(1)
51 port = int(m.group(2))
52 else:
53 net = 'ipv6'
54 if m.group(1) in ['::']: # Not interested in localhost
55 return None
56 ipstr = m.group(1)
57 sortkey = ipstr # XXX parse IPv6 into number, could use name_to_ipv6 from generate-seeds
58 port = int(m.group(2))
59 else:
60 # Do IPv4 sanity check
61 ip = 0
62 for i in range(0,4):
63 if int(m.group(i+2)) < 0 or int(m.group(i+2)) > 255:
64 return None
65 ip = ip + (int(m.group(i+2)) << (8*(3-i)))
66 if ip == 0:
67 return None
68 net = 'ipv4'
69 sortkey = ip
70 ipstr = m.group(1)
71 port = int(m.group(6))
72 # Skip bad results.
73 if sline[1] == 0:
74 return None
75 # Extract uptime %.
76 uptime30 = float(sline[7][:-1])
77 # Extract Unix timestamp of last success.
78 lastsuccess = int(sline[2])
79 # Extract protocol version.
80 version = int(sline[10])
81 # Extract user agent.
82 agent = sline[11][1:-1]
83 # Extract service flags.
84 service = int(sline[9], 16)
85 # Extract blocks.
86 blocks = int(sline[8])
87 # Construct result.
88 return {
89 'net': net,
90 'ip': ipstr,
91 'port': port,
92 'ipnum': ip,
93 'uptime': uptime30,
94 'lastsuccess': lastsuccess,
95 'version': version,
96 'agent': agent,
97 'service': service,
98 'blocks': blocks,
99 'sortkey': sortkey,
102 def filtermultiport(ips):
103 '''Filter out hosts with more nodes per IP'''
104 hist = collections.defaultdict(list)
105 for ip in ips:
106 hist[ip['sortkey']].append(ip)
107 return [value[0] for (key,value) in list(hist.items()) if len(value)==1]
109 # Based on Greg Maxwell's seed_filter.py
110 def filterbyasn(ips, max_per_asn, max_total):
111 # Sift out ips by type
112 ips_ipv4 = [ip for ip in ips if ip['net'] == 'ipv4']
113 ips_ipv6 = [ip for ip in ips if ip['net'] == 'ipv6']
114 ips_onion = [ip for ip in ips if ip['net'] == 'onion']
116 # Filter IPv4 by ASN
117 result = []
118 asn_count = {}
119 for ip in ips_ipv4:
120 if len(result) == max_total:
121 break
122 try:
123 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])
124 if asn not in asn_count:
125 asn_count[asn] = 0
126 if asn_count[asn] == max_per_asn:
127 continue
128 asn_count[asn] += 1
129 result.append(ip)
130 except:
131 sys.stderr.write('ERR: Could not resolve ASN for "' + ip['ip'] + '"\n')
133 # TODO: filter IPv6 by ASN
135 # Add back non-IPv4
136 result.extend(ips_ipv6)
137 result.extend(ips_onion)
138 return result
140 def main():
141 lines = sys.stdin.readlines()
142 ips = [parseline(line) for line in lines]
144 # Skip entries with valid address.
145 ips = [ip for ip in ips if ip is not None]
146 # Skip entries from suspicious hosts.
147 ips = [ip for ip in ips if ip['ip'] not in SUSPICIOUS_HOSTS]
148 # Enforce minimal number of blocks.
149 ips = [ip for ip in ips if ip['blocks'] >= MIN_BLOCKS]
150 # Require service bit 1.
151 ips = [ip for ip in ips if (ip['service'] & 1) == 1]
152 # Require at least 50% 30-day uptime.
153 ips = [ip for ip in ips if ip['uptime'] > 50]
154 # Require a known and recent user agent.
155 ips = [ip for ip in ips if PATTERN_AGENT.match(ip['agent'])]
156 # Sort by availability (and use last success as tie breaker)
157 ips.sort(key=lambda x: (x['uptime'], x['lastsuccess'], x['ip']), reverse=True)
158 # Filter out hosts with multiple bitcoin ports, these are likely abusive
159 ips = filtermultiport(ips)
160 # Look up ASNs and limit results, both per ASN and globally.
161 ips = filterbyasn(ips, MAX_SEEDS_PER_ASN, NSEEDS)
162 # Sort the results by IP address (for deterministic output).
163 ips.sort(key=lambda x: (x['net'], x['sortkey']))
165 for ip in ips:
166 if ip['net'] == 'ipv6':
167 print('[%s]:%i' % (ip['ip'], ip['port']))
168 else:
169 print('%s:%i' % (ip['ip'], ip['port']))
171 if __name__ == '__main__':
172 main()