-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaclookup.js
190 lines (148 loc) · 4.83 KB
/
maclookup.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
class MacLookup extends Console {
constructor(params) {
super();
this.params = params ?? { entries: [] };
this.AddCssDependencies("tools.css");
this.hashtable = {}; //contains all elements
this.SetTitle("MAC lookup");
this.SetIcon("mono/maclookup.svg");
this.SetupToolbar();
this.clearButton = this.AddToolbarButton("Clear", "mono/wing-light.svg");
this.txtInput.placeholder = "mac address";
if (this.params.entries) { //restore entries from previous session
let temp = this.params.entries;
this.params.entries = [];
for (let i = 0; i < temp.length; i++)
this.Push(temp[i]);
}
this.clearButton.addEventListener("click", ()=> {
const btnOK = this.ConfirmBox("Are you sure you want to clear the list?");
if (btnOK) btnOK.addEventListener("click", ()=> {
this.list.textContent = "";
this.hashtable = {};
this.params.entries = [];
});
});
}
Push(name) { //override
if (!super.Push(name)) return;
this.Filter(name);
}
Filter(macaddr) {
if (macaddr.indexOf(";", 0) > -1) {
let ips = macaddr.split(";");
for (let i = 0; i < ips.length; i++) this.Add(ips[i].trim());
}
else if (macaddr.indexOf(",", 0) > -1) {
let ips = macaddr.split(",");
for (let i = 0; i < ips.length; i++) this.Add(ips[i].trim());
}
else {
this.Add(macaddr);
}
}
Add(macaddr) {
while (macaddr.indexOf("-") > -1) macaddr = macaddr.replace("-", "");
while (macaddr.indexOf(":") > -1) macaddr = macaddr.replace(":", "");
while (macaddr.indexOf(" ") > -1) macaddr = macaddr.replace(" ", "");
if (this.hashtable.hasOwnProperty(macaddr)) {
this.list.appendChild(this.hashtable[macaddr].element);
return;
}
this.txtInput.className = "input-box-dark";
let element = document.createElement("div");
element.className = "tool-element collapsible-box";
this.list.appendChild(element);
let name = document.createElement("div");
name.className = "tool-label";
name.style.paddingLeft = "24px";
name.innerHTML = macaddr;
element.appendChild(name);
let result = document.createElement("div");
result.className = "tool-result collapsed100";
result.innerHTML = "";
element.appendChild(result);
let remove = document.createElement("div");
remove.className = "tool-remove";
element.appendChild(remove);
this.hashtable[macaddr] = {
element: element,
result: result
};
remove.onclick = () => { this.Remove(macaddr); };
this.params.entries.push(macaddr);
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState == 4 && xhr.status == 200) {
let bytes = xhr.responseText;
let target = this.BytesToInt([
parseInt(macaddr.toLowerCase().substring(4, 6), 16),
parseInt(macaddr.toLowerCase().substring(2, 4), 16),
parseInt(macaddr.toLowerCase().substring(0, 2), 16)
]);
let label = document.createElement("div");
result.appendChild(label);
if (isNaN(target)) {
label.innerHTML = "not a valid mac address";
return;
}
let namesBegin = this.BytesToInt([
bytes.charCodeAt(0) & 0xff,
bytes.charCodeAt(1) & 0xff,
bytes.charCodeAt(2) & 0xff,
bytes.charCodeAt(3) & 0xff
]);
let low = 4;
let high = namesBegin;
let pivot, current;
do { //binary search
pivot = (low + high) / 2;
pivot = pivot - pivot % 7 + 4;
current = this.BytesToInt([
bytes.charCodeAt(pivot + 2) & 0xff,
bytes.charCodeAt(pivot + 1) & 0xff,
bytes.charCodeAt(pivot) & 0xff
]);
if (current == target) break; //found
if (target < current) high = pivot;
if (target > current) low = pivot;
} while (high - low > 7);
if (target == current) { //found
let manufacturer = "";
let name_index = this.BytesToInt([
bytes.charCodeAt(pivot + 3) & 0xff,
bytes.charCodeAt(pivot + 4) & 0xff,
bytes.charCodeAt(pivot + 5) & 0xff,
bytes.charCodeAt(pivot + 6) & 0xff
]);
let char = null;
do {
char = bytes.charCodeAt(namesBegin + name_index++) & 0xff;
manufacturer += String.fromCharCode(char);
} while (char != 0 && manufacturer.length < 512);
label.innerHTML = manufacturer;
} else {
label.innerHTML = "not found";
}
} else if (xhr.readyState == 4 && xhr.status == 0) //disconnected
this.ConfirmBox("Server is unavailable.", true);
};
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.open("GET", "data/mac.bin", true);
xhr.send();
}
BytesToInt(array) {
var value = 0;
for (var i = array.length - 1; i >= 0; i--)
value = Number(value * 256) + Number(array[i]);
return value;
};
Remove(macaddr) {
if (!(macaddr in this.hashtable)) return;
this.list.removeChild(this.hashtable[macaddr].element);
delete this.hashtable[macaddr];
const index = this.params.entries.indexOf(macaddr);
if (index > -1)
this.params.entries.splice(index, 1);
}
}