2019-04-16 08:21:11 +00:00
|
|
|
import * as plugins from './smartnetwork.plugins';
|
|
|
|
|
2019-04-17 18:05:07 +00:00
|
|
|
export interface ISpeedtestData {
|
2019-04-16 08:21:11 +00:00
|
|
|
speeds: {
|
|
|
|
download: number;
|
|
|
|
upload: number;
|
|
|
|
originalDownload: number;
|
|
|
|
originalUpload: number;
|
|
|
|
};
|
|
|
|
client: {
|
|
|
|
ip: string;
|
|
|
|
lat: number;
|
|
|
|
lon: number;
|
|
|
|
isp: string;
|
|
|
|
isprating: string;
|
|
|
|
rating: number;
|
|
|
|
ispdlavg: number;
|
|
|
|
ispulavg: number;
|
|
|
|
};
|
|
|
|
server: {
|
|
|
|
host: string;
|
|
|
|
lat: number;
|
|
|
|
lon: number;
|
|
|
|
location: string;
|
|
|
|
country: string;
|
|
|
|
cc: string;
|
|
|
|
sponsor: string;
|
|
|
|
distance: number;
|
|
|
|
distanceMi: number;
|
|
|
|
ping: number;
|
|
|
|
id: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SmartNetwork simplifies actions within the network
|
|
|
|
*/
|
|
|
|
export class SmartNetwork {
|
2019-09-06 16:26:32 +00:00
|
|
|
/**
|
|
|
|
* get network speed
|
|
|
|
* @param measurementTime
|
|
|
|
*/
|
|
|
|
public async getSpeed(measurementTime = 5000): Promise<ISpeedtestData> {
|
|
|
|
const done = plugins.smartpromise.defer<ISpeedtestData>();
|
2019-04-16 08:21:11 +00:00
|
|
|
const test = plugins.speedtestNet({ maxTime: measurementTime });
|
2020-08-12 16:30:17 +00:00
|
|
|
test.on('data', (data) => {
|
2019-04-16 08:21:11 +00:00
|
|
|
done.resolve(data);
|
|
|
|
});
|
2020-08-12 16:30:17 +00:00
|
|
|
test.on('error', (err) => {
|
2019-04-16 08:21:11 +00:00
|
|
|
done.reject(err);
|
|
|
|
});
|
|
|
|
return await done.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a promise with a boolean answer
|
|
|
|
* note: false also resolves with false as argument
|
|
|
|
* @param port
|
|
|
|
*/
|
2019-09-06 16:26:32 +00:00
|
|
|
public async isLocalPortAvailable(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
|
|
|
|
2020-08-12 16:29:03 +00:00
|
|
|
plugins.isopen(domainPart, port, (response) => {
|
|
|
|
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
|
|
|
}
|