From 5c7e723bdc7d9f47d458089b79b10b50feb6b15e Mon Sep 17 00:00:00 2001 From: andrei-mihnea-cerbu Date: Fri, 14 Feb 2025 12:11:10 +0200 Subject: [PATCH] improved searching of active users --- Desktop App/src/helpers/network_scanner.ts | 7 +++++- Desktop App/src/network/udp/udp_client.ts | 26 +++++++++------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Desktop App/src/helpers/network_scanner.ts b/Desktop App/src/helpers/network_scanner.ts index cb8b66b..2d7a83b 100644 --- a/Desktop App/src/helpers/network_scanner.ts +++ b/Desktop App/src/helpers/network_scanner.ts @@ -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, diff --git a/Desktop App/src/network/udp/udp_client.ts b/Desktop App/src/network/udp/udp_client.ts index 618c0a1..c78e12a 100644 --- a/Desktop App/src/network/udp/udp_client.ts +++ b/Desktop App/src/network/udp/udp_client.ts @@ -32,21 +32,14 @@ export class UdpClient { async getTargetClients(heartbeatCode: string): Promise { 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 { - 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; } }