From d446b8d9e48ac04c17231d853a1208de1351d456 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 21 Apr 2022 22:55:32 +0200 Subject: [PATCH] etworkStats() improved scanning (mac OS) --- CHANGELOG.md | 1 + docs/history.html | 5 +++ docs/index.html | 2 +- lib/battery.js | 2 +- lib/network.js | 104 ++++++++++++++++++++++++++-------------------- 5 files changed, 68 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 449017f..0b69b14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,7 @@ For major (breaking) changes - **version 4, 3 and 2** - see end of page. | Version | Date | Comment | | -------------- | -------------- | -------- | +| 5.11.13 | 2022-04-21 | `etworkStats()` improved scanning (mac OS) | | 5.11.12 | 2022-04-19 | `battery()` improved M1 support (mac OS) | | 5.11.11 | 2022-04-19 | `networkInterfaces()` improved parsing (windows) | | 5.11.10 | 2022-04-18 | updated docs | diff --git a/docs/history.html b/docs/history.html index bd5cf92..9d17be3 100644 --- a/docs/history.html +++ b/docs/history.html @@ -57,6 +57,11 @@ + + 5.11.13 + 2022-04-21 + networkStats() improved scanning (mac OS) + 5.11.12 2022-04-19 diff --git a/docs/index.html b/docs/index.html index e627c1f..388d857 100644 --- a/docs/index.html +++ b/docs/index.html @@ -170,7 +170,7 @@
systeminformation
 
-
New Version: 5.11.12
+
New Version: 5.11.13
diff --git a/lib/battery.js b/lib/battery.js index cfc6858..942cb3b 100644 --- a/lib/battery.js +++ b/lib/battery.js @@ -185,7 +185,7 @@ module.exports = function (callback) { result.voltage = parseInt('0' + util.getValue(lines, 'voltage', '='), 10) / 1000.0; result.capacityUnit = result.voltage ? 'mWh' : 'mAh'; result.maxCapacity = Math.round(parseInt('0' + util.getValue(lines, 'applerawmaxcapacity', '='), 10) * (result.voltage || 1)); - result.currentCapacity = Math.round(parseInt('0' + util.getValue(lines, 'applerawxcurrentcapacity', '='), 10) * (result.voltage || 1)); + result.currentCapacity = Math.round(parseInt('0' + util.getValue(lines, 'applerawcurrentcapacity', '='), 10) * (result.voltage || 1)); result.designedCapacity = Math.round(parseInt('0' + util.getValue(lines, 'DesignCapacity', '='), 10) * (result.voltage || 1)); result.manufacturer = 'Apple'; result.serial = util.getValue(lines, 'BatterySerialNumber', '='); diff --git a/lib/network.js b/lib/network.js index ea649a8..d67bc51 100644 --- a/lib/network.js +++ b/lib/network.js @@ -108,6 +108,7 @@ function getDefaultNetworkInterface() { if (_linux) { cmd = 'ip route 2> /dev/null | grep default | awk \'{print $5}\''; } if (_darwin) { cmd = 'route -n get default 2>/dev/null | grep interface: | awk \'{print $2}\''; } if (_freebsd || _openbsd || _netbsd || _sunos) { cmd = 'route get 0.0.0.0 | grep interface:'; } + // console.log('SYNC - default darwin 3'); let result = execSync(cmd); ifacename = result.toString().split('\n')[0]; if (ifacename.indexOf(':') > -1) { @@ -172,6 +173,7 @@ function getMacAddresses() { if (_darwin) { try { const cmd = '/sbin/ifconfig'; + // console.log('SYNC - macAde darwin 6'); let res = execSync(cmd); const lines = res.toString().split('\n'); for (let i = 0; i < lines.length; i++) { @@ -504,6 +506,7 @@ function parseLinesDarwinNics(sections) { function getDarwinNics() { const cmd = '/sbin/ifconfig -v'; try { + // console.log('SYNC - Nics darwin 12'); const lines = execSync(cmd, { maxBuffer: 1024 * 20000 }).toString().split('\n'); const nsections = splitSectionsNics(lines); return (parseLinesDarwinNics(nsections)); @@ -622,6 +625,7 @@ function getDarwinIfaceDHCPstatus(iface) { let result = false; const cmd = `ipconfig getpacket "${iface}" 2>/dev/null | grep lease_time;`; try { + // console.log('SYNC - DHCP status darwin 17'); const lines = execSync(cmd).toString().split('\n'); if (lines.length && lines[0].startsWith('lease_time')) { result = true; @@ -713,7 +717,6 @@ function networkInterfaces(callback, rescan, defaultString) { return new Promise((resolve) => { process.nextTick(() => { - const defaultInterface = getDefaultNetworkInterface(); let ifaces = os.networkInterfaces(); @@ -723,56 +726,67 @@ function networkInterfaces(callback, rescan, defaultString) { let nics8021xInfo = []; // seperate handling in OSX if (_darwin || _freebsd || _openbsd || _netbsd) { - nics = getDarwinNics(); + if ((JSON.stringify(ifaces) === JSON.stringify(_ifaces)) && !rescan) { + // no changes - just return object + result = _networkInterfaces; + + if (callback) { callback(result); } + resolve(result); + } else { + const defaultInterface = getDefaultNetworkInterface(); + _ifaces = JSON.parse(JSON.stringify(ifaces)); + + nics = getDarwinNics(); - nics.forEach(nic => { + nics.forEach(nic => { - if ({}.hasOwnProperty.call(ifaces, nic.iface)) { - ifaces[nic.iface].forEach(function (details) { - if (details.family === 'IPv4') { - nic.ip4subnet = details.netmask; - } - if (details.family === 'IPv6') { - nic.ip6subnet = details.netmask; - } + if ({}.hasOwnProperty.call(ifaces, nic.iface)) { + ifaces[nic.iface].forEach(function (details) { + if (details.family === 'IPv4') { + nic.ip4subnet = details.netmask; + } + if (details.family === 'IPv6') { + nic.ip6subnet = details.netmask; + } + }); + } + + result.push({ + iface: nic.iface, + ifaceName: nic.iface, + default: nic.iface === defaultInterface, + ip4: nic.ip4, + ip4subnet: nic.ip4subnet || '', + ip6: nic.ip6, + ip6subnet: nic.ip6subnet || '', + mac: nic.mac, + internal: nic.internal, + virtual: nic.internal ? false : testVirtualNic(nic.iface, nic.iface, nic.mac), + operstate: nic.operstate, + type: nic.type, + duplex: nic.duplex, + mtu: nic.mtu, + speed: nic.speed, + dhcp: getDarwinIfaceDHCPstatus(nic.iface), + dnsSuffix: '', + ieee8021xAuth: '', + ieee8021xState: '', + carrierChanges: 0 }); - } - - result.push({ - iface: nic.iface, - ifaceName: nic.iface, - default: nic.iface === defaultInterface, - ip4: nic.ip4, - ip4subnet: nic.ip4subnet || '', - ip6: nic.ip6, - ip6subnet: nic.ip6subnet || '', - mac: nic.mac, - internal: nic.internal, - virtual: nic.internal ? false : testVirtualNic(nic.iface, nic.iface, nic.mac), - operstate: nic.operstate, - type: nic.type, - duplex: nic.duplex, - mtu: nic.mtu, - speed: nic.speed, - dhcp: getDarwinIfaceDHCPstatus(nic.iface), - dnsSuffix: '', - ieee8021xAuth: '', - ieee8021xState: '', - carrierChanges: 0 }); - }); - _networkInterfaces = result; - if (defaultString.toLowerCase().indexOf('default') >= 0) { - result = result.filter(item => item.default); - if (result.length > 0) { - result = result[0]; - } else { - result = []; + _networkInterfaces = result; + if (defaultString.toLowerCase().indexOf('default') >= 0) { + result = result.filter(item => item.default); + if (result.length > 0) { + result = result[0]; + } else { + result = []; + } } + if (callback) { callback(result); } + resolve(result); } - if (callback) { callback(result); } - resolve(result); } if (_linux) { if ((JSON.stringify(ifaces) === JSON.stringify(_ifaces)) && !rescan) { @@ -784,6 +798,7 @@ function networkInterfaces(callback, rescan, defaultString) { } else { _ifaces = JSON.parse(JSON.stringify(ifaces)); _dhcpNics = getLinuxDHCPNics(); + const defaultInterface = getDefaultNetworkInterface(); for (let dev in ifaces) { let ip4 = ''; let ip4subnet = ''; @@ -927,6 +942,7 @@ function networkInterfaces(callback, rescan, defaultString) { resolve(result); } else { _ifaces = JSON.parse(JSON.stringify(ifaces)); + const defaultInterface = getDefaultNetworkInterface(); getWindowsNics().then(function (nics) { nics.forEach(nic => {