Skip to content

Commit

Permalink
delete stales ips and add append ips for clients
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Apr 2, 2024
1 parent 7011efd commit 0b3f136
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dotenv from 'dotenv';
import { neigh } from 'ip-wrapper';
import { pingIP, arraysEqual, isMacAddress, isIp } from './utils.js';
import { arraysEqual, isMacAddress, isIp } from './utils.js';

dotenv.config();

Expand Down Expand Up @@ -61,27 +61,14 @@ async function updateClients() {
if (macAddresses.length === 0) return

const originalClientIps = client.ids.filter(id => isIp(id));
let ipsForClient = [...originalClientIps];
let ipsForClient = [];

macAddresses.forEach(mac => {
if (neighborIPsByMAC[mac]) {
ipsForClient = neighborIPsByMAC[mac];
ipsForClient = [...ipsForClient, ...neighborIPsByMAC[mac]];
}
});

const pingResults = await Promise.all(ipsForClient.map(ip => pingIP(ip)));
ipsForClient = ipsForClient.filter((ip, index) => {
const isAlive = pingResults[index];
if (isAlive) {
if (staleIPs[ip]) {
delete staleIPs[ip];
}
} else {
staleIPs[ip] = (staleIPs[ip] || 0) + 1;
}
return isAlive || (staleIPs[ip] <= 100);
});

const updatedIds = [...new Set([...ipsForClient])];

if (!arraysEqual(originalClientIps, updatedIds)) {
Expand Down

0 comments on commit 0b3f136

Please sign in to comment.