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.

generate-seeds.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/python
  2. # Copyright (c) 2014 Wladmir J. van der Laan
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. '''
  6. Script to generate list of seed nodes for chainparams.cpp.
  7. This script expects two text files in the directory that is passed as an
  8. argument:
  9. nodes_main.txt
  10. nodes_test.txt
  11. These files must consist of lines in the format
  12. <ip>
  13. <ip>:<port>
  14. [<ipv6>]
  15. [<ipv6>]:<port>
  16. <onion>.onion
  17. 0xDDBBCCAA (IPv4 little-endian old pnSeeds format)
  18. The output will be two data structures with the peers in binary format:
  19. static SeedSpec6 pnSeed6_main[]={
  20. ...
  21. }
  22. static SeedSpec6 pnSeed6_test[]={
  23. ...
  24. }
  25. These should be pasted into `src/chainparamsseeds.h`.
  26. '''
  27. from __future__ import print_function, division
  28. from base64 import b32decode
  29. from binascii import a2b_hex
  30. import sys, os
  31. import re
  32. # ipv4 in ipv6 prefix
  33. pchIPv4 = bytearray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff])
  34. # tor-specific ipv6 prefix
  35. pchOnionCat = bytearray([0xFD,0x87,0xD8,0x7E,0xEB,0x43])
  36. def name_to_ipv6(addr):
  37. if len(addr)>6 and addr.endswith('.onion'):
  38. vchAddr = b32decode(addr[0:-6], True)
  39. if len(vchAddr) != 16-len(pchOnionCat):
  40. raise ValueError('Invalid onion %s' % s)
  41. return pchOnionCat + vchAddr
  42. elif '.' in addr: # IPv4
  43. return pchIPv4 + bytearray((int(x) for x in addr.split('.')))
  44. elif ':' in addr: # IPv6
  45. sub = [[], []] # prefix, suffix
  46. x = 0
  47. addr = addr.split(':')
  48. for i,comp in enumerate(addr):
  49. if comp == '':
  50. if i == 0 or i == (len(addr)-1): # skip empty component at beginning or end
  51. continue
  52. x += 1 # :: skips to suffix
  53. assert(x < 2)
  54. else: # two bytes per component
  55. val = int(comp, 16)
  56. sub[x].append(val >> 8)
  57. sub[x].append(val & 0xff)
  58. nullbytes = 16 - len(sub[0]) - len(sub[1])
  59. assert((x == 0 and nullbytes == 0) or (x == 1 and nullbytes > 0))
  60. return bytearray(sub[0] + ([0] * nullbytes) + sub[1])
  61. elif addr.startswith('0x'): # IPv4-in-little-endian
  62. return pchIPv4 + bytearray(reversed(a2b_hex(addr[2:])))
  63. else:
  64. raise ValueError('Could not parse address %s' % addr)
  65. def parse_spec(s, defaultport):
  66. match = re.match('\[([0-9a-fA-F:]+)\](?::([0-9]+))?$', s)
  67. if match: # ipv6
  68. host = match.group(1)
  69. port = match.group(2)
  70. elif s.count(':') > 1: # ipv6, no port
  71. host = s
  72. port = ''
  73. else:
  74. (host,_,port) = s.partition(':')
  75. if not port:
  76. port = defaultport
  77. else:
  78. port = int(port)
  79. host = name_to_ipv6(host)
  80. return (host,port)
  81. def process_nodes(g, f, structname, defaultport):
  82. g.write('static SeedSpec6 %s[] = {\n' % structname)
  83. first = True
  84. for line in f:
  85. comment = line.find('#')
  86. if comment != -1:
  87. line = line[0:comment]
  88. line = line.strip()
  89. if not line:
  90. continue
  91. if not first:
  92. g.write(',\n')
  93. first = False
  94. (host,port) = parse_spec(line, defaultport)
  95. hoststr = ','.join(('0x%02x' % b) for b in host)
  96. g.write(' {{%s}, %i}' % (hoststr, port))
  97. g.write('\n};\n')
  98. def main():
  99. if len(sys.argv)<2:
  100. print(('Usage: %s <path_to_nodes_txt>' % sys.argv[0]), file=sys.stderr)
  101. exit(1)
  102. g = sys.stdout
  103. indir = sys.argv[1]
  104. g.write('#ifndef BITCOIN_CHAINPARAMSSEEDS_H\n')
  105. g.write('#define BITCOIN_CHAINPARAMSSEEDS_H\n')
  106. g.write('/**\n')
  107. g.write(' * List of fixed seed nodes for the bitcoin network\n')
  108. g.write(' * AUTOGENERATED by contrib/seeds/generate-seeds.py\n')
  109. g.write(' *\n')
  110. g.write(' * Each line contains a 16-byte IPv6 address and a port.\n')
  111. g.write(' * IPv4 as well as onion addresses are wrapped inside a IPv6 address accordingly.\n')
  112. g.write(' */\n')
  113. with open(os.path.join(indir,'nodes_main.txt'),'r') as f:
  114. process_nodes(g, f, 'pnSeed6_main', 8333)
  115. g.write('\n')
  116. with open(os.path.join(indir,'nodes_test.txt'),'r') as f:
  117. process_nodes(g, f, 'pnSeed6_test', 18333)
  118. g.write('#endif // BITCOIN_CHAINPARAMSSEEDS_H\n')
  119. if __name__ == '__main__':
  120. main()