improved searching of active users
This commit is contained in:
@@ -105,14 +105,19 @@ export class NetworkScanner {
|
|||||||
if (this.ipLookupBusy) return
|
if (this.ipLookupBusy) return
|
||||||
this.ipLookupBusy = true
|
this.ipLookupBusy = true
|
||||||
|
|
||||||
|
const data = await this.db.read()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.log('IP Lookup running...', 'log', 'startUserIPLookup')
|
this.log('IP Lookup running...', 'log', 'startUserIPLookup')
|
||||||
const udpClient = new UdpClient(this.udpPort)
|
const udpClient = new UdpClient(this.udpPort)
|
||||||
const activeClients = await udpClient.getTargetClients(operationCodes.ARE_YOU_HUMAN)
|
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'
|
// Save the filtered IPs to 'users_ip'
|
||||||
await this.db.update((data) => {
|
await this.db.update((data) => {
|
||||||
data.network.usersInLan = activeClients.map((client) => ({
|
data.network.usersInLan = filteredClients.map((client) => ({
|
||||||
ip: client.ip,
|
ip: client.ip,
|
||||||
name: client.name,
|
name: client.name,
|
||||||
departmentId: client.departmentId,
|
departmentId: client.departmentId,
|
||||||
|
|||||||
@@ -32,21 +32,14 @@ export class UdpClient {
|
|||||||
async getTargetClients(heartbeatCode: string): Promise<any[]> {
|
async getTargetClients(heartbeatCode: string): Promise<any[]> {
|
||||||
const subnet = this.getSubnet()
|
const subnet = this.getSubnet()
|
||||||
const ipRange = this.getIPRange(subnet)
|
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)
|
const activeIps = await this.filterActiveIps(ipRange)
|
||||||
|
|
||||||
// Send heartbeat to each active IP and keep only those that respond with ALIVE
|
// Send heartbeat to each active IP and keep only those that respond with ALIVE
|
||||||
const aliveClients: any[] = []
|
const aliveClients: any[] = []
|
||||||
for (const ip of activeIps) {
|
for (const ip of activeIps) {
|
||||||
if (!localIPs.includes(ip)) {
|
const result = await this.sendHeartbeat(ip, heartbeatCode)
|
||||||
const result = await this.sendHeartbeat(ip, heartbeatCode)
|
if (result.found) {
|
||||||
if (result.found) {
|
aliveClients.push(result.data ? result.data : ip)
|
||||||
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
|
// Filter only active IPs by pinging each IP in the range
|
||||||
private async filterActiveIps(ipRange: string[]): Promise<string[]> {
|
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) {
|
for (const result of pingResults) {
|
||||||
if (result.alive) {
|
if (result.alive && !localIPs.includes(result.host)) {
|
||||||
activeIps.push(result.host)
|
activeIps.push(result.host);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return activeIps
|
return activeIps;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user