generate_tox_bootstrap.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/python
  2. # pip install jinja2 requests
  3. import datetime
  4. import jinja2
  5. import json
  6. import requests
  7. import socket
  8. json_url = 'https://nodes.tox.chat/json'
  9. tox_bootstrap_template = """
  10. /*
  11. * Generated with generate_tox_bootstrap.py by GDR!
  12. * from {{ json_url }} on {{ now }}
  13. */
  14. struct bootstrap_node {
  15. char *address;
  16. uint16_t port;
  17. uint8_t key[32];
  18. } bootstrap_nodes[] = {
  19. {% for node in nodes %}
  20. {
  21. "{{ node.ipv4 }}",
  22. {{ node.port }},
  23. {
  24. {{ node.public_key|toxtoc }}
  25. }
  26. },
  27. {% endfor %}
  28. };
  29. struct bootstrap_node tcp_relays[] = {
  30. {% for node in relays %}
  31. {
  32. "{{ node.ipv4 }}",
  33. {{ node.port }},
  34. {
  35. {{ node.public_key|toxtoc }}
  36. }
  37. },
  38. {% endfor %}
  39. };
  40. """
  41. def toxtoc(value):
  42. """
  43. A Jinja2 filter to turn a ToxID into two lines of C bytes
  44. """
  45. def get_16_bytes(value):
  46. """
  47. Generate 1 line of C code - 16 bytes
  48. @param value a hex string of length 32 (32 hex chars)
  49. """
  50. if len(value) <> 32:
  51. raise ValueError('%r is not a 32-char string')
  52. rv = ""
  53. for i in range(16):
  54. rv += "0x%s" % value[2*i : 2*i+2]
  55. if i < 15:
  56. rv += ", "
  57. return rv
  58. rv = get_16_bytes(value[:32]) + \
  59. ",\n" + (12*' ') + \
  60. get_16_bytes(value[32:])
  61. return rv
  62. class Loader(jinja2.BaseLoader):
  63. def get_source(self, environment, template):
  64. return tox_bootstrap_template, 'tox_bootstrap_template', True
  65. if __name__ == "__main__":
  66. r = requests.get(json_url)
  67. data = r.json()
  68. if 'nodes' not in data:
  69. raise ValueError('nodes element not in JSON')
  70. nodes = []
  71. tcp_relays = []
  72. for elem in data['nodes']:
  73. node = {}
  74. if 'ipv4' not in elem or 'port' not in elem or 'public_key' not in elem:
  75. print "SKIPPING", elem
  76. continue
  77. if len(elem['public_key']) <> 64:
  78. print "Bad public key %s, skipping!" % elem['public_key']
  79. continue
  80. node['port'] = int(elem['port'])
  81. node['public_key'] = elem['public_key']
  82. try:
  83. socket.inet_aton(elem['ipv4'])
  84. node['ipv4'] = elem['ipv4']
  85. except socket.error:
  86. # IPv4 is not numeric, let's try resolving
  87. try:
  88. print "RESOLVING", elem['ipv4']
  89. node['ipv4'] = socket.gethostbyname(elem['ipv4'])
  90. except socket.error:
  91. print "Could not resolve ipv4: %s, skipping!" % elem['ipv4']
  92. continue
  93. if 'status_udp' in elem and elem['status_udp']:
  94. nodes.append(node)
  95. if 'tcp_ports' in elem and elem['tcp_ports'] and \
  96. 'status_tcp' in elem and elem['status_tcp']:
  97. for port in elem['tcp_ports']:
  98. relay = dict(node)
  99. try:
  100. relay['port'] = int(port)
  101. except ValueError:
  102. continue
  103. tcp_relays.append(relay)
  104. env = jinja2.Environment(loader=Loader())
  105. env.filters['toxtoc'] = toxtoc
  106. template = env.get_template('tox_bootstrap_template')
  107. tox_bootstrap_h = template.render(nodes=nodes, now=datetime.datetime.now(), json_url=json_url, relays=tcp_relays)
  108. open('tox_bootstrap.h', 'w').write(tox_bootstrap_h)