networkConnections: wip ss fix (linux)

This commit is contained in:
Sebastian Hildebrandt 2025-12-26 15:55:16 +01:00
parent f93e8a622f
commit 151817432e

View File

@ -1597,20 +1597,20 @@ function networkConnections(callback) {
let result = []; let result = [];
if (_linux || _freebsd || _openbsd || _netbsd) { if (_linux || _freebsd || _openbsd || _netbsd) {
let cmd = let cmd =
'export LC_ALL=C; netstat -tunap | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL'; 'export LC_ALL=C; netstatX -tunap | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL';
if (_freebsd || _openbsd || _netbsd) { if (_freebsd || _openbsd || _netbsd) {
cmd = 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'; '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 * 102400 }, function (error, stdout) { exec(cmd, { maxBuffer: 1024 * 102400 }, (error, stdout) => {
let lines = stdout.toString().split('\n'); let lines = stdout.toString().split('\n');
if (!error && (lines.length > 1 || lines[0] !== '')) { if (!error && (lines.length > 1 || lines[0] !== '')) {
lines.forEach(function (line) { lines.forEach((line) => {
line = line.replace(/ +/g, ' ').split(' '); line = line.replace(/ +/g, ' ').split(' ');
if (line.length >= 7) { if (line.length >= 7) {
let localip = line[3]; let localip = line[3];
let localport = ''; let localport = '';
let localaddress = line[3].split(':'); const localaddress = line[3].split(':');
if (localaddress.length > 1) { if (localaddress.length > 1) {
localport = localaddress[localaddress.length - 1]; localport = localaddress[localaddress.length - 1];
localaddress.pop(); localaddress.pop();
@ -1618,14 +1618,14 @@ function networkConnections(callback) {
} }
let peerip = line[4]; let peerip = line[4];
let peerport = ''; let peerport = '';
let peeraddress = line[4].split(':'); const peeraddress = line[4].split(':');
if (peeraddress.length > 1) { if (peeraddress.length > 1) {
peerport = peeraddress[peeraddress.length - 1]; peerport = peeraddress[peeraddress.length - 1];
peeraddress.pop(); peeraddress.pop();
peerip = peeraddress.join(':'); peerip = peeraddress.join(':');
} }
let connstate = line[5]; const connstate = line[5];
let proc = line[6].split('/'); const proc = line[6].split('/');
if (connstate) { if (connstate) {
result.push({ result.push({
@ -1647,15 +1647,15 @@ function networkConnections(callback) {
resolve(result); resolve(result);
} else { } else {
cmd = 'ss -tunap | grep "ESTAB\\|SYN-SENT\\|SYN-RECV\\|FIN-WAIT1\\|FIN-WAIT2\\|TIME-WAIT\\|CLOSE\\|CLOSE-WAIT\\|LAST-ACK\\|LISTEN\\|CLOSING"'; 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 * 102400 }, function (error, stdout) { exec(cmd, { maxBuffer: 1024 * 102400 }, (error, stdout) => {
if (!error) { if (!error) {
let lines = stdout.toString().split('\n'); const lines = stdout.toString().split('\n');
lines.forEach(function (line) { lines.forEach((line) => {
line = line.replace(/ +/g, ' ').split(' '); line = line.replace(/ +/g, ' ').split(' ');
if (line.length >= 6) { if (line.length >= 6) {
let localip = line[4]; let localip = line[4];
let localport = ''; let localport = '';
let localaddress = line[4].split(':'); const localaddress = line[4].split(':');
if (localaddress.length > 1) { if (localaddress.length > 1) {
localport = localaddress[localaddress.length - 1]; localport = localaddress[localaddress.length - 1];
localaddress.pop(); localaddress.pop();
@ -1663,7 +1663,7 @@ function networkConnections(callback) {
} }
let peerip = line[5]; let peerip = line[5];
let peerport = ''; let peerport = '';
let peeraddress = line[5].split(':'); const peeraddress = line[5].split(':');
if (peeraddress.length > 1) { if (peeraddress.length > 1) {
peerport = peeraddress[peeraddress.length - 1]; peerport = peeraddress[peeraddress.length - 1];
peeraddress.pop(); peeraddress.pop();
@ -1679,7 +1679,7 @@ function networkConnections(callback) {
let pid = null; let pid = null;
let process = ''; let process = '';
if (line.length >= 7 && line[6].indexOf('users:') > -1) { if (line.length >= 7 && line[6].indexOf('users:') > -1) {
let proc = line[6].replace('users:(("', '').replace(/"/g, '').split(','); const proc = line[6].replace('users:(("', '').replace(/"/g, '').split(',');
if (proc.length > 2) { if (proc.length > 2) {
process = proc[0].split(' ')[0].split(':')[0]; process = proc[0].split(' ')[0].split(':')[0];
pid = parseInt(proc[1], 10); pid = parseInt(proc[1], 10);
@ -1709,16 +1709,16 @@ function networkConnections(callback) {
}); });
} }
if (_darwin) { if (_darwin) {
let cmd = 'netstat -natvln | head -n2; netstat -natvln | grep "tcp4\\|tcp6\\|udp4\\|udp6"'; const 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('|'); 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 * 102400 }, function (error, stdout) { exec(cmd, { maxBuffer: 1024 * 102400 }, (error, stdout) => {
if (!error) { if (!error) {
exec('ps -axo pid,command', { maxBuffer: 1024 * 102400 }, function (err2, stdout2) { exec('ps -axo pid,command', { maxBuffer: 1024 * 102400 }, (err2, stdout2) => {
let processes = stdout2.toString().split('\n'); let processes = stdout2.toString().split('\n');
processes = processes.map((line) => { processes = processes.map((line) => {
return line.trim().replace(/ +/g, ' '); return line.trim().replace(/ +/g, ' ');
}); });
let lines = stdout.toString().split('\n'); const lines = stdout.toString().split('\n');
lines.shift(); lines.shift();
let pidPos = 8; let pidPos = 8;
if (lines.length > 1 && lines[0].indexOf('pid') > 0) { if (lines.length > 1 && lines[0].indexOf('pid') > 0) {
@ -1729,12 +1729,12 @@ function networkConnections(callback) {
.split(' '); .split(' ');
pidPos = header.indexOf('pid'); pidPos = header.indexOf('pid');
} }
lines.forEach(function (line) { lines.forEach((line) => {
line = line.replace(/ +/g, ' ').split(' '); line = line.replace(/ +/g, ' ').split(' ');
if (line.length >= 8) { if (line.length >= 8) {
let localip = line[3]; let localip = line[3];
let localport = ''; let localport = '';
let localaddress = line[3].split('.'); const localaddress = line[3].split('.');
if (localaddress.length > 1) { if (localaddress.length > 1) {
localport = localaddress[localaddress.length - 1]; localport = localaddress[localaddress.length - 1];
localaddress.pop(); localaddress.pop();
@ -1742,14 +1742,14 @@ function networkConnections(callback) {
} }
let peerip = line[4]; let peerip = line[4];
let peerport = ''; let peerport = '';
let peeraddress = line[4].split('.'); const peeraddress = line[4].split('.');
if (peeraddress.length > 1) { if (peeraddress.length > 1) {
peerport = peeraddress[peeraddress.length - 1]; peerport = peeraddress[peeraddress.length - 1];
peeraddress.pop(); peeraddress.pop();
peerip = peeraddress.join('.'); peerip = peeraddress.join('.');
} }
const hasState = states.indexOf(line[5]) >= 0; const hasState = states.indexOf(line[5]) >= 0;
let connstate = hasState ? line[5] : 'UNKNOWN'; const connstate = hasState ? line[5] : 'UNKNOWN';
let pidField = ''; let pidField = '';
if (line[line.length - 9].indexOf(':') >= 0) { if (line[line.length - 9].indexOf(':') >= 0) {
pidField = line[line.length - 9].split(':')[1]; pidField = line[line.length - 9].split(':')[1];
@ -1760,7 +1760,7 @@ function networkConnections(callback) {
pidField = pidField.split(':')[1]; pidField = pidField.split(':')[1];
} }
} }
let pid = parseInt(pidField, 10); const pid = parseInt(pidField, 10);
if (connstate) { if (connstate) {
result.push({ result.push({
protocol: line[0], protocol: line[0],