-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClientManager.js
387 lines (356 loc) · 10.8 KB
/
ClientManager.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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
const EventEmitter = require('events');
const OUI = require('oui');
const Config = require('./Config');
const DeviceInstanceManager = require('./DeviceInstanceManager');
const DeviceState = require('./DeviceState');
const TopologyManager = require('./TopologyManager');
const DB = require('./Database');
const ARP = require('./discovery/arp');
const Debounce = require('./utils/Debounce');
const Log = require('debug')('clientmanager');
const SCRUB_TIMER = 10 * 60 * 1000; // 10 minutes
class ClientManager extends EventEmitter {
constructor() {
super();
this.mac = {};
}
async start() {
((await DB.getAllMacs()) || []).forEach(data => {
this.updateEntry(data._id, { name: data.name, firstSeen: data.firstSeen, lastSeen: data.lastSeen });
});
const debounced = Debounce(() => this.updateDeviceClients());
DeviceInstanceManager.on('update', evt => {
if (!evt.key || evt.key.startsWith('network.clients.') || (evt.key.startsWith('network.physical.port.') && evt.key.indexOf('.statistics.') === -1)) {
debounced();
}
});
TopologyManager.on('update', () => {
// Topology has changed, so we need to recalculate all the connection points.
this.updateDeviceClients();
});
this.arp = ARP.getInstance();
this.arp.on('update', () => {
this.updateArpClients();
});
this.arp.start();
this._scrubTimer = setInterval(() => {
this.updateDeviceClients();
this.scrubEntries();
}, SCRUB_TIMER);
this.updateDeviceClients();
this.scrubEntries();
}
stop() {
this.arp.stop();
clearInterval(this._scrubTimer);
this._scrubTimer = null;
}
async forgetClient(mac) {
if (!this.mac[mac]) {
return false;
}
await DB.removeMac(this.toDB(mac));
delete this.mac[mac];
this.emit('update');
return true;
}
async updateDeviceClients() {
Log('update clients:');
let change = false;
// Build a map of device:portnr to quickly identify clients which
// are just connected to other parts of the network.
const net = {};
TopologyManager.getTopology().forEach(link => {
net[`${link[0].device._id}:${link[0].port}`] = true;
net[`${link[1].device._id}:${link[1].port}`] = true;
});
const attach = TopologyManager.getAttachmentPoint();
const devices = DeviceInstanceManager.getAuthenticatedDevices();
for (let i = 0; i < devices.length; i++) {
const device = devices[i];
const clients = device.readKV('network.clients');
for (let id in clients) {
const client = clients[id];
let portnr = client.portnr;
if (String(portnr).indexOf('lag') === 0) {
// Some devices report lags as being a port and so macs can appear on these virtual ports.
// We map these to the base port on the lag itself which is how we track things.
const link = TopologyManager.findLinkLag(device, parseInt(portnr.substring(3)));
if (!link) {
continue;
}
portnr = link[0].port;
}
else {
// Othewise, just map the port to the base port of the lag (if there is one)
const link = TopologyManager.findLink(device, portnr);
if (link) {
portnr = link[0].port;
}
}
const update = {};
if (client.ip) {
update.ip = client.ip;
}
if (client.hostname) {
update.hostname = client.hostname;
}
if (client.ssid) {
update.ssid = client.ssid;
update.connected = { device: device, portnr: client.ssid };
update.limited = null;
update.blocked = null;
}
else if (!net[`${device._id}:${portnr}`]) {
update.connected = { device: device, portnr: portnr };
const port = device.readKV(`network.physical.port.${portnr}`);
if (port.limit) {
update.limited = {
ingress: port.limit.ingress,
egress: port.limit.egress
};
}
if ('enable' in port && !(attach && attach.device === device && attach.port === portnr)) {
update.blocked = !port.enable;
}
}
const changed = this.updateEntry(client.mac, update);
if (changed) {
change = true;
this.emit('update.client', { mac: client.mac });
}
}
}
if (change) {
this.emit('update');
}
}
async updateArpClients() {
Log('update arp clients:');
let change = false;
const hosts = this.arp.getAddresses();
for (let i = 0; i < hosts.length; i++) {
const mac = hosts[i].txt.macAddress;
const updated = this.updateEntry(mac, { ip: hosts[i].ip, hostname: hosts[i].txt.hostname });
if (updated) {
Log('update arp mac:', mac);
change = true;
this.emit('update.client', { mac: mac });
}
}
if (change) {
this.emit('update');
}
}
updateEntry(addr, info) {
const now = Date.now();
let change = false;
let entry = this.mac[addr];
if (!entry) {
const oui = OUI(addr);
entry = {
id: addr.replace(/:/g, '-'),
mac: addr,
ip: info.ip,
hostname: info.hostname,
name: info.name || '',
ssid: info.ssid || '',
firstSeen: info.firstSeen || now,
lastSeen: info.lastSeen || now,
instances: [],
oui: oui ? oui.split('\n')[0] : null,
connected: null,
blocked: info.blocked,
limited: info.limited
};
this.mac[addr] = entry;
change = true;
}
else {
for (let key in info) {
if (key in entry && info[key] !== entry[key]) {
change = true;
entry[key] = info[key];
}
}
entry.lastSeen = now;
}
if (change) {
DB.updateMac(this.toDB(addr));
}
return change;
}
scrubEntries() {
let anychange = false;
const before = Date.now() - 60 * 60 * 1000;
let ageout = parseFloat(Config.read('clients.inactive.age'));
if (ageout === 0) {
ageout = Number.MAX_SAFE_INTEGER;
}
else {
ageout = Date.now() - ageout * 24 * 60 * 60 * 1000;
}
for (let addr in this.mac) {
let change = false;
const entry = this.mac[addr];
if (entry.lastSeen < ageout && !entry.name) {
DB.removeMac(this.toDB(addr));
delete this.mac[addr];
anychange = true;
}
else if (entry.lastSeen < before) {
// Old entry. Clear transient info.
if (entry.ip) {
entry.ip = null;
change = true;
}
if (entry.connected) {
entry.connected = null;
change = true;
}
if (change) {
DB.updateMac(this.toDB(addr));
anychange = true;
}
}
}
if (anychange) {
this.emit('update');
}
}
async setName(mac, name) {
this.mac[mac].name = name;
await DB.updateMac(this.toDB(mac));
}
async setIngress(mac, value) {
const client = this.mac[mac];
if (client && client.limited) {
client.limited.ingress = value;
if (client.connected) {
if (client.ssid) {
// WiFi
}
else if (typeof client.connected.portnr === 'number') {
// Wired
client.connected.device.writeKV(`network.physical.port.${client.connected.portnr}.limit.ingress`, value);
}
}
}
}
async setEgress(mac, value) {
const client = this.mac[mac];
if (client && client.limited) {
client.limited.egress = value;
if (client.connected) {
if (client.ssid) {
// WiFi
}
else if (typeof client.connected.portnr === 'number') {
// Wired
client.connected.device.writeKV(`network.physical.port.${client.connected.portnr}.limit.egress`, value);
}
}
}
}
async setBlocked(mac, isblocked) {
const client = this.mac[mac];
if (client) {
client.blocked = isblocked;
if (client.connected) {
if (client.ssid) {
// WiFi
}
else if (typeof client.connected.portnr === 'number') {
// Wired
client.connected.device.writeKV(`network.physical.port.${client.connected.portnr}.enable`, !isblocked);
}
}
}
}
getClientsForDeviceAndPort(device, portnr) {
const clients = [];
for (let mac in this.mac) {
const client = this.mac[mac];
if (client.connected && client.connected.device === device && client.connected.portnr === portnr) {
clients.push(client);
}
}
return clients;
}
getClientByMac(addr) {
return this.mac[addr];
}
getClientByIP(address) {
for (let addr in this.mac) {
if (this.mac[addr].ip == address) {
return this.mac[addr];
}
}
return null;
}
getAllClients() {
return this.mac;
}
getFilteredClients(filter) {
const inlast24hours = Date.now() - (24 * 60 * 60 * 1000);
const clients = {};
for (let m in this.mac) {
const client = this.mac[m];
let include = true;
if (filter.onlyNew && client.firstSeen <= inlast24hours) {
include = false;
}
if (filter.onlyBlocked && !client.blocked) {
include = false;
}
if (filter.onlyLimited && !(client.limited && (client.limited.ingress || client.limited.egress))) {
include = false;
}
if (include && (
filter.all ||
('mac' in filter && client.mac.includes(filter.mac)) ||
('ip' in filter && client.ip && client.ip.includes(filter.ip)) ||
('hostname' in filter && client.hostname && client.hostname.toLowerCase().includes(filter.hostname)) ||
('name' in filter && client.name.toLowerCase().includes(filter.name)) ||
('wifi' in filter && filter.wifi && client.ssid) ||
('wired' in filter && filter.wired && !client.ssid) ||
('ssid' in filter && client.ssid.toLowerCase().includes(filter.ssid)) ||
('oui' in filter && client.oui && client.oui.toLowerCase().includes(filter.oui)) ||
('connection' in filter && client.connected && client.connected.device.name.toLowerCase().includes(filter.connection))
)) {
clients[m] = client;
}
}
return clients;
}
_getDeviceMacs() {
const dmacs = {};
DeviceInstanceManager.getAuthenticatedDevices().forEach(dev => {
const macs = dev.readKV(DeviceState.KEY_SYSTEM_MACADDRESS);
if (macs) {
Object.values(macs).forEach(mac => dmacs[mac] = true);
}
});
return dmacs;
}
toDB(mac) {
const m = this.mac[mac];
return {
_id: mac,
name: m.name,
firstSeen: m.firstSeen,
lastSeen: m.lastSeen
};
}
fromDB(mac, data) {
const m = this.mac[mac];
if (data) {
for (let k in data) {
if (k in m) {
m[k] = data[k];
}
}
}
}
}
module.exports = new ClientManager();