-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
175 lines (160 loc) · 4.71 KB
/
app.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
const COLORS = [
"#a2cffe",
"#a6e7ff",
"#74bbfb",
"#efc5b5",
"#fbe8ce",
"#fdee73",
"#aaffaa",
"#aefd6c",
"#a5fbd5",
"#64bfa4",
"#c8fd3d",
"#ffa180",
"#efc0fe",
"#ffcfdc",
"#eeaaff",
"#bf77f6",
"#f6cefc",
"#feff7f"
]
async function draw() {
let url = new URL('/connection.data', location.protocol + "//" + location.host + "/")
let links = (await fetch(url).then(r => r.text())).split('\n').map(_ => _.split('\t')).map(link => {
link[0] = link[0].trim()
link[1] = link[1].trim()
return link
}).filter(link => link[0] && link[1])
let formattedData = {
nodes: [],
// {
// "id" : "1.1.1.1",
// "label" : "1.1.1.1",
// }
links: [],
// {
// "from" : "1.1.1.1",
// "to" : "8.8.8.8",
// },
colors: {}
}
let counter = []
let getColor = (node) => {
if (!Object.keys(formattedData.colors).includes(node)) {
formattedData.colors[node] = COLORS.pop()
}
return formattedData.colors[node]
}
var i = 0;
for (let link of links) {
let node1 = link[0]
let node2 = link[1]
let node1Owner = node1.split('-')[0]
let node2Owner = node2.split('-')[0]
if (!formattedData.nodes.find(_ => _.label == node1)) {
counter.push([node1, 0])
formattedData.nodes.push({
"id": i++,
"label": node1,
"color": {
background: getColor(node1Owner),
color: getColor(node1Owner)
},
})
}
if (!formattedData.nodes.find(_ => _.label == node2)) {
counter.push([node2, 0])
formattedData.nodes.push({
"id": i++,
"label": node2,
"color": {
background: getColor(node2Owner),
color: getColor(node2Owner)
},
})
}
if (node1Owner != node2Owner) {
counter.find(_ => _[0] == node1)[1]++
counter.find(_ => _[0] == node2)[1]++
}
formattedData.links.push({
"from": formattedData.nodes.find(_ => _.label == node1).id,
"to": formattedData.nodes.find(_ => _.label == node2).id,
"label": ``,
})
}
console.log(counter.sort((a, b) => {
return b[1] - a[1]
}))
const nodes = new vis.DataSet(formattedData.nodes);
const edges = new vis.DataSet(formattedData.links);
const nodesView = new vis.DataView(nodes);
const edgesView = new vis.DataView(edges);
let network = new vis.Network(document.getElementById("network"), {
nodes: nodesView,
edges: edgesView,
}, {
edges: {
length: 300,
},
interaction: {
hideEdgesOnDrag: true,
},
physics: {
solver: "barnesHut",
barnesHut: {
gravitationalConstant: -10000,
avoidOverlap: 1,
},
stabilization: {
enabled: true,
iterations: 20,
},
},
nodes: {
shape: "dot",
color: {
highlight: {
background: "lightgreen",
},
},
},
});
network.on('doubleClick', (event) => {
console.log(event)
let nodeID = event.nodes[0]
let node = formattedData.nodes.find(_ => _.id == nodeID)
let selfNodeIDs = new Set();
let usedNodeIDs = new Set();
let owner = node.label.split('-')[0]
if (node) {
for (let node of formattedData.nodes) {
if (node.label.startsWith(owner)) {
selfNodeIDs.add(node.id)
}
}
for (let edge of formattedData.links) {
let aNodeID = edge.from;
let aNode = formattedData.nodes.find(_ => _.id == aNodeID);
let bNodeID = edge.to;
let bNode = formattedData.nodes.find(_ => _.id == bNodeID);
if (!selfNodeIDs.has(aNodeID) && !selfNodeIDs.has(bNodeID)) {
edges.remove({
id: edge.id
})
} else {
usedNodeIDs.add(aNodeID)
usedNodeIDs.add(bNodeID)
}
}
for (let node of formattedData.nodes) {
if (!usedNodeIDs.has(node.id)) {
nodes.remove({
id: node.id
})
}
}
}
})
}
draw()