networkInterfaces() added default property and parameter

This commit is contained in:
Sebastian Hildebrandt
2022-01-26 20:51:33 +01:00
parent fe0d9703a9
commit b4ae9c3ac0
6 changed files with 90 additions and 4 deletions
+40 -1
View File
@@ -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);
});