Compare commits

...

12 Commits

Author SHA1 Message Date
695d047200 2.0.12 2022-02-17 00:03:13 +01:00
c308589d28 fix(core): update 2022-02-17 00:03:13 +01:00
068177b09d 2.0.11 2022-02-16 23:28:12 +01:00
4a299cf3cb fix(core): update 2022-02-16 23:28:12 +01:00
e5c37b1801 2.0.10 2021-04-29 15:12:05 +00:00
5be0586790 fix(core): update 2021-04-29 15:12:05 +00:00
f5e5297d47 2.0.9 2021-04-28 14:32:56 +00:00
718fada493 fix(core): update 2021-04-28 14:32:56 +00:00
a42b1b48b5 2.0.8 2021-04-28 14:31:31 +00:00
5ec50975f3 fix(core): update 2021-04-28 14:31:30 +00:00
ad222abb6a 2.0.7 2021-04-28 14:27:23 +00:00
b29e13b162 fix(core): update 2021-04-28 14:27:22 +00:00
11 changed files with 19266 additions and 4902 deletions

4
.snyk
View File

@ -1,4 +0,0 @@
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
version: v1.13.5
ignore: {}
patch: {}

23944
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartnetwork",
"version": "2.0.6",
"version": "2.0.12",
"private": false,
"description": "network diagnostics",
"main": "dist_ts/index.js",
@ -12,20 +12,20 @@
"build": "(tsbuild --web)"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.25",
"@gitzone/tstest": "^1.0.52",
"@pushrocks/tapbundle": "^3.2.14",
"@gitzone/tsbuild": "^2.1.29",
"@gitzone/tstest": "^1.0.64",
"@pushrocks/tapbundle": "^4.0.7",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
},
"dependencies": {
"@pushrocks/smartpromise": "^3.1.3",
"@pushrocks/smartpromise": "^3.1.6",
"@pushrocks/smartstring": "^3.0.24",
"@types/default-gateway": "^3.0.1",
"icmp": "^2.0.1",
"isopen": "^1.3.0",
"public-ip": "^4.0.3",
"speedtest-net": "^2.1.1",
"systeminformation": "^5.6.12"
"public-ip": "^4.0.4",
"systeminformation": "^5.11.3"
},
"files": [
"ts/**/*",

16
test/test.ping.ts Normal file
View File

@ -0,0 +1,16 @@
import {tap, expect, expectAsync} from '@pushrocks/tapbundle';
import * as smartnetwork from '../ts';
let testSmartnetwork: smartnetwork.SmartNetwork;
tap.test('should create a vlid instance of SmartNetwork', async () => {
testSmartnetwork = new smartnetwork.SmartNetwork();
expect(testSmartnetwork).toBeInstanceOf(smartnetwork.SmartNetwork);
})
tap.test('should send a ping to Google', async () => {
expectAsync(testSmartnetwork.ping('https://lossless.com')).toBeTrue();
})
tap.start();

View File

@ -1,26 +1,27 @@
import { expect, tap } from '@pushrocks/tapbundle';
import { expect, expectAsync, tap } from '@pushrocks/tapbundle';
import * as smartnetwork from '../ts/index';
let testSmartNetwork: smartnetwork.SmartNetwork;
tap.test('should create a valid instance of SmartNetwork', async () => {
testSmartNetwork = new smartnetwork.SmartNetwork();
expect(testSmartNetwork).to.be.instanceOf(smartnetwork.SmartNetwork);
expect(testSmartNetwork).toBeInstanceOf(smartnetwork.SmartNetwork);
});
tap.test('should perform a speedtest', async () => {
const result = await testSmartNetwork.getSpeed();
console.log(result);
console.log(`Download speed for this instance is ${result.downloadSpeed}`);
console.log(`Upload speed for this instance is ${result.uploadSpeed}`);
});
tap.test('should determine wether a port is free', async () => {
await expect(testSmartNetwork.isLocalPortUnused(8080)).to.eventually.be.true;
await expectAsync(testSmartNetwork.isLocalPortUnused(8080)).toBeTrue();
});
tap.test('should scan a port', async () => {
await expect(testSmartNetwork.isRemotePortAvailable('lossless.com:443')).to.eventually.be.true;
await expect(testSmartNetwork.isRemotePortAvailable('lossless.com', 443)).to.be.eventually.true;
// await expect(testSmartNetwork.isRemotePortAvailable('lossless.com:444')).to.eventually.be.false;
await expectAsync(testSmartNetwork.isRemotePortAvailable('lossless.com:443')).toBeTrue();
await expectAsync(testSmartNetwork.isRemotePortAvailable('lossless.com', 443)).toBeTrue();
await expectAsync(testSmartNetwork.isRemotePortAvailable('lossless.com:444')).toBeFalse();
});
tap.test('should get gateways', async () => {

View File

@ -1,4 +1,4 @@
export function average(values) {
export function average(values: number[]) {
let total = 0;
for (let i = 0; i < values.length; i += 1) {
@ -8,7 +8,7 @@ export function average(values) {
return total / values.length;
}
export function median(values) {
export function median(values: number[]) {
const half = Math.floor(values.length / 2);
values.sort((a, b) => a - b);
@ -18,7 +18,7 @@ export function median(values) {
return (values[half - 1] + values[half]) / 2;
}
export function quartile(values, percentile) {
export function quartile(values: number[], percentile: number) {
values.sort((a, b) => a - b);
const pos = (values.length - 1) * percentile;
const base = Math.floor(pos);
@ -31,7 +31,7 @@ export function quartile(values, percentile) {
return values[base];
}
export function jitter(values) {
export function jitter(values: number[]) {
// Average distance between consecutive latency measurements...
let jitters = [];

View File

@ -1 +1 @@
export * from './smartnetwork.classes.smartnetwork';
export * from './smartnetwork.classes.smartnetwork';

View File

@ -6,18 +6,36 @@ export class CloudflareSpeed {
public async speedTest() {
const latency = await this.measureLatency();
const serverLocations = await this.fetchServerLocations();
const cgiData = await this.fetchCfCdnCgiTrace();
// lets test the download speed
const testDown1 = await this.measureDownload(101000, 10);
const testDown2 = await this.measureDownload(1001000, 8);
const testDown3 = await this.measureDownload(10001000, 6);
const testDown4 = await this.measureDownload(25001000, 4);
const testDown5 = await this.measureDownload(100001000, 1);
const downloadTests = [...testDown1, ...testDown2, ...testDown3, ...testDown4, ...testDown5];
const speedDownload = stats.quartile(downloadTests, 0.9).toFixed(2);
// lets test the upload speed
const testUp1 = await this.measureUpload(11000, 10);
const testUp2 = await this.measureUpload(101000, 10);
const testUp3 = await this.measureUpload(1001000, 8);
const uploadTests = [...testUp1, ...testUp2, ...testUp3];
const speedUpload = stats.quartile(uploadTests, 0.9).toFixed(2);
return {
...latency,
ip: cgiData.ip,
serverLocation: {
shortId: cgiData.colo,
name: serverLocations[cgiData.colo],
availableLocations: serverLocations,
}
availableLocations: serverLocations,
},
downloadSpeed: speedDownload,
uploadSpeed: speedUpload,
};
}
@ -42,19 +60,57 @@ export class CloudflareSpeed {
averageTime: stats.average(measurements),
medianTime: stats.median(measurements),
jitter: stats.jitter(measurements),
}
;
};
}
public async fetchServerLocations(): Promise<{[key: string]: string}> {
public async measureDownload(bytes: number, iterations: number) {
const measurements: number[] = [];
for (let i = 0; i < iterations; i += 1) {
await this.download(bytes).then(
async (response) => {
const transferTime = response[5] - response[4];
measurements.push(await this.measureSpeed(bytes, transferTime));
},
(error) => {
console.log(`Error: ${error}`);
}
);
}
return measurements;
}
public async measureUpload(bytes: number, iterations: number) {
const measurements: number[] = [];
for (let i = 0; i < iterations; i += 1) {
await this.upload(bytes).then(
async (response) => {
const transferTime = response[6];
measurements.push(await this.measureSpeed(bytes, transferTime));
},
(error) => {
console.log(`Error: ${error}`);
}
);
}
return measurements;
}
public async measureSpeed(bytes: number, duration: number) {
return (bytes * 8) / (duration / 1000) / 1e6;
}
public async fetchServerLocations(): Promise<{ [key: string]: string }> {
const res = JSON.parse(await this.get('speed.cloudflare.com', '/locations'));
return res.reduce((data, { iata, city }) => {
return res.reduce((data: any, optionsArg: { iata: string, city: string}) => {
// Bypass prettier "no-assign-param" rules
const data1 = data;
data1[iata] = city;
data1[optionsArg.iata] = optionsArg.city;
return data1;
}, {});
}
@ -68,7 +124,7 @@ export class CloudflareSpeed {
method: 'GET',
},
(res) => {
const body = [];
const body: Array<Buffer> = [];
res.on('data', (chunk) => {
body.push(chunk);
});
@ -89,7 +145,7 @@ export class CloudflareSpeed {
});
}
public async download(bytes) {
public async download(bytes: number) {
const options = {
hostname: 'speed.cloudflare.com',
path: `/__down?bytes=${bytes}`,
@ -99,13 +155,27 @@ export class CloudflareSpeed {
return this.request(options);
}
public async request(options, data = '') {
let started;
let dnsLookup;
let tcpHandshake;
let sslHandshake;
let ttfb;
let ended;
public async upload(bytes: number) {
const data = '0'.repeat(bytes);
const options = {
hostname: 'speed.cloudflare.com',
path: '/__up',
method: 'POST',
headers: {
'Content-Length': Buffer.byteLength(data),
},
};
return this.request(options, data);
}
public async request(options: plugins.https.RequestOptions, data = ''): Promise<number[]> {
let started: number;
let dnsLookup: number;
let tcpHandshake : number;
let sslHandshake: number;
let ttfb: number;
let ended: number;
return new Promise((resolve, reject) => {
started = plugins.perfHooks.performance.now();
@ -150,21 +220,21 @@ export class CloudflareSpeed {
}
public async fetchCfCdnCgiTrace(): Promise<{
fl: string,
h: string,
ip: string,
ts: string,
visit_scheme: string,
uag: string,
colo: string,
http: string,
loc: string,
tls: string,
sni: string,
warp: string,
gateway: string,
fl: string;
h: string;
ip: string;
ts: string;
visit_scheme: string;
uag: string;
colo: string;
http: string;
loc: string;
tls: string;
sni: string;
warp: string;
gateway: string;
}> {
const parseCfCdnCgiTrace = (text) =>
const parseCfCdnCgiTrace = (text: string) =>
text
.split('\n')
.map((i) => {
@ -172,7 +242,7 @@ export class CloudflareSpeed {
return [j[0], j[1]];
})
.reduce((data, [k, v]) => {
.reduce((data: any, [k, v]) => {
if (v === undefined) return data;
// Bypass prettier "no-assign-param" rules

View File

@ -16,6 +16,19 @@ export class SmartNetwork {
return test;
}
public async ping(hostArg: string, timeoutArg: number = 500): Promise<boolean> {
if (process.getuid() !== 0 ) {
console.log('icmp not allowed for nonroot!');
return;
}
const result = await plugins.icmp.ping(hostArg, timeoutArg).catch();
if (result) {
return true;
} else {
return false;
}
}
/**
* returns a promise with a boolean answer
* note: false also resolves with false as argument
@ -78,7 +91,7 @@ export class SmartNetwork {
const domainPart = domainArg.split(':')[0];
const port = portArg ? portArg : parseInt(domainArg.split(':')[1], 10);
plugins.isopen(domainPart, port, (response) => {
plugins.isopen(domainPart, port, (response: any) => {
console.log(response);
if (response[port.toString()].isOpen) {
done.resolve(true);

View File

@ -12,8 +12,9 @@ import * as smartstring from '@pushrocks/smartstring';
export { smartpromise, smartstring };
// @third party scope
import isopen from 'isopen';
import publicIp from 'public-ip';
const isopen = require('isopen');
const icmp = require('icmp');
import * as publicIp from 'public-ip';
import * as systeminformation from 'systeminformation';
export { isopen, publicIp, systeminformation };
export { isopen, icmp, publicIp, systeminformation };

5
ts/tsconfig.json Normal file
View File

@ -0,0 +1,5 @@
{
"compilerOptions": {
"noImplicitAny": true
}
}