-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
73 lines (66 loc) · 3.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const os = require('os');
const exec = require('child_process').exec;
class NetworkBandwithMonitor {
constructor(interface_ = os.platform() === 'darwin' ? 'en0' : 'eth0', interval_ = 1000, callback) {
this.interface = interface_;
this.interval = interval_;
this.oldRx = 0;
this.oldTx = 0;
this.isStart = true;
}
stop() {
this.isStart = false;
}
async start() {
while (this.isStart) {
await new Promise(resolve => setTimeout(resolve, this.interval));
const { rx, tx } = await this.getCurrentSpeed();
const rxMbps = ((rx - this.oldRx) * 8 / (this.interval / 1000)) / 1000 / 1000;
const txMbps = ((tx - this.oldTx) * 8 / (this.interval / 1000)) / 1000 / 1000;
const rxKbps = ((rx - this.oldRx) * 8 / (this.interval / 1000)) / 1000;
const txKbps = ((tx - this.oldTx) * 8 / (this.interval / 1000)) / 1000;
const rxbps = ((rx - this.oldRx) * 8 / (this.interval / 1000));
const txbps = ((tx - this.oldTx) * 8 / (this.interval / 1000));
const object = { uplink: { bps: `${rxbps.toFixed(2)}`, kbps: `${rxKbps.toFixed(2)}`, mbps: `${rxMbps.toFixed(2)}` }, downlink: { bps: `${txbps.toFixed(2)}`, kbps: `${txKbps.toFixed(2)}`, mbps: `${txMbps.toFixed(2)}` } }
this.callback(object);
this.oldRx = rx;
this.oldTx = tx;
if (typeof this.callback === 'function') {
this.callback(object);
}
}
}
registerCallback(cb) {
this.callback = cb;
}
async getCurrentSpeed() {
return new Promise((resolve, reject) => {
let command = '';
if (os.platform() === 'darwin') {
command = `netstat -ibI ${this.interface} | grep -E "^[a-z]" | awk '{print $10, $7}' | tail -n 1`;
} else if (os.platform() === 'win32' || os.platform() === 'win64') {
command = `netstat /e | findstr "Bytes"`;
} else {
command = `cat /sys/class/net/${this.interface}/statistics/rx_bytes && cat /sys/class/net/${this.interface}/statistics/tx_bytes`;
}
exec(command, (err, stdout) => {
if (err) {
reject(err);
} else {
if (os.platform() === 'win32' || os.platform() === 'win64') {
const output = stdout.trim().replace(/\t|\s{2,}/g, ' ').replace(/Bytes/g, '').trim();
const [rx, tx] = output.split(' ').map(n => parseInt(n));
resolve({ rx, tx });
} else if (os.platform() === 'darwin') {
const [rx, tx] = stdout.trim().split(' ').map(n => parseInt(n));
resolve({ rx, tx });
} else {
const [rx, tx] = stdout.trim().split(/\r?\n/).map(n => parseInt(n));
resolve({ rx, tx });
}
}
});
});
}
}
module.exports = NetworkBandwithMonitor;