1
|
|
|
define(['moment', 'utils/router', 'leaflet', 'gui', 'helper', 'utils/language'], |
2
|
|
|
function (moment, Router, L, GUI, helper, Language) { |
3
|
|
|
'use strict'; |
4
|
|
|
|
5
|
|
|
return function (config) { |
6
|
|
|
function handleData(data) { |
7
|
|
|
var dataNodes = {}; |
8
|
|
|
dataNodes.nodes = []; |
9
|
|
|
var dataGraph = {}; |
10
|
|
|
dataGraph.batadv = {}; |
11
|
|
|
dataGraph.batadv.nodes = []; |
12
|
|
|
dataGraph.batadv.links = []; |
13
|
|
|
var gateways = {}; |
14
|
|
|
|
15
|
|
|
function rearrangeLinks(d) { |
16
|
|
|
d.source += dataGraph.batadv.nodes.length; |
17
|
|
|
d.target += dataGraph.batadv.nodes.length; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
for (var i = 0; i < data.length; ++i) { |
21
|
|
|
var vererr; |
22
|
|
|
if (i % 2) { |
23
|
|
|
if (data[i].version !== 1) { |
24
|
|
|
vererr = 'Unsupported graph version: ' + data[i].version; |
25
|
|
|
console.error(vererr); // silent fail |
26
|
|
|
} else { |
27
|
|
|
data[i].batadv.links.forEach(rearrangeLinks); |
28
|
|
|
dataGraph.batadv.nodes = dataGraph.batadv.nodes.concat(data[i].batadv.nodes); |
29
|
|
|
dataGraph.batadv.links = dataGraph.batadv.links.concat(data[i].batadv.links); |
30
|
|
|
dataGraph.timestamp = data[i].timestamp; |
31
|
|
|
} |
32
|
|
|
} else if (data[i].version !== 2) { |
33
|
|
|
vererr = 'Unsupported nodes version: ' + data[i].version; |
34
|
|
|
console.error(vererr); // silent fail |
35
|
|
|
} else { |
36
|
|
|
dataNodes.nodes = dataNodes.nodes.concat(data[i].nodes); |
37
|
|
|
dataNodes.timestamp = data[i].timestamp; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
var nodes = dataNodes.nodes.filter(function (d) { |
42
|
|
|
return 'firstseen' in d && 'lastseen' in d; |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
nodes.forEach(function (node) { |
46
|
|
|
node.firstseen = moment.utc(node.firstseen).local(); |
47
|
|
|
node.lastseen = moment.utc(node.lastseen).local(); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
var now = moment(); |
51
|
|
|
var age = moment(now).subtract(config.maxAge, 'days'); |
52
|
|
|
|
53
|
|
|
var newnodes = helper.limit('firstseen', age, helper.sortByKey('firstseen', nodes).filter(helper.online)); |
54
|
|
|
var lostnodes = helper.limit('lastseen', age, helper.sortByKey('lastseen', nodes).filter(helper.offline)); |
55
|
|
|
|
56
|
|
|
var graphnodes = {}; |
57
|
|
|
|
58
|
|
|
dataNodes.nodes.forEach(function (d) { |
59
|
|
|
graphnodes[d.nodeinfo.node_id] = d; |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
var graph = dataGraph.batadv; |
63
|
|
|
|
64
|
|
|
graph.nodes.forEach(function (d) { |
65
|
|
|
if (d.node_id in graphnodes) { |
66
|
|
|
d.node = graphnodes[d.node_id]; |
67
|
|
|
if (d.unseen) { |
68
|
|
|
d.node.flags.online = true; |
69
|
|
|
d.node.flags.unseen = true; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
graph.links.forEach(function (d) { |
75
|
|
|
d.source = graph.nodes[d.source]; |
76
|
|
|
|
77
|
|
|
if (graph.nodes[d.target].node) { |
78
|
|
|
d.target = graph.nodes[d.target]; |
79
|
|
|
} else { |
80
|
|
|
d.target = undefined; |
81
|
|
|
} |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
var links = graph.links.filter(function (d) { |
85
|
|
|
return d.target !== undefined; |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
nodes.forEach(function (d) { |
89
|
|
|
d.neighbours = []; |
90
|
|
|
if (d.nodeinfo.network.mesh) { |
91
|
|
|
var mesh = d.nodeinfo.network.mesh; |
92
|
|
|
if (mesh[Object.keys(mesh)[0]].interfaces.tunnel) { |
93
|
|
|
mesh[Object.keys(mesh)[0]].interfaces.tunnel.forEach(function (mac) { |
94
|
|
|
gateways[mac] = d; |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
if (mesh[Object.keys(mesh)[0]].interfaces.wireless) { |
98
|
|
|
mesh[Object.keys(mesh)[0]].interfaces.wireless.forEach(function (mac) { |
99
|
|
|
gateways[mac] = d; |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
if (mesh[Object.keys(mesh)[0]].interfaces.other) { |
103
|
|
|
mesh[Object.keys(mesh)[0]].interfaces.other.forEach(function (mac) { |
104
|
|
|
gateways[mac] = d; |
105
|
|
|
}); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
links.forEach(function (d) { |
111
|
|
|
var ids; |
112
|
|
|
|
113
|
|
|
ids = [d.source.node.nodeinfo.node_id, d.target.node.nodeinfo.node_id]; |
114
|
|
|
d.source.node.neighbours.push({ node: d.target.node, link: d, incoming: false }); |
115
|
|
|
d.target.node.neighbours.push({ node: d.source.node, link: d, incoming: true }); |
116
|
|
|
|
117
|
|
|
d.id = ids.join('-'); |
118
|
|
|
|
119
|
|
|
if (d.type === 'tunnel' || d.vpn) { |
120
|
|
|
d.type = 'VPN'; |
121
|
|
|
d.isVPN = true; |
122
|
|
|
} else if (d.type === 'fastd') { |
123
|
|
|
d.type = 'fastd'; |
124
|
|
|
d.isVPN = true; |
125
|
|
|
} else if (d.type === 'l2tp') { |
126
|
|
|
d.type = 'L2TP'; |
127
|
|
|
d.isVPN = true; |
128
|
|
|
} else if (d.type === 'gre') { |
129
|
|
|
d.type = 'GRE'; |
130
|
|
|
d.isVPN = true; |
131
|
|
|
} else if (d.type === 'wireless') { |
132
|
|
|
d.type = 'Wifi'; |
133
|
|
|
d.isVPN = false; |
134
|
|
|
} else if (d.type === 'other') { |
135
|
|
|
d.type = 'Kabel'; |
136
|
|
|
d.isVPN = false; |
137
|
|
|
} else { |
138
|
|
|
d.type = 'N/A'; |
139
|
|
|
d.isVPN = false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
try { |
143
|
|
|
d.latlngs = []; |
144
|
|
|
d.latlngs.push(L.latLng(d.source.node.nodeinfo.location.latitude, d.source.node.nodeinfo.location.longitude)); |
145
|
|
|
d.latlngs.push(L.latLng(d.target.node.nodeinfo.location.latitude, d.target.node.nodeinfo.location.longitude)); |
146
|
|
|
|
147
|
|
|
d.distance = d.latlngs[0].distanceTo(d.latlngs[1]); |
148
|
|
|
} catch (e) { |
149
|
|
|
// ignore exception |
150
|
|
|
} |
151
|
|
|
}); |
152
|
|
|
|
153
|
|
|
links.sort(function (a, b) { |
154
|
|
|
return b.tq - a.tq; |
155
|
|
|
}); |
156
|
|
|
|
157
|
|
|
return { |
158
|
|
|
now: now, |
159
|
|
|
timestamp: moment.utc(dataNodes.timestamp).local(), |
160
|
|
|
nodes: { |
161
|
|
|
all: nodes, |
162
|
|
|
new: newnodes, |
163
|
|
|
lost: lostnodes |
164
|
|
|
}, |
165
|
|
|
graph: { |
166
|
|
|
links: links, |
167
|
|
|
nodes: graph.nodes |
168
|
|
|
}, |
169
|
|
|
gateways: gateways |
170
|
|
|
}; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
var language = new Language(config); |
174
|
|
|
var router = new Router(language); |
175
|
|
|
|
176
|
|
|
var urls = []; |
177
|
|
|
|
178
|
|
|
if (typeof config.dataPath === 'string' || config.dataPath instanceof String) { |
179
|
|
|
config.dataPath = [config.dataPath]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
for (var i in config.dataPath) { |
183
|
|
|
if (config.dataPath.hasOwnProperty(i)) { |
184
|
|
|
urls.push(config.dataPath[i] + 'nodes.json'); |
185
|
|
|
urls.push(config.dataPath[i] + 'graph.json'); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
function update() { |
190
|
|
|
language.init(router); |
191
|
|
|
return Promise.all(urls.map(helper.getJSON)) |
192
|
|
|
.then(handleData); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
update() |
196
|
|
|
.then(function (d) { |
197
|
|
|
var gui = new GUI(config, router, language); |
198
|
|
|
gui.setData(d); |
199
|
|
|
router.setData(d); |
200
|
|
|
router.resolve(); |
201
|
|
|
|
202
|
|
|
window.setInterval(function () { |
203
|
|
|
update().then(function (n) { |
204
|
|
|
gui.setData(n); |
205
|
|
|
router.setData(n); |
206
|
|
|
}); |
207
|
|
|
}, 60000); |
208
|
|
|
}) |
209
|
|
|
.catch(function (e) { |
210
|
|
|
document.querySelector('.loader').innerHTML += e.message |
211
|
|
|
+ '<br /><br /><button onclick="location.reload(true)" class="btn text">Try to reload</button><br /> or report to your community'; |
212
|
|
|
console.warn(e); |
213
|
|
|
}); |
214
|
|
|
}; |
215
|
|
|
}); |
216
|
|
|
|