smartnetwork/ts/smartnetwork.classes.smartnetwork.ts

137 lines
3.7 KiB
TypeScript
Raw Normal View History

2019-04-16 08:21:11 +00:00
import * as plugins from './smartnetwork.plugins';
2021-04-28 13:41:55 +00:00
import { CloudflareSpeed } from './smartnetwork.classes.cloudflarespeed';
2019-04-16 08:21:11 +00:00
/**
* SmartNetwork simplifies actions within the network
*/
export class SmartNetwork {
2019-09-06 16:26:32 +00:00
/**
* get network speed
* @param measurementTime
*/
2021-04-13 13:05:47 +00:00
public async getSpeed() {
2021-04-28 13:41:55 +00:00
const cloudflareSpeedInstance = new CloudflareSpeed();
const test = await cloudflareSpeedInstance.speedTest();
2021-04-13 10:29:42 +00:00
return test;
2019-04-16 08:21:11 +00:00
}
2022-02-16 23:03:13 +00:00
public async ping(hostArg: string, timeoutArg: number = 500): Promise<boolean> {
2022-02-16 23:18:23 +00:00
if (process.getuid() !== 0) {
2022-02-16 23:03:13 +00:00
console.log('icmp not allowed for nonroot!');
return;
}
const result = await plugins.icmp.ping(hostArg, timeoutArg).catch();
if (result) {
return true;
} else {
return false;
}
}
2019-04-16 08:21:11 +00:00
/**
* returns a promise with a boolean answer
* note: false also resolves with false as argument
* @param port
*/
2021-04-13 10:09:39 +00:00
public async isLocalPortUnused(port: number): Promise<boolean> {
2019-04-16 08:21:11 +00:00
const doneIpV4 = plugins.smartpromise.defer<boolean>();
const doneIpV6 = plugins.smartpromise.defer<boolean>();
const net = await import('net'); // creates only one instance of net ;) even on multiple calls
// test IPv4 space
const ipv4Test = net.createServer();
2019-09-06 16:26:32 +00:00
ipv4Test.once('error', (err: any) => {
2019-04-16 08:21:11 +00:00
if (err.code !== 'EADDRINUSE') {
doneIpV4.resolve(false);
return;
}
doneIpV4.resolve(false);
});
2019-09-06 16:26:32 +00:00
ipv4Test.once('listening', () => {
2019-04-16 08:21:11 +00:00
ipv4Test.once('close', () => {
doneIpV4.resolve(true);
});
ipv4Test.close();
});
ipv4Test.listen(port, '0.0.0.0');
await doneIpV4.promise;
// test IPv6 space
2019-09-06 16:26:32 +00:00
const ipv6Test = net.createServer();
2020-08-12 16:30:17 +00:00
ipv6Test.once('error', function (err: any) {
2019-04-16 08:21:11 +00:00
if (err.code !== 'EADDRINUSE') {
doneIpV6.resolve(false);
return;
}
doneIpV6.resolve(false);
});
2019-09-06 16:26:32 +00:00
ipv6Test.once('listening', () => {
ipv6Test.once('close', () => {
2019-04-16 08:21:11 +00:00
doneIpV6.resolve(true);
});
2019-09-06 16:26:32 +00:00
ipv6Test.close();
2019-04-16 08:21:11 +00:00
});
2019-09-06 16:26:32 +00:00
ipv6Test.listen(port, '::');
2019-04-16 08:21:11 +00:00
// lets wait for the result
const resultIpV4 = await doneIpV4.promise;
const resultIpV6 = await doneIpV6.promise;
const result = resultIpV4 === true && resultIpV6 === true;
return result;
}
/**
* checks wether a remote port is available
* @param domainArg
*/
public async isRemotePortAvailable(domainArg: string, portArg?: number): Promise<boolean> {
const done = plugins.smartpromise.defer<boolean>();
const domainPart = domainArg.split(':')[0];
2019-04-16 08:23:11 +00:00
const port = portArg ? portArg : parseInt(domainArg.split(':')[1], 10);
2019-04-16 08:21:11 +00:00
2022-02-16 22:28:12 +00:00
plugins.isopen(domainPart, port, (response: any) => {
2020-08-12 16:29:03 +00:00
console.log(response);
if (response[port.toString()].isOpen) {
2019-04-16 08:21:11 +00:00
done.resolve(true);
} else {
2019-04-17 18:05:07 +00:00
done.resolve(false);
2019-04-16 08:21:11 +00:00
}
2019-09-06 16:26:32 +00:00
});
2019-04-16 08:21:11 +00:00
const result = await done.promise;
return result;
}
2019-09-06 16:26:32 +00:00
2019-09-08 12:59:47 +00:00
public async getGateways() {
const result = plugins.os.networkInterfaces();
2019-09-06 16:26:32 +00:00
return result;
}
2019-09-08 14:15:10 +00:00
2019-11-19 23:00:37 +00:00
public async getDefaultGateway(): Promise<{
ipv4: plugins.os.NetworkInterfaceInfo;
ipv6: plugins.os.NetworkInterfaceInfo;
}> {
2019-09-08 14:15:10 +00:00
const defaultGatewayName = await plugins.systeminformation.networkInterfaceDefault();
2019-09-08 14:24:59 +00:00
if (!defaultGatewayName) {
console.log('Cannot determine default gateway');
return null;
}
2019-09-08 14:15:10 +00:00
const gateways = await this.getGateways();
const defaultGateway = gateways[defaultGatewayName];
return {
ipv4: defaultGateway[0],
2020-08-12 16:30:17 +00:00
ipv6: defaultGateway[1],
2019-09-08 14:15:10 +00:00
};
}
2019-11-23 16:07:04 +00:00
2020-08-12 16:30:17 +00:00
public async getPublicIps() {
2019-11-23 16:07:04 +00:00
return {
v4: await plugins.publicIp.v4({
timeout: 1000,
2020-08-12 16:30:17 +00:00
onlyHttps: true,
}),
2019-11-23 16:07:04 +00:00
};
}
2019-04-16 08:21:11 +00:00
}