improved searching of active users
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user