From b4ae9c3ac0d5e41e4f3b990f299ef98e81aee4fe Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Wed, 26 Jan 2022 20:51:33 +0100 Subject: [PATCH] networkInterfaces() added default property and parameter --- CHANGELOG.md | 1 + README.md | 4 +++- docs/history.html | 5 +++++ docs/index.html | 4 ++-- docs/network.html | 39 +++++++++++++++++++++++++++++++++++++++ lib/network.js | 41 ++++++++++++++++++++++++++++++++++++++++- 6 files changed, 90 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 840e69e..598d4b4 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.0 | 2022-01-26 | `networkInterfaces()` added default property and parameter | | 5.10.7 | 2022-01-21 | `processes()` reverted PR #560 (windows) | | 5.10.6 | 2022-01-21 | `usb()` fix `users()` fix tty (windows) | | 5.10.5 | 2022-01-19 | `processes()` fix calculation (windows) | diff --git a/README.md b/README.md index 9a6cf16..bf80acf 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ si.cpu() (last 7 major and minor version releases) +- Version 5.11.0: `networkInterfaces()` added default property and default parameter - Version 5.10.0: basic `android` support - Version 5.9.0: `graphics()` added properties (macOS) - Version 5.8.0: `disksIO()` added waitTime, waitPercent (linux) @@ -539,9 +540,10 @@ Full function reference with examples can be found at [https://systeminformation | Function | Result object | Linux | BSD | Mac | Win | Sun | Comments | | --------------- | ------------- | ----- | ------- | --- | --- | --- | -------- | -| si.networkInterfaces(cb) | [{...}] | X | X | X | X | X | array of network interfaces | +| si.networkInterfaces(cb) | [{...}] | X | X | X | X | X | array of network interfaces
With the 'default' parameter it returns
only the default interface | | | [0].iface | X | X | X | X | X | interface | | | [0].ifaceName | X | X | X | X | X | interface name (differs on Windows) | +| | [0].default | X | X | X | X | X | true if this is the default interface | | | [0].ip4 | X | X | X | X | X | ip4 address | | | [0].ip4subnet | X | X | X | X | X | ip4 subnet mask | | | [0].ip6 | X | X | X | X | X | ip6 address | diff --git a/docs/history.html b/docs/history.html index 507b3bb..70e7c73 100644 --- a/docs/history.html +++ b/docs/history.html @@ -57,6 +57,11 @@ + + 5.11.0 + 2022-01-26 + networkInterfaces() added default property and parameter + 5.10.7 2022-01-21 diff --git a/docs/index.html b/docs/index.html index 77db2c3..2134dd1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -170,7 +170,7 @@
systeminformation
 
-
New Version: 5.10.7
+
New Version: 5.11.0
@@ -206,7 +206,7 @@
-
15,121
+
15,185
Lines of code
diff --git a/docs/network.html b/docs/network.html index 4a44ad4..3229675 100644 --- a/docs/network.html +++ b/docs/network.html @@ -97,6 +97,16 @@ X interface name (differs on Windows) + + + [0].default + X + X + X + X + X + true if this is the default interface + [0].ip4 @@ -278,6 +288,7 @@ si.networkInterfaces().then(data => console.log(data)); console.log(data));const si = require('systeminformation'); +si.networkInterfaces('default').then(data => console.log(data)); +
+{
+  iface: 'en0',
+  ifaceName: 'en0',
+  default: true,
+  ip4: '192.168.0.27',
+  ip4subnet: '255.255.255.0',
+  ip6: 'fe80::134a:1e43:abc5:d413',
+  ip6subnet: 'ffff:ffff:ffff:ffff::',
+  mac: 'xx:xx:xx:xx:xx:xx',
+  internal: false,
+  virtual: false,
+  operstate: 'up',
+  type: 'wired',
+  duplex: 'full',
+  mtu: 1500,
+  speed: 1000,
+  dhcp: true,
+  dnsSuffix: '',
+  ieee8021xAuth: '',
+  ieee8021xState: '',
+  carrierChanges: 0
+}
si.networkInterfaceDefault(cb) diff --git a/lib/network.js b/lib/network.js index 9c79c84..31a2ca6 100644 --- a/lib/network.js +++ b/lib/network.js @@ -692,17 +692,29 @@ function testVirtualNic(iface, ifaceName, mac) { } else { return false; } } -function networkInterfaces(callback, rescan) { +function networkInterfaces(callback, rescan, defaultString) { + + if (typeof callback === 'string') { + defaultString = callback; + rescan = true; + callback = null; + } if (typeof callback === 'boolean') { rescan = callback; callback = null; + defaultString = ''; } if (typeof rescan === 'undefined') { rescan = true; } + defaultString = defaultString || ''; + defaultString = '' + defaultString; + return new Promise((resolve) => { process.nextTick(() => { + const defaultInterface = getDefaultNetworkInterface(); + let ifaces = os.networkInterfaces(); let result = []; @@ -730,6 +742,7 @@ function networkInterfaces(callback, rescan) { result.push({ iface: nic.iface, ifaceName: nic.iface, + default: nic.iface === defaultInterface, ip4: nic.ip4, ip4subnet: nic.ip4subnet || '', ip6: nic.ip6, @@ -750,6 +763,14 @@ function networkInterfaces(callback, rescan) { }); }); _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); } @@ -863,6 +884,7 @@ function networkInterfaces(callback, rescan) { result.push({ iface, ifaceName, + default: iface === defaultInterface, ip4, ip4subnet, ip6, @@ -884,6 +906,14 @@ function networkInterfaces(callback, rescan) { } } _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); } @@ -990,6 +1020,7 @@ function networkInterfaces(callback, rescan) { result.push({ iface, ifaceName, + default: iface === defaultInterface, ip4, ip4subnet, ip6, @@ -1011,6 +1042,14 @@ function networkInterfaces(callback, rescan) { } } _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); });