You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

makeseeds.py 5.6KB

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