forked from tux-00/network_miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
214 lines (183 loc) · 5.58 KB
/
functions.js
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
// Scalable Vector Graphics object
var svg;
// Nodes list
var nodes = [];
$(document).ready(function() {
$("#search_btn").prop('disabled', true);
});
$('#discover_btn').click(function() {
if ($("#ip_input").val() == '') {
$('#ip_input').popover('show');
} else {
$('#ip_input').popover('hide');
discover();
}
});
$('#search_btn').click(function() {
display_node(document.getElementById('search_input').value);
});
$('.selectpicker').selectpicker();
$('#community_input').popover();
$('#dig_level_input').popover();
// Run algorythm to discover nodes/links and create map
function discover() {
jQuery.ajax({
type: 'POST',
url: 'data_mining.php',
data: {
selected_proto: $("#select_proto option:selected").text(),
ip_input: $("#ip_input").val(),
community_input: $("#community_input").val(),
dig_level_input: $("#dig_level_input").val()
},
beforeSend: function(jqXHR, settings) {
$("#notif").html("");
$("svg").remove();
$(":input").prop('disabled', true);
$("#discover_btn").button('loading');
},
success: function(data, textStatus, jqXHR) {
$("#notif").html(data);
$(":input").prop('disabled', false);
$("#discover_btn").button('reset');
rendering();
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error (" + errorThrown + ": " + textStatus + ")");
}
});
}
// From twitter typeahead.js examples
// Return matching nodes
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// Array that will be populated with substring matches
matches = [];
// Regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// Iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
// Typeahead init
var typeahead = $('#search_input').typeahead({
hint: true,
highlight: true,
}, {
name: 'nodes',
limit: 8,
source: substringMatcher(nodes)
});
// Display the selected node
typeahead.on('typeahead:selected', function(evt, data) {
display_node(data);
});
// Create D3JS network map
function rendering() {
// Layout scale
var width = 1400,
height = 1600;
// Set colors
var color = d3.scale.category20();
// Set force layout
var force = d3.layout.force()
.charge(-400)
.linkDistance(150)
.size([width, height]);
// Add svg to the page
svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// Read json data
$.getJSON('./data/snmp_data.json', function(data) {
graph = data;
// Create nodes and links
force.nodes(graph.nodes)
.links(graph.links)
.start();
// Create lines
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) {
return Math.sqrt(d.value);
});
// Create circles
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
.attr("r", 8)
.style("fill", function(d) {
return color(d.group);
})
node.append("text")
.attr("dx", 10)
.attr("dy", ".35em")
.text(function(d) {
return d.name
});
// Generating coordinates
force.on("tick", function() {
link.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;
});
d3.selectAll("circle").attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
});
d3.selectAll("text").attr("x", function(d) {
return d.x;
})
.attr("y", function(d) {
return d.y;
});
});
// Get nodes name
for (var i = 0; i < graph.nodes.length - 1; i++) {
nodes.push(graph.nodes[i].name);
}
nodes = nodes.sort();
});
}
// Display the node 'node_mame'
function display_node(node_name) {
if($.inArray(node_name, nodes) == -1) {
return false;
}
var node = svg.selectAll(".node");
if (node_name == "none") {
node.style("stroke", "white").style("stroke-width", "1");
} else {
var selected = node.filter(function(d, i) {
return d.name != node_name;
});
selected.style("opacity", "0");
var link = svg.selectAll(".link")
link.style("opacity", "0");
d3.selectAll(".node, .link").transition()
.duration(5000)
.style("opacity", 1);
}
}