-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
executable file
·303 lines (226 loc) · 7.92 KB
/
filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/python3
# -*- coding: utf-8; -*-
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2016-2020, Sven Eckelmann <[email protected]>
import json
import os
import os.path
import sys
def dump_json(data, filename):
with open(filename, 'w') as f:
json.dump(data, f)
f.flush()
os.fsync(f.fileno())
def valid_node(n):
# is vpn?
if 'vpn' in n['nodeinfo'] and n['nodeinfo']['vpn']:
return True
# node with default config offline?
online = True
if 'flags' in n and 'online' in n['flags'] and not n['flags']['online']:
online = False
if online is False and 'hostname' in n['nodeinfo'] and \
n['nodeinfo']['hostname'].startswith("ffv-"):
return False
return True
def get_nodes_validity(nodes):
return {n['nodeinfo']['node_id']: valid_node(n) for n in nodes['nodes']}
def filter_nodes(nodes, valid_nodes):
# only save valid nodes
nodes_ffv = filter(lambda n: valid_nodes[n['nodeinfo']['node_id']],
nodes['nodes'])
nodes['nodes'] = list(nodes_ffv)
def filter_meshviewer(meshviewer, valid_nodes):
# only save valid nodes
nodes_ffv = filter(lambda n: n['node_id'] in valid_nodes and valid_nodes[n['node_id']],
meshviewer['nodes'])
meshviewer['nodes'] = list(nodes_ffv)
nodes_src = filter(lambda n: valid_nodes[n['source']], meshviewer['links'])
nodes_dst = filter(lambda n: valid_nodes[n['target']], nodes_src)
meshviewer['links'] = list(nodes_dst)
def filter_nodelist(nodelist, valid_nodes):
nodes_ffv = filter(lambda n:
n['id'] in valid_nodes and valid_nodes[n['id']],
nodelist['nodes'])
nodelist['nodes'] = list(nodes_ffv)
def add_gw_nexthop(nodes, meshviewer):
nexthops_id = {}
for n in meshviewer['nodes']:
if 'gateway_nexthop' not in n:
continue
if 'node_id' not in n:
continue
nexthops_id[n['node_id']] = n['gateway_nexthop']
gw_id = {}
for n in meshviewer['nodes']:
if 'gateway' not in n:
continue
if 'node_id' not in n:
continue
gw_id[n['node_id']] = n['gateway']
# prepare mapping of interface mac to "primary" mac
primary_mac_mapping = {}
for n in nodes['nodes']:
if 'nodeinfo' not in n:
continue
if 'node_id' not in n['nodeinfo']:
continue
node_id = n['nodeinfo']['node_id']
if 'network' not in n['nodeinfo']:
continue
if 'mac' not in n['nodeinfo']['network']:
continue
primary_mac_mapping[node_id] = n['nodeinfo']['network']['mac']
for n in nodes['nodes']:
if 'nodeinfo' not in n:
continue
if 'node_id' not in n['nodeinfo']:
continue
if 'statistics' not in n:
continue
if n['nodeinfo']['node_id'] in gw_id:
node_gw_id = gw_id[n['nodeinfo']['node_id']]
if node_gw_id not in primary_mac_mapping:
continue
n['statistics']['gateway'] = primary_mac_mapping[node_gw_id]
if n['nodeinfo']['node_id'] in nexthops_id:
node_gw_id = nexthops_id[n['nodeinfo']['node_id']]
if node_gw_id not in primary_mac_mapping:
continue
nexthop = primary_mac_mapping[node_gw_id]
n['statistics']['gateway_nexthop'] = nexthop
def add_uplink(nodes):
for n in nodes['nodes']:
if 'flags' not in n:
continue
n['flags']['uplink'] = False
if 'statistics' not in n:
continue
if 'gateway' not in n['statistics']:
continue
if 'gateway_nexthop' not in n['statistics']:
continue
# TODO maybe it is better to check the graph for links with type 'vpn'
if n['statistics']['gateway'] != n['statistics']['gateway_nexthop']:
continue
n['flags']['uplink'] = True
def extract_graph(meshviewer):
graph = {
"version": 1,
"batadv": {
"multigraph": True,
"graph": {},
"directed": True,
"nodes": [],
"links": [],
},
}
endpoints_map = {}
endpoints = graph['batadv']['nodes']
links = graph['batadv']['links']
# prepare mapping of interface mac to "primary" mac
primary_mac_mapping = {}
for n in meshviewer['nodes']:
if 'node_id' not in n:
continue
if 'mac' not in n:
continue
primary_mac_mapping[n['node_id']] = n['mac']
pos = 0
for l in meshviewer['links']:
if 'source' not in l:
continue
if 'source_tq' not in l:
continue
if 'target' not in l:
continue
if 'target_tq' not in l:
continue
if 'type' not in l:
continue
if l['source'] not in primary_mac_mapping:
continue
if l['target'] not in primary_mac_mapping:
continue
src = (l['source'], primary_mac_mapping[l['source']])
dst = (l['target'], primary_mac_mapping[l['target']])
if src not in endpoints_map:
endpoints.append({
"node_id": src[0],
"id": src[1],
})
endpoints_map[src] = pos
pos += 1
if dst not in endpoints_map:
endpoints.append({
"node_id": dst[0],
"id": dst[1],
})
endpoints_map[dst] = pos
pos += 1
if l['target_tq'] <= 0:
l['target_tq'] = 0.01
if l['source_tq'] <= 0:
l['source_tq'] = 0.01
if l["type"] == "vpn":
mapped_type = "tunnel"
elif l["type"] == "wifi":
mapped_type = "wireless"
else:
mapped_type = l["type"]
links.append({
"key": 0,
"source": endpoints_map[src],
"target": endpoints_map[dst],
"tq": 1. / l['source_tq'],
"type": mapped_type,
})
links.append({
"key": 0,
"source": endpoints_map[dst],
"target": endpoints_map[src],
"tq": 1. / l['target_tq'],
"type": mapped_type,
})
return graph
def filter_json(nodes, nodelist, meshviewer):
valid_nodes = get_nodes_validity(nodes)
filter_nodes(nodes, valid_nodes)
filter_meshviewer(meshviewer, valid_nodes)
add_gw_nexthop(nodes, meshviewer)
add_uplink(nodes)
filter_nodelist(nodelist, valid_nodes)
def main():
if len(sys.argv) != 3:
print("./filter INPATH OUTPATH")
sys.exit(1)
inpath = sys.argv[1]
outpath = sys.argv[2]
graph_out = os.path.join(outpath, "graph.json")
graph_outtmp = os.path.join(outpath, "graph.json.tmp")
nodes_in = os.path.join(inpath, "nodes.json")
nodes_out = os.path.join(outpath, "nodes.json")
nodes_outtmp = os.path.join(outpath, "nodes.json.tmp")
nodelist_in = os.path.join(inpath, "nodelist.json")
nodelist_out = os.path.join(outpath, "nodelist.json")
nodelist_outtmp = os.path.join(outpath, "nodelist.json.tmp")
meshviewer_in = os.path.join(inpath, "meshviewer.json")
meshviewer_out = os.path.join(outpath, "meshviewer.json")
meshviewer_outtmp = os.path.join(outpath, "meshviewer.json.tmp")
# load
nodes = json.load(open(nodes_in))
nodelist = json.load(open(nodelist_in))
meshviewer = json.load(open(meshviewer_in))
filter_json(nodes, nodelist, meshviewer)
graph = extract_graph(meshviewer)
# save
dump_json(graph, graph_outtmp)
dump_json(nodes, nodes_outtmp)
dump_json(nodelist, nodelist_outtmp)
dump_json(meshviewer, meshviewer_outtmp)
os.rename(graph_outtmp, graph_out)
os.rename(nodes_outtmp, nodes_out)
os.rename(nodelist_outtmp, nodelist_out)
os.rename(meshviewer_outtmp, meshviewer_out)
if __name__ == "__main__":
main()