I wrote a method that should return a list of devices connected to the network that the client has.
export const connectedDevicesCore = (vpnId: string, vpnAuthToken: string) =>
Service.listVPNConnectionsCore(vpnId, vpnAuthToken).then(vpnConnectedDevices => {
let listPromises: Promise<any>[] = [];
let listDevices: IDeviceData[] = [];
vpnConnectedDevices.data.forEach((member: IZeroTierMember) => {
listPromises.push(apiUtils.ApiGet(`http://${member.config.ipAssignments[0]}:5000/api/Main`).then((response: any) => {
let device: IDeviceData = response.data;
device.IPVpn = member.config.ipAssignments[0];
listDevices.push(device);
}).catch(() => {
let device: IDeviceData = IDeviceExample;
device.IPVpn = member.config.ipAssignments[0];
device.DeviceName = member.name;
device.Status = 'Have no client';
listDevices.push(device);
}));
});
return Promise.allSettled(listPromises).then(() => listDevices);
});
ApiGet method:
export const ApiGet = (apiString: string, authToken: string | null = null) => {
let config = { headers: { Authorization: `Bearer ${authToken}` } };
let promise = axios.get(apiString, authToken != null ? config : undefined);
return promise
};
The idea was that if the client responded to the request, then the data that he transferred should be added to the array with all devices, if the requested client does not respond (i.e., is absent) Add a device with the same IP and information that he is without a client.
The problem is that writing data to the array is very strange, not the way it always is.
Example 1: There are 3 devices on the network, each without a client.
- When the first request returns the "ETIMEDOUT" error, listDevices looks like this:
[ { DeviceName: 'HP Server',
ID: 0,
IP: '',
IPVpn: '192.168.193.227',
MAC_Adress: '',
Status: 'Have no client',
AudioOutputs: [] } ]
- When the second request returns the "ETIMEDOUT" error, listDevices looks like this:
[ { DeviceName: 'Huawei P Smart 2021',
ID: 0,
IP: '',
IPVpn: '192.168.193.64',
MAC_Adress: '',
Status: 'Have no client',
AudioOutputs: [] },
{ DeviceName: 'Huawei P Smart 2021',
ID: 0,
IP: '',
IPVpn: '192.168.193.64',
MAC_Adress: '',
Status: 'Have no client',
AudioOutputs: [] } ]
- When the third request returns the "ETIMEDOUT" error, listDevices looks like this:
[ { DeviceName: 'Test Client',
ID: 0,
IP: '',
IPVpn: '192.168.193.147',
MAC_Adress: '',
Status: 'Have no client',
AudioOutputs: [] },
{ DeviceName: 'Test Client',
ID: 0,
IP: '',
IPVpn: '192.168.193.147',
MAC_Adress: '',
Status: 'Have no client',
AudioOutputs: [] },
{ DeviceName: 'Test Client',
ID: 0,
IP: '',
IPVpn: '192.168.193.147',
MAC_Adress: '',
Status: 'Have no client',
AudioOutputs: [] } ]
Example 2: There are 3 devices on the network, only the first device has a client.
- The first request writes the data returned by the client to listDevices.
- The second request behaves as in example 1, only it does not overwrite the response of the first client.
- The third request behaves as in example 1, only it does not overwrite the response of the first client.
That is, for example 2, the final answer will look like this:
[ { DeviceName: 'Test Client',
ID: 0,
IP: '192.168.8.3',
IPVpn: '192.168.193.147',
MAC_Adress: 'C0B883046C3F',
Status: 'Succes',
AudioOutputs: [...{sth: "sth"}...] },
{ DeviceName: 'Huawei P Smart 2021',
ID: 0,
IP: '',
IPVpn: '192.168.193.64',
MAC_Adress: '',
Status: 'Have no client',
AudioOutputs: [] },
{ DeviceName: 'Huawei P Smart 2021',
ID: 0,
IP: '',
IPVpn: '192.168.193.64',
MAC_Adress: '',
Status: 'Have no client',
AudioOutputs: [] } ]
I tried many variations of this code, keeping the logical idea, but alas, it did not help in any way.
This code runs on Node.js 10.23.0 + Typescript 4.2.3
Any ideas what I'm doing wrong?