improved searching of active users

This commit is contained in:
andrei-mihnea-cerbu
2025-02-14 12:11:10 +02:00
parent 73f9c6b526
commit 5c7e723bdc
2 changed files with 16 additions and 17 deletions
+6 -1
View File
@@ -105,14 +105,19 @@ export class NetworkScanner {
if (this.ipLookupBusy) return
this.ipLookupBusy = true
const data = await this.db.read()
try {
this.log('IP Lookup running...', 'log', 'startUserIPLookup')
const udpClient = new UdpClient(this.udpPort)
const activeClients = await udpClient.getTargetClients(operationCodes.ARE_YOU_HUMAN)
const filteredClients = activeClients.filter(
(client) => client.name != data.app_config.user_info.name,
)
// Save the filtered IPs to 'users_ip'
await this.db.update((data) => {
data.network.usersInLan = activeClients.map((client) => ({
data.network.usersInLan = filteredClients.map((client) => ({
ip: client.ip,
name: client.name,
departmentId: client.departmentId,
+10 -16
View File
@@ -32,21 +32,14 @@ export class UdpClient {
async getTargetClients(heartbeatCode: string): Promise<any[]> {
const subnet = this.getSubnet()
const ipRange = this.getIPRange(subnet)
// Get local machine's IP addresses to exclude
const localIPs = this.getLocalIPs()
// First, filter active IPs that respond to ping
const activeIps = await this.filterActiveIps(ipRange)
// Send heartbeat to each active IP and keep only those that respond with ALIVE
const aliveClients: any[] = []
for (const ip of activeIps) {
if (!localIPs.includes(ip)) {
const result = await this.sendHeartbeat(ip, heartbeatCode)
if (result.found) {
aliveClients.push(result.data ? result.data : ip)
}
const result = await this.sendHeartbeat(ip, heartbeatCode)
if (result.found) {
aliveClients.push(result.data ? result.data : ip)
}
}
@@ -134,18 +127,19 @@ export class UdpClient {
// Filter only active IPs by pinging each IP in the range
private async filterActiveIps(ipRange: string[]): Promise<string[]> {
const activeIps: string[] = []
const activeIps: string[] = [];
const localIPs = this.getLocalIPs(); // Get local machine's IPs to exclude
const pingPromises = ipRange.map((ip) => ping.promise.probe(ip, { timeout: 1 }))
const pingPromises = ipRange.map((ip) => ping.promise.probe(ip, { timeout: 1 }));
const pingResults = await Promise.all(pingPromises)
const pingResults = await Promise.all(pingPromises);
for (const result of pingResults) {
if (result.alive) {
activeIps.push(result.host)
if (result.alive && !localIPs.includes(result.host)) {
activeIps.push(result.host);
}
}
return activeIps
return activeIps;
}
}