optimized networkDefaultInterface() detection, fixed network operstate MacOS

This commit is contained in:
Sebastian Hildebrandt 2018-03-25 11:07:27 +02:00
parent 6288330437
commit d343eb4e39
3 changed files with 31 additions and 66 deletions

View File

@ -100,6 +100,7 @@ Other changes
| Version | Date | Comment |
| -------------- | -------------- | -------- |
| 3.37.8 | 2018-03-25 | optimized `networkDefaultInterface()` detection, fixed network `operstate` MacOS |
| 3.37.7 | 2018-03-13 | celebrating 4th birthday |
| 3.37.6 | 2018-03-12 | updated docs: fixed `diskLayout`and `mamlayout` |
| 3.37.5 | 2018-03-12 | added support for `ip` instead of `ifconfig` |

View File

@ -9,37 +9,6 @@ Simple system and OS information library for [node.js][nodejs-url]
[![Caretaker][caretaker-image]][caretaker-url]
[![MIT license][license-img]][license-url]
### systeminformation is celebrating 4th birthday today!
```
( (
( ) ( ) (
( Y Y ( )
( ) |"| |"| Y
Y | | | | |"|
|"| | |.-----| |---.___ | |
| | .--| |,~~~~~| |~~~,,,,'-| |
| |-,,~~'-'___ '-' ~~| |._
.| |~ // ___ '-',,'.
/,'-' <_// // _ __ ~,\
/ ; ,-, \\_> <<_' ____________;_)
| ; {(_)} _, ._>>`'-._ |
| ; '-'\_\/> '-._ |
|\ ~,,, _\__ ,,,,,'-. |
| '-._ ~~,,, ,,,~~ __.-'~ | |
| '-.__ ~~~~~~~~~~~~ __.-' |__|
|\ `'----------'` _|
| '=._ __.=' |
: '=.__ __.=' |
\ `'==========='` .'
'-._ __.-'
'-.__ __.-'
`'-----------'`
```
Born 2014-03-13, started as a small project, now over 6000 lines of code - still no dependencies.
Time to say thank you to all contributers!
## Quick Start
Lightweight collection of 35+ functions to retrieve detailed hardware, system and OS information (Linux, macOS, partial Windows and FreeBSD support) - no npm dependencies.
@ -602,9 +571,9 @@ OSX Temperature: credits here are going to:
Linux is a registered trademark of Linus Torvalds. Apple, macOS, OS X are registered trademarks of Apple Inc.,
Windows is a registered trademark of Microsoft Corporation. Node.js is a trademark of Joyent Inc.,
Intel is a trademark of Intel Corporation, AMD is a trademark of Advanced Micro Devices Inc.,
Raspberry Pi is a trademark of the Raspberry Pi Foundation, Debian is a trademark of the Debian Project,
Ubuntu is a trademark of Canonical Ltd., FreeBSD is a registered trademark of The FreeBSD Foundation,
Intel is a trademark of Intel Corporation, AMD is a trademark of Advanced Micro Devices Inc.,
Raspberry Pi is a trademark of the Raspberry Pi Foundation, Debian is a trademark of the Debian Project,
Ubuntu is a trademark of Canonical Ltd., FreeBSD is a registered trademark of The FreeBSD Foundation,
Docker is a trademark of Docker, Inc.
All other trademarks are the property of their respective owners.

View File

@ -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); }