networkInterfaces() fixed caching issue

This commit is contained in:
Sebastian Hildebrandt
2020-06-06 15:48:39 +02:00
parent 2b038e8fcd
commit 40699b8047
4 changed files with 33 additions and 10 deletions
+25 -8
View File
@@ -89,7 +89,21 @@ function getDefaultNetworkInterface() {
}
}
}
if (_linux || _darwin || _freebsd || _openbsd || _netbsd || _sunos) {
if (_linux) {
let cmd = 'ip route 2> /dev/null | grep default';
let result = execSync(cmd);
let parts = result.toString().split('\n')[0].split(/\s+/);
if (parts[0] === 'none' && parts[5]) {
ifacename = parts[5];
} else if (parts[4]) {
ifacename = parts[4];
}
if (ifacename.indexOf(':') > -1) {
ifacename = ifacename.split(':')[1].trim();
}
}
if (_darwin || _freebsd || _openbsd || _netbsd || _sunos) {
let cmd = '';
if (_linux) cmd = 'ip route 2> /dev/null | grep default | awk \'{print $5}\'';
if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk \'{print $2}\'';
@@ -663,8 +677,12 @@ function testVirtualNic(iface, ifaceName, mac) {
} else return false;
}
function networkInterfaces(callback) {
function networkInterfaces(callback, rescan = true) {
if (typeof callback === "boolean") {
rescan = callback;
callback = null;
}
return new Promise((resolve) => {
process.nextTick(() => {
let ifaces = os.networkInterfaces();
@@ -733,7 +751,7 @@ function networkInterfaces(callback) {
if (callback) { callback(result); }
resolve(result);
} else {
if (JSON.stringify(ifaces) === JSON.stringify(_ifaces)) {
if ((JSON.stringify(ifaces) === JSON.stringify(_ifaces)) && !rescan) {
// no changes - just return object
result = _networkInterfaces;
@@ -963,7 +981,7 @@ function networkStats(ifaces, callback) {
const workload = [];
if (ifacesArray.length && ifacesArray[0].trim() === '*') {
ifacesArray = [];
networkInterfaces().then(allIFaces => {
networkInterfaces(false).then(allIFaces => {
for (let iface of allIFaces) {
ifacesArray.push(iface.iface);
}
@@ -1137,7 +1155,7 @@ function networkStatsSingle(iface) {
}
// Network Interfaces
networkInterfaces().then(interfaces => {
networkInterfaces(false).then(interfaces => {
// get bytes sent, received from perfData by name
rx_bytes = 0;
tx_bytes = 0;
@@ -1147,8 +1165,8 @@ function networkStatsSingle(iface) {
det.mac.toLowerCase() === ifaceSanitized.toLowerCase() ||
det.ip4.toLowerCase() === ifaceSanitized.toLowerCase() ||
det.ip6.toLowerCase() === ifaceSanitized.toLowerCase() ||
(det.ifaceName.replace(/[()\[\] ]+/g, '').toLowerCase() === ifaceSanitized.replace(/[()\[\] ]+/g, '').toLowerCase()) &&
det.ifaceName.replace(/[()\[\] ]+/g, '').toLowerCase() === detail.name)) {
det.ifaceName.replace(/[()\[\] ]+/g, '').toLowerCase() === ifaceSanitized.replace(/[()\[\] ]+/g, '').toLowerCase()) &&
(det.ifaceName.replace(/[()\[\] ]+/g, '').toLowerCase() === detail.name)) {
ifaceName = det.iface;
rx_bytes = detail.rx_bytes;
rx_dropped = detail.rx_dropped;
@@ -1160,7 +1178,6 @@ function networkStatsSingle(iface) {
}
});
});
if (rx_bytes && tx_bytes) {
result = calcNetworkSpeed(ifaceName, parseInt(rx_bytes), parseInt(tx_bytes), operstate, rx_dropped, rx_errors, tx_dropped, tx_errors);
}