networkInterface() fix secondary and link-local ip (linux, macOS)

This commit is contained in:
Sebastian Hildebrandt
2025-12-26 08:56:25 +01:00
parent 6b160d9a90
commit ec84442dd5
7 changed files with 521 additions and 370 deletions
+1 -1
View File
@@ -779,7 +779,7 @@ function graphics(callback) {
try {
stdout = execSync(
'defaults read /Library/Preferences/com.apple.windowserver.plist 2>/dev/null;defaults read /Library/Preferences/com.apple.windowserver.displays.plist 2>/dev/null; echo ""',
{ maxBuffer: 1024 * 20000 }
{ maxBuffer: 1024 * 102400 }
);
const output = (stdout || '').toString();
const obj = util.plistReader(output);
+57 -32
View File
@@ -524,12 +524,12 @@ function parseLinesDarwinNics(sections) {
function getDarwinNics() {
const cmd = '/sbin/ifconfig -v';
try {
const lines = execSync(cmd, { maxBuffer: 1024 * 20000 })
const lines = execSync(cmd, { maxBuffer: 1024 * 102400 })
.toString()
.split('\n');
const nsections = splitSectionsNics(lines);
return parseLinesDarwinNics(nsections);
} catch (e) {
} catch {
return [];
}
}
@@ -543,7 +543,7 @@ function getLinuxIfaceConnectionName(interfaceName) {
const connectionNameLines = resultFormat.split(' ').slice(3);
const connectionName = connectionNameLines.join(' ');
return connectionName !== '--' ? connectionName : '';
} catch (e) {
} catch {
return '';
}
}
@@ -566,7 +566,7 @@ function checkLinuxDCHPInterfaces(file) {
result = result.concat(checkLinuxDCHPInterfaces(file));
}
});
} catch (e) {
} catch {
util.noop();
}
return result;
@@ -580,12 +580,12 @@ function getLinuxDHCPNics() {
const lines = execSync(cmd, util.execOptsLinux).toString().split('\n');
const nsections = splitSectionsNics(lines);
result = parseLinuxDHCPNics(nsections);
} catch (e) {
} catch {
util.noop();
}
try {
result = checkLinuxDCHPInterfaces('/etc/network/interfaces');
} catch (e) {
} catch {
util.noop();
}
return result;
@@ -632,7 +632,7 @@ function getLinuxIfaceDHCPstatus(iface, connectionName, DHCPNics) {
break;
}
return result;
} catch (e) {
} catch {
return DHCPNics.indexOf(iface) >= 0;
}
} else {
@@ -648,7 +648,7 @@ function getDarwinIfaceDHCPstatus(iface) {
if (lines.length && lines[0].startsWith('lease_time')) {
result = true;
}
} catch (e) {
} catch {
util.noop();
}
return result;
@@ -662,7 +662,7 @@ function getLinuxIfaceDNSsuffix(connectionName) {
const resultFormat = result.replace(/\s+/g, ' ').trim();
const dnsSuffix = resultFormat.split(' ').slice(1).toString();
return dnsSuffix === '--' ? 'Not defined' : dnsSuffix;
} catch (e) {
} catch {
return 'Unknown';
}
} else {
@@ -679,7 +679,7 @@ function getLinuxIfaceIEEE8021xAuth(connectionName) {
const authenticationProtocol = resultFormat.split(' ').slice(1).toString();
return authenticationProtocol === '--' ? '' : authenticationProtocol;
} catch (e) {
} catch {
return 'Not defined';
}
} else {
@@ -785,16 +785,44 @@ function networkInterfaces(callback, rescan, defaultString) {
nics = getDarwinNics();
nics.forEach((nic) => {
let ip4link = '';
let ip4linksubnet = '';
let ip6link = '';
let ip6linksubnet = '';
nic.ip4 = '';
nic.ip6 = '';
if ({}.hasOwnProperty.call(ifaces, nic.iface)) {
ifaces[nic.iface].forEach((details) => {
if (details.family === 'IPv4' || details.family === 4) {
nic.ip4subnet = details.netmask;
if (!nic.ip4 && !nic.ip4.match(/^169.254/i)) {
nic.ip4 = details.address;
nic.ip4subnet = details.netmask;
}
if (nic.ip4.match(/^169.254/i)) {
ip4link = details.address;
ip4linksubnet = details.netmask;
}
}
if (details.family === 'IPv6' || details.family === 6) {
nic.ip6subnet = details.netmask;
if (!nic.ip6 && !nic.ip6.match(/^fe80::/i)) {
nic.ip6 = details.address;
nic.ip6subnet = details.netmask;
}
if (nic.ip6.match(/^fe80::/i)) {
ip6link = details.address;
ip6linksubnet = details.netmask;
}
}
});
}
if (!nic.ip4 && ip4link) {
nic.ip4 = ip4link;
nic.ip4subnet = ip4linksubnet;
}
if (!nic.ip6 && ip6link) {
nic.ip6 = ip6link;
nic.ip6subnet = ip6linksubnet;
}
let ifaceSanitized = '';
const s = util.isPrototypePolluted() ? '---' : util.sanitizeShellString(nic.iface);
@@ -881,17 +909,14 @@ function networkInterfaces(callback, rescan, defaultString) {
const ifaceName = dev;
ifaces[dev].forEach((details) => {
if (details.family === 'IPv4' || details.family === 4) {
if (ip4.match(/^169.254/i)) {
ip4link = details.address;
ip4linksubnet = details.netmask;
} else if (!ip4) {
if (!ip4 && !ip4.match(/^169.254/i)) {
ip4 = details.address;
ip4subnet = details.netmask;
}
}
if (!ip4 && (details.family === 'IPv4' || details.family === 4)) {
ip4 = details.address;
ip4subnet = details.netmask;
if (ip4.match(/^169.254/i)) {
ip4link = details.address;
ip4linksubnet = details.netmask;
}
}
if (details.family === 'IPv6' || details.family === 6) {
if (!ip6 && !ip6.match(/^fe80::/i)) {
@@ -1260,7 +1285,7 @@ function networkStats(ifaces, callback) {
ifaces.__proto__.substring = util.stringSubstring;
ifaces.__proto__.trim = util.stringTrim;
ifaces.__proto__.startsWith = util.stringStartWith;
} catch (e) {
} catch {
Object.setPrototypeOf(ifaces, util.stringObj);
}
@@ -1577,7 +1602,7 @@ function networkConnections(callback) {
cmd =
'export LC_ALL=C; netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL';
}
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
exec(cmd, { maxBuffer: 1024 * 102400 }, function (error, stdout) {
let lines = stdout.toString().split('\n');
if (!error && (lines.length > 1 || lines[0] !== '')) {
lines.forEach(function (line) {
@@ -1622,7 +1647,7 @@ function networkConnections(callback) {
resolve(result);
} else {
cmd = 'ss -tunap | grep "ESTAB\\|SYN-SENT\\|SYN-RECV\\|FIN-WAIT1\\|FIN-WAIT2\\|TIME-WAIT\\|CLOSE\\|CLOSE-WAIT\\|LAST-ACK\\|LISTEN\\|CLOSING"';
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
exec(cmd, { maxBuffer: 1024 * 102400 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
lines.forEach(function (line) {
@@ -1686,9 +1711,9 @@ function networkConnections(callback) {
if (_darwin) {
let cmd = 'netstat -natvln | head -n2; netstat -natvln | grep "tcp4\\|tcp6\\|udp4\\|udp6"';
const states = 'ESTABLISHED|SYN_SENT|SYN_RECV|FIN_WAIT1|FIN_WAIT_1|FIN_WAIT2|FIN_WAIT_2|TIME_WAIT|CLOSE|CLOSE_WAIT|LAST_ACK|LISTEN|CLOSING|UNKNOWN'.split('|');
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
exec(cmd, { maxBuffer: 1024 * 102400 }, function (error, stdout) {
if (!error) {
exec('ps -axo pid,command', { maxBuffer: 1024 * 20000 }, function (err2, stdout2) {
exec('ps -axo pid,command', { maxBuffer: 1024 * 102400 }, function (err2, stdout2) {
let processes = stdout2.toString().split('\n');
processes = processes.map((line) => {
return line.trim().replace(/ +/g, ' ');
@@ -1847,7 +1872,7 @@ function networkConnections(callback) {
resolve(result);
}
});
} catch (e) {
} catch {
if (callback) {
callback(result);
}
@@ -1867,7 +1892,7 @@ function networkGatewayDefault(callback) {
if (_linux || _freebsd || _openbsd || _netbsd) {
let cmd = 'ip route get 1';
try {
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
exec(cmd, { maxBuffer: 1024 * 102400 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
const line = lines && lines[0] ? lines[0] : '';
@@ -1887,7 +1912,7 @@ function networkGatewayDefault(callback) {
resolve(result);
}
});
} catch (e) {
} catch {
if (callback) {
callback(result);
}
@@ -1897,7 +1922,7 @@ function networkGatewayDefault(callback) {
if (_darwin) {
let cmd = 'route -n get default';
try {
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
exec(cmd, { maxBuffer: 1024 * 102400 }, function (error, stdout) {
if (!error) {
const lines = stdout
.toString()
@@ -1907,7 +1932,7 @@ function networkGatewayDefault(callback) {
}
if (!result) {
cmd = "netstat -rn | awk '/default/ {print $2}'";
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
exec(cmd, { maxBuffer: 1024 * 102400 }, function (error, stdout) {
const lines = stdout
.toString()
.split('\n')
@@ -1927,7 +1952,7 @@ function networkGatewayDefault(callback) {
resolve(result);
}
});
} catch (e) {
} catch {
if (callback) {
callback(result);
}
@@ -1979,7 +2004,7 @@ function networkGatewayDefault(callback) {
resolve(result);
}
});
} catch (e) {
} catch {
if (callback) {
callback(result);
}
+451 -331
View File
File diff suppressed because it is too large Load Diff
+4 -4
View File
@@ -51,13 +51,13 @@ const _psIdSeperator = '--##ID##--';
const execOptsWin = {
windowsHide: true,
maxBuffer: 1024 * 20000,
maxBuffer: 1024 * 102400,
encoding: 'UTF-8',
env: Object.assign({}, process.env, { LANG: 'en_US.UTF-8' })
};
const execOptsLinux = {
maxBuffer: 1024 * 20000,
maxBuffer: 1024 * 102400,
encoding: 'UTF-8',
stdio: ['pipe', 'pipe', 'ignore']
};
@@ -436,7 +436,7 @@ function powerShellStart() {
_psChild = spawn(_powerShell, ['-NoProfile', '-NoLogo', '-InputFormat', 'Text', '-NoExit', '-Command', '-'], {
stdio: 'pipe',
windowsHide: true,
maxBuffer: 1024 * 20000,
maxBuffer: 1024 * 102400,
encoding: 'UTF-8',
env: Object.assign({}, process.env, { LANG: 'en_US.UTF-8' })
});
@@ -516,7 +516,7 @@ function powerShell(cmd) {
const child = spawn(_powerShell, ['-NoProfile', '-NoLogo', '-InputFormat', 'Text', '-ExecutionPolicy', 'Unrestricted', '-Command', cmd], {
stdio: 'pipe',
windowsHide: true,
maxBuffer: 1024 * 20000,
maxBuffer: 1024 * 102400,
encoding: 'UTF-8',
env: Object.assign({}, process.env, { LANG: 'en_US.UTF-8' })
});