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 | | 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.7 | 2018-03-13 | celebrating 4th birthday |
| 3.37.6 | 2018-03-12 | updated docs: fixed `diskLayout`and `mamlayout` | | 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` | | 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] [![Caretaker][caretaker-image]][caretaker-url]
[![MIT license][license-img]][license-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 ## 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. Lightweight collection of 35+ functions to retrieve detailed hardware, system and OS information (Linux, macOS, partial Windows and FreeBSD support) - no npm dependencies.

View File

@ -37,9 +37,21 @@ let isIpAvailable;
function getDefaultNetworkInterface() { function getDefaultNetworkInterface() {
if (!_default_iface) { let ifaces = os.networkInterfaces();
let ifacename = ''; let ifacename = '';
let scopeid = 9999; let scopeid = 9999;
// 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 (_linux || _darwin || _freebsd || _openbsd) { if (_linux || _darwin || _freebsd || _openbsd) {
let cmd = ''; let cmd = '';
if (_linux) cmd = 'route 2>/dev/null | grep default | awk "{print $8}"'; if (_linux) cmd = 'route 2>/dev/null | grep default | awk "{print $8}"';
@ -52,23 +64,7 @@ function getDefaultNetworkInterface() {
} }
} }
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;
}
});
}
}
}
if (ifacename) _default_iface = ifacename; if (ifacename) _default_iface = ifacename;
}
return _default_iface; return _default_iface;
} }
@ -277,8 +273,7 @@ function networkStats(iface, callback) {
return new Promise((resolve) => { return new Promise((resolve) => {
process.nextTick(() => { process.nextTick(() => {
_default_iface = _default_iface || getDefaultNetworkInterface(); iface = iface || getDefaultNetworkInterface();
iface = iface || _default_iface; // (_darwin ? 'en0' : 'eth0');
let result = { let result = {
iface: iface, iface: iface,
@ -357,7 +352,7 @@ function networkStats(iface, callback) {
rx = parseInt(stats[6]); rx = parseInt(stats[6]);
tx = parseInt(stats[9]); tx = parseInt(stats[9]);
result = calcNetworkSpeed(iface, rx, tx, operstate); result = calcNetworkSpeed(iface, rx, tx, result.operstate);
} }
} }
if (callback) { callback(result); } if (callback) { callback(result); }