forked from smlng/bgp-hijack-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitoring.html
336 lines (299 loc) · 12 KB
/
monitoring.html
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!DOCTYPE html>
<html>
<head>
<title>Peeroskop: BGP Monitoring</title>
<script type="text/javascript" src="http://d3js.org/d3.v2.min.js"></script>
<link rel="stylesheet" type="text/css" href="demo.css"/>
</head>
<body>
<div id="header" align="center" style="height:100px; line-height:100px; background-color:#e41a1c">
<span style="color:#fff;font-size:80px; font-family:sans-serif;">BGP Monitoring</span>
</div>
<div id="viewer" align="center">
<script>
// websockets -------------------------------------
// function newWebsocket() {
var connection = new WebSocket('ws://localhost:5002');
// When the connection is open, send some data to the server
connection.onopen = function ()
{
console.log('OPEN');
};
// Log errors
connection.onerror = function (error)
{
console.log('WebSocket Error ' + error);
};
// Log messages from the server
connection.onmessage = function (e)
{
console.log("update graph...");
// TODO:
// only parse, if update has no line break
if (e.data != "\n") {
// parse string to json object
json = JSON.parse(e.data)
newData(json);
};
};
// }
//-------------------------------------------------
var width = window.innerWidth-50,
height = window.innerHeight-110;
var force = d3.layout.force()
.charge(function (d) { return d.asn ? -1500 : -300 } )
.linkDistance(function (d) { return node_link_distance(d) } )
.size([width, height])
.on("tick", tick);
var nodes = [],
links = [];
var svg = d3.select("#viewer").append("svg")
.attr("width", width)
.attr("height", height)
var node = svg.selectAll(".node"),
path = svg.selectAll(".link");
var tooltipDiv = d3.select("#viewer")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
function initGraph() {
var request = new XMLHttpRequest();
request.open("POST", "/demo/graph", false);
request.send("dummy");
console.log(request.responseText);
json = JSON.parse(request.responseText);
json.nodes.forEach(function (d) {
nodes.push(d);
});
// add links
json.links.forEach(function (d) {
links.push({source: d.source, target: d.target});
});
// add prefixes as nodes and add links to corresponding 'asn'
json.nodes.forEach(function (nodeWithPrefix) {
nodeWithPrefix.prefixes.forEach(function (pfx) {
var newNode = nodes.push({prefix: pfx, parent: nodeWithPrefix.asn});
links.push({source: nodeWithPrefix, target: nodes[newNode-1]});
});
});
update();
};
function update() {
force
.nodes(nodes)
.links(links)
.start();
path = svg.selectAll(".link")
.data(links);
path.enter().insert("line", "g")
.attr("class", "link")
.style("stroke-width", function(d) { return node_link_width(d) })
.style("opacity", 0)
.transition().duration(3000)
.style("opacity", 1);
path.exit().remove();
node = svg.selectAll(".node")
.data(nodes);
node.select(".icon")
.attr("xlink:href", function(d) { return node_icon(d); })
.attr("x", function (d) { return ( -1 * node_icon_size(d)) + "px"; })
.attr("y", function (d) { return ( -1 * node_icon_size(d)) + "px"; })
.attr("width", function (d) { return ( 2 * node_icon_size(d)) + "px"; })
.attr("height", function (d) { return ( 2 * node_icon_size(d)) + "px"; });
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.call(force.drag);
nodeEnter.append("circle")
.attr("class", "circle")
.attr("r", function (d) { return d.asn ? 36 : 12 ; })
.style("stroke", function (d) { return d.asn ? "#4daf4a" : "#fff"; } )
.style("stroke-width", function (d) { return d.asn ? "5px" : "0px"; } )
.style("fill", function (d) { return node_color(d) });
nodeEnter.append("image")
.attr("class", "icon")
.attr("xlink:href", function(d) { return node_icon(d); })
.attr("x", function (d) { return ( -1 * node_icon_size(d)) + "px"; })
.attr("y", function (d) { return ( -1 * node_icon_size(d)) + "px"; })
.attr("width", function (d) { return ( 2 * node_icon_size(d)) + "px"; })
.attr("height", function (d) { return ( 2 * node_icon_size(d)) + "px"; });
node.on("mouseover", function (d) {
tooltipDiv.transition()
.duration(200)
.style("opacity", .75);
if (d.prefix && d.asn == null) {
tooltipDiv
.text(function (o) { return d.prefix; })
.style("color", "white")
.style("left", d.x + 25 + "px")
.style("top", d.y + 75 + "px");
}
else {
tooltipDiv.html(tip_text(d))
.style("color", "white")
.style("left", d.x + 45 + "px")
.style("top", d.y + "px");
}
});
node.on("mouseout", function (d) {
tooltipDiv.transition()
.duration(500)
.style("opacity", 0);
});
node.attr("xlink:href", function(d) { return node_icon(d); })
node.exit().remove();
}
function newData(json) {
// get the updated node
var updateNode = json.nodes[0];
var updateGraph = false
// return the origin node that will be updated
var nodeToUpdate = nodes.filter(function (d) {
return (d.asn == updateNode.asn);
})[0];
console.log("updateAS: "+nodeToUpdate.asn);
// check whether the update is an 'announcement'
if (updateNode.type == "announcement") {
// add new 'prefix' that the already existing AS reaches with specific 'path'
nodeToUpdate.path.push(updateNode.path);
nodeToUpdate.reaches.push(updateNode.prefix[0]);
var lastASIndex = updateNode.path.length-1;
// return the origin node that will receive a new prefix
var originASNode;
if (lastASIndex < 0) {
originASNode = nodeToUpdate;
}
else
{
originASNode= nodes.filter(function (d) {
return d.asn == updateNode.path[lastASIndex];
})[0];
}
console.log("originAS: "+originASNode.asn);
/***
Two types of announcements:
1.) nodeToUpdate.asn == originASNode.asn -> AS has new prefix
2.) nodeToUpdate.asn != originASNode.asn -> AS has new path for prefix
***/
if (nodeToUpdate.asn == originASNode.asn) {
var createPrefixNode = true;
nodes.filter(function (d) {
if (d.parent == nodeToUpdate.asn && d.prefix == updateNode.prefix[0]) {
nodeToUpdate = false;
}
})
if (nodeToUpdate) {
console.log("Add new node for prefix: "+updateNode.prefix);
// add new prefix as node and new link to 'complete graph'
var newNode = nodes.push({prefix: updateNode.prefix, parent: originASNode.asn});
links.push({source: originASNode, target: nodes[newNode-1]});
updateGraph = true
};
}
else {
var deletePrefixNode = false;
nodes.filter(function (d) {
if ((d.parent == nodeToUpdate.asn) && (d.prefix == updateNode.prefix[0])) {
deletePrefixNode = true;
}
})
if (deletePrefixNode) {
console.log("Prefix "+updateNode.prefix[0]+" withdrawn by "+nodeToUpdate.asn);
var newNodes = nodes.filter(function (d) {
// 'd' is a node
if (d.asn != null)
return d;
// 'd' is a prefix AND matches not to 'updateNode.prefix[0]'
else if ((d.parent != nodeToUpdate.asn) || (d.prefix != updateNode.prefix[0]))
return d;
});
var newLinks = links.filter(function (d) {
// as-as link
if ((d.source.asn != null) && (d.target.asn != null))
return d;
// 'target' is a prefix AND matches not to 'updateNode.prefix[0]'
else if ((d.target.prefix != updateNode.prefix[0]) || (d.target.parent != nodeToUpdate.asn))
return d;
});
nodes = newNodes;
links = newLinks;
updateGraph = true
}
}
}
else {
console.log("Unknown message received via websocket!");
}
if (updateGraph)
update();
}
function updateFromJSONFile() {
d3.json("updateAnnounce.json", function(json) {
newData(json);
});
}
function tick() {
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
path.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
}
function tip_text(d) {
// get announced prefixes
var request = new XMLHttpRequest();
request.open("POST", "/demo/get", false);
request.send("oid=1.3.6.1.4.1.8072.2.267."+d.asn+"."+d.port);
if(request.readyState == 4) {
head = "<table cellspacing=\"4\" cellpadding=\"2\"><tr><th></th><th>network</th><th>next hop</th><th>path</th></tr><tr><td>";
end = "</td></tr></table>";
return head+(request.responseText.split("\\t").join("</td><td>")).split("\\n").join("</td></tr><tr><td>")+end;
}
}
// set node color
function node_color(d) {
if (d.asn != null) {
return "#fff";
}
return "#377eb8";
}
function node_icon(d) {
if (d.asn != null) {
console.log("AS node");
return d.icon;
}
if (d.prefix == "160.45.111.0/26") {
console.log("node with zeit.de prefix");
return "zeit.png";
}
if (d.prefix == "160.45.144.0/26") {
console.log("node with enduser prefix");
return "user.png";
}
console.log("some other prefix node");
return "";
}
function node_icon_size(d) {
if (d.asn == "65005")
return 55;
if (d.prefix == "160.45.144.0/26")
return 20;
return 15;
}
// set link length by src-dst node type
function node_link_distance(d) {
if ((d.source.asn != null) && (d.target.asn != null))
return 150;
else
return 30;
}
// set link width by src-dst node type
function node_link_width(d) {
if ((d.source.asn != null) && (d.target.asn != null))
return "5px";
else
return "2px";
}
initGraph();
</script>
</body>
</html>