summaryrefslogtreecommitdiff
path: root/generate_tox_bootstrap.py
diff options
context:
space:
mode:
Diffstat (limited to 'generate_tox_bootstrap.py')
-rw-r--r--generate_tox_bootstrap.py112
1 files changed, 112 insertions, 0 deletions
diff --git a/generate_tox_bootstrap.py b/generate_tox_bootstrap.py
new file mode 100644
index 0000000..cb8c804
--- /dev/null
+++ b/generate_tox_bootstrap.py
@@ -0,0 +1,112 @@
1#!/usr/bin/python
2# pip install jinja2 requests
3
4import datetime
5import jinja2
6import json
7import requests
8import socket
9
10json_url = 'https://nodes.tox.chat/json'
11
12tox_bootstrap_template = """
13/*
14 * Generated with generate_tox_bootstrap.py by GDR!
15 * from {{ json_url }} on {{ now }}
16 */
17struct bootstrap_node {
18 char *address;
19 uint16_t port;
20 uint8_t key[32];
21} bootstrap_nodes[] = {
22{% for node in nodes %}
23 {
24 "{{ node.ipv4 }}",
25 {{ node.port }},
26 {
27 {{ node.public_key|toxtoc }}
28 }
29 },
30{% endfor %}
31 {
32 "176.31.28.63",
33 465,
34 {
35 0x0B, 0x6C, 0x7A, 0x93, 0xA6, 0xD8, 0xC0, 0xEB, 0x44, 0x96, 0x5C, 0x4A, 0x85, 0xCB, 0x88, 0xBA,
36 0x75, 0x71, 0x17, 0x0F, 0xE2, 0xDB, 0x3D, 0xEA, 0x10, 0x71, 0x3E, 0x48, 0x55, 0x9A, 0x55, 0x4D
37 }
38 },
39};
40"""
41
42def toxtoc(value):
43 """
44 A Jinja2 filter to turn a ToxID into two lines of C bytes
45 """
46 def get_16_bytes(value):
47 """
48 Generate 1 line of C code - 16 bytes
49 @param value a hex string of length 32 (32 hex chars)
50 """
51 if len(value) <> 32:
52 raise ValueError('%r is not a 32-char string')
53
54 rv = ""
55
56 for i in range(16):
57 rv += "0x%s" % value[2*i : 2*i+2]
58 if i < 15:
59 rv += ", "
60
61 return rv
62
63 rv = get_16_bytes(value[:32]) + \
64 ",\n" + (12*' ') + \
65 get_16_bytes(value[32:])
66
67 return rv
68
69class Loader(jinja2.BaseLoader):
70 def get_source(self, environment, template):
71 return tox_bootstrap_template, 'tox_bootstrap_template', True
72
73if __name__ == "__main__":
74 r = requests.get(json_url)
75 data = r.json()
76 if 'nodes' not in data:
77 raise ValueError('nodes element not in JSON')
78
79 nodes = []
80 for elem in data['nodes']:
81 node = {}
82 if 'ipv4' not in elem or 'port' not in elem or 'public_key' not in elem:
83 print "SKIPPING", elem
84 continue
85
86 if len(elem['public_key']) <> 64:
87 print "Bad public key %s, skipping!" % elem['public_key']
88 continue
89
90 node['port'] = int(elem['port'])
91 node['public_key'] = elem['public_key']
92
93 try:
94 socket.inet_aton(elem['ipv4'])
95 node['ipv4'] = elem['ipv4']
96 except socket.error:
97 # IPv4 is not numeric, let's try resolving
98 try:
99 print "RESOLVING", elem['ipv4']
100 node['ipv4'] = socket.gethostbyname(elem['ipv4'])
101 except socket.error:
102 print "Could not resolve ipv4: %s, skipping!" % elem['ipv4']
103 continue
104
105 nodes.append(node)
106
107 env = jinja2.Environment(loader=Loader())
108 env.filters['toxtoc'] = toxtoc
109 template = env.get_template('tox_bootstrap_template')
110 tox_bootstrap_h = template.render(nodes=nodes, now=datetime.datetime.now(), json_url=json_url)
111 open('tox_bootstrap.h', 'w').write(tox_bootstrap_h)
112