Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

makeseeds.py 4.8KB

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