networkConnections: improved parsing (macOS)

This commit is contained in:
Sebastian Hildebrandt 2024-12-13 07:33:20 +01:00
parent 81d816769c
commit e91f61dd17

View File

@ -1534,8 +1534,7 @@ function networkConnections(callback) {
}); });
} }
if (_darwin) { if (_darwin) {
// let cmd = 'netstat -natv | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"'; let cmd = 'netstat -natvln | grep "tcp4\\|tcp6\\|udp4\\|udp6\\|Recv-Q"';
let cmd = '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'; 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';
exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) { exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
if (!error) { if (!error) {
@ -1543,6 +1542,9 @@ function networkConnections(callback) {
let processes = stdout2.toString().split('\n'); let processes = stdout2.toString().split('\n');
processes = processes.map((line => { return line.trim().replace(/ +/g, ' '); })); processes = processes.map((line => { return line.trim().replace(/ +/g, ' '); }));
let lines = stdout.toString().split('\n'); let lines = stdout.toString().split('\n');
const header = lines[0];
const hasTransferred = header.indexOf('rxbytes') > -1;
lines.shift();
lines.forEach(function (line) { lines.forEach(function (line) {
line = line.replace(/ +/g, ' ').split(' '); line = line.replace(/ +/g, ' ').split(' ');
@ -1565,7 +1567,7 @@ function networkConnections(callback) {
} }
const hasState = states.indexOf(line[5]) >= 0; const hasState = states.indexOf(line[5]) >= 0;
let connstate = hasState ? line[5] : 'UNKNOWN'; let connstate = hasState ? line[5] : 'UNKNOWN';
let pid = parseInt(line[8 + (hasState ? 0 : -1)], 10); let pid = parseInt(line[8 + (hasState ? 0 : -1) + (hasTransferred ? 2 : 0)], 10);
if (connstate) { if (connstate) {
result.push({ result.push({
protocol: line[0], protocol: line[0],