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 = [];
if (_linux || _freebsd || _openbsd || _netbsd) {
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) {
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 * 102400 }, function (error, stdout) {
exec(cmd, { maxBuffer: 1024 * 102400 }, (error, stdout) => {
let lines = stdout.toString().split('\n');
if (!error && (lines.length > 1 || lines[0] !== '')) {
lines.forEach(function (line) {
lines.forEach((line) => {
line = line.replace(/ +/g, ' ').split(' ');
if (line.length >= 7) {
let localip = line[3];
let localport = '';
let localaddress = line[3].split(':');
const localaddress = line[3].split(':');
if (localaddress.length > 1) {
localport = localaddress[localaddress.length - 1];
localaddress.pop();
@ -1618,14 +1618,14 @@ function networkConnections(callback) {
}
let peerip = line[4];
let peerport = '';
let peeraddress = line[4].split(':');
const peeraddress = line[4].split(':');
if (peeraddress.length > 1) {
peerport = peeraddress[peeraddress.length - 1];
peeraddress.pop();
peerip = peeraddress.join(':');
}
let connstate = line[5];
let proc = line[6].split('/');
const connstate = line[5];
const proc = line[6].split('/');
if (connstate) {
result.push({
@ -1647,15 +1647,15 @@ 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 * 102400 }, function (error, stdout) {
exec(cmd, { maxBuffer: 1024 * 102400 }, (error, stdout) => {
if (!error) {
let lines = stdout.toString().split('\n');
lines.forEach(function (line) {
const lines = stdout.toString().split('\n');
lines.forEach((line) => {
line = line.replace(/ +/g, ' ').split(' ');
if (line.length >= 6) {
let localip = line[4];
let localport = '';
let localaddress = line[4].split(':');
const localaddress = line[4].split(':');
if (localaddress.length > 1) {
localport = localaddress[localaddress.length - 1];
localaddress.pop();
@ -1663,7 +1663,7 @@ function networkConnections(callback) {
}
let peerip = line[5];
let peerport = '';
let peeraddress = line[5].split(':');
const peeraddress = line[5].split(':');
if (peeraddress.length > 1) {
peerport = peeraddress[peeraddress.length - 1];
peeraddress.pop();
@ -1679,7 +1679,7 @@ function networkConnections(callback) {
let pid = null;
let process = '';
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) {
process = proc[0].split(' ')[0].split(':')[0];
pid = parseInt(proc[1], 10);
@ -1709,16 +1709,16 @@ function networkConnections(callback) {
});
}
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('|');
exec(cmd, { maxBuffer: 1024 * 102400 }, function (error, stdout) {
exec(cmd, { maxBuffer: 1024 * 102400 }, (error, stdout) => {
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');
processes = processes.map((line) => {
return line.trim().replace(/ +/g, ' ');
});
let lines = stdout.toString().split('\n');
const lines = stdout.toString().split('\n');
lines.shift();
let pidPos = 8;
if (lines.length > 1 && lines[0].indexOf('pid') > 0) {
@ -1729,12 +1729,12 @@ function networkConnections(callback) {
.split(' ');
pidPos = header.indexOf('pid');
}
lines.forEach(function (line) {
lines.forEach((line) => {
line = line.replace(/ +/g, ' ').split(' ');
if (line.length >= 8) {
let localip = line[3];
let localport = '';
let localaddress = line[3].split('.');
const localaddress = line[3].split('.');
if (localaddress.length > 1) {
localport = localaddress[localaddress.length - 1];
localaddress.pop();
@ -1742,14 +1742,14 @@ function networkConnections(callback) {
}
let peerip = line[4];
let peerport = '';
let peeraddress = line[4].split('.');
const peeraddress = line[4].split('.');
if (peeraddress.length > 1) {
peerport = peeraddress[peeraddress.length - 1];
peeraddress.pop();
peerip = peeraddress.join('.');
}
const hasState = states.indexOf(line[5]) >= 0;
let connstate = hasState ? line[5] : 'UNKNOWN';
const connstate = hasState ? line[5] : 'UNKNOWN';
let pidField = '';
if (line[line.length - 9].indexOf(':') >= 0) {
pidField = line[line.length - 9].split(':')[1];
@ -1760,7 +1760,7 @@ function networkConnections(callback) {
pidField = pidField.split(':')[1];
}
}
let pid = parseInt(pidField, 10);
const pid = parseInt(pidField, 10);
if (connstate) {
result.push({
protocol: line[0],