Add device traffic counters and ambiguous MAC detection
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import crypto from 'node:crypto';
|
||||
import fs from 'node:fs';
|
||||
import net from 'node:net';
|
||||
import { HarborError } from '../../shared/errors.js';
|
||||
|
||||
const SCHEMA_VERSION = 1;
|
||||
@@ -100,6 +101,13 @@ export function createDeviceInventoryService({ store, observe, vendor = () => nu
|
||||
}
|
||||
const observedAt = result?.observedAt || now().toISOString();
|
||||
const observations = Array.isArray(result?.observations) ? result.observations : [];
|
||||
const ipsByMac = new Map();
|
||||
for (const observation of observations) {
|
||||
const mac = normalizeMac(observation.mac);
|
||||
if (!mac || !net.isIPv4(String(observation.ip || ''))) continue;
|
||||
if (!ipsByMac.has(mac)) ipsByMac.set(mac, new Set());
|
||||
ipsByMac.get(mac).add(String(observation.ip));
|
||||
}
|
||||
store.update((stored) => {
|
||||
const state = migrate(stored);
|
||||
const byMac = new Map(state.devices.map((device) => [device.mac, device]));
|
||||
@@ -122,7 +130,9 @@ export function createDeviceInventoryService({ store, observe, vendor = () => nu
|
||||
firstSeenAt: previous?.firstSeenAt || observation.observedAt || observedAt,
|
||||
lastSeenAt,
|
||||
source: 'neighbor',
|
||||
confidence: isPrivateMac(mac) ? 'medium' : 'high',
|
||||
confidence: ipsByMac.get(mac)?.size > 1
|
||||
? 'ambiguous'
|
||||
: isPrivateMac(mac) ? 'medium' : 'high',
|
||||
});
|
||||
}
|
||||
const cutoff = new Date(observedAt).getTime() - RETENTION_MS;
|
||||
|
||||
Reference in New Issue
Block a user