smartnetwork/ts/smartnetwork.classes.smartnetwork.ts

137 lines
3.4 KiB
TypeScript
Raw Normal View History

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 });
test.on('data', data => {
done.resolve(data);
});
test.on('error', err => {
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();
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
2019-09-06 16:26:32 +00:00
plugins.portscanner.checkPortStatus(port, domainPart, (err, status) => {
2019-04-16 08:21:11 +00:00
if (err) {
2019-04-17 22:04:02 +00:00
// console.log(err);
2019-04-17 18:05:07 +00:00
return done.resolve(false);
2019-04-16 08:21:11 +00:00
}
if (status === 'open') {
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-04-16 08:21:11 +00:00
}