optimized networkDefaultInterface() detection, fixed network operstate MacOS
This commit is contained in:
+27
-32
@@ -37,38 +37,34 @@ let isIpAvailable;
|
||||
|
||||
function getDefaultNetworkInterface() {
|
||||
|
||||
if (!_default_iface) {
|
||||
let ifacename = '';
|
||||
let scopeid = 9999;
|
||||
if (_linux || _darwin || _freebsd || _openbsd) {
|
||||
let cmd = '';
|
||||
if (_linux) cmd = 'route 2>/dev/null | grep default | awk "{print $8}"';
|
||||
if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk "{print $2}"';
|
||||
if (_freebsd || _openbsd) cmd = 'route get 0.0.0.0 | grep interface:';
|
||||
let result = execSync(cmd);
|
||||
ifacename = result.toString().split('\n')[0];
|
||||
if (ifacename.indexOf(':') > -1) {
|
||||
ifacename = ifacename.split(':')[1].trim();
|
||||
}
|
||||
}
|
||||
let ifaces = os.networkInterfaces();
|
||||
let ifacename = '';
|
||||
let scopeid = 9999;
|
||||
|
||||
if (!ifacename) { // fallback - "first" external interface (sorted by scopeid)
|
||||
|
||||
let ifaces = os.networkInterfaces();
|
||||
|
||||
for (let dev in ifaces) {
|
||||
if (ifaces.hasOwnProperty(dev)) {
|
||||
ifaces[dev].forEach(function (details) {
|
||||
if (details && details.internal === false && details.scopeid && details.scopeid < scopeid) {
|
||||
ifacename = dev;
|
||||
scopeid = details.scopeid;
|
||||
}
|
||||
});
|
||||
// fallback - "first" external interface (sorted by scopeid)
|
||||
for (let dev in ifaces) {
|
||||
if (ifaces.hasOwnProperty(dev)) {
|
||||
ifaces[dev].forEach(function (details) {
|
||||
if (details && details.internal === false && details.scopeid && details.scopeid < scopeid) {
|
||||
ifacename = dev;
|
||||
scopeid = details.scopeid;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (ifacename) _default_iface = ifacename;
|
||||
}
|
||||
if (_linux || _darwin || _freebsd || _openbsd) {
|
||||
let cmd = '';
|
||||
if (_linux) cmd = 'route 2>/dev/null | grep default | awk "{print $8}"';
|
||||
if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk "{print $2}"';
|
||||
if (_freebsd || _openbsd) cmd = 'route get 0.0.0.0 | grep interface:';
|
||||
let result = execSync(cmd);
|
||||
ifacename = result.toString().split('\n')[0];
|
||||
if (ifacename.indexOf(':') > -1) {
|
||||
ifacename = ifacename.split(':')[1].trim();
|
||||
}
|
||||
}
|
||||
|
||||
if (ifacename) _default_iface = ifacename;
|
||||
return _default_iface;
|
||||
}
|
||||
|
||||
@@ -84,7 +80,7 @@ function getMacAddresses() {
|
||||
isIpAvailable = true;
|
||||
} else {
|
||||
isIpAvailable = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
const cmd = 'export LC_ALL=C; /sbin/' + ((isIpAvailable) ? 'ip link show up' : 'ifconfig') + '; unset LC_ALL';
|
||||
let res = execSync(cmd);
|
||||
@@ -277,8 +273,7 @@ function networkStats(iface, callback) {
|
||||
return new Promise((resolve) => {
|
||||
process.nextTick(() => {
|
||||
|
||||
_default_iface = _default_iface || getDefaultNetworkInterface();
|
||||
iface = iface || _default_iface; // (_darwin ? 'en0' : 'eth0');
|
||||
iface = iface || getDefaultNetworkInterface();
|
||||
|
||||
let result = {
|
||||
iface: iface,
|
||||
@@ -357,7 +352,7 @@ function networkStats(iface, callback) {
|
||||
rx = parseInt(stats[6]);
|
||||
tx = parseInt(stats[9]);
|
||||
|
||||
result = calcNetworkSpeed(iface, rx, tx, operstate);
|
||||
result = calcNetworkSpeed(iface, rx, tx, result.operstate);
|
||||
}
|
||||
}
|
||||
if (callback) { callback(result); }
|
||||
|
||||
Reference in New Issue
Block a user