Skip to content

Commit

Permalink
networkConnections: improved parsing (macOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebhildebrandt committed Dec 13, 2024
1 parent 81d8167 commit e91f61d
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -1534,15 +1534,17 @@ function networkConnections(callback) {
});
}
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"';
let cmd = 'netstat -natvln | grep "tcp4\\|tcp6\\|udp4\\|udp6\\|Recv-Q"';
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) {
if (!error) {
exec('ps -axo pid,command', { maxBuffer: 1024 * 20000 }, function (err2, stdout2) {
let processes = stdout2.toString().split('\n');
processes = processes.map((line => { return line.trim().replace(/ +/g, ' '); }));
let lines = stdout.toString().split('\n');
const header = lines[0];
const hasTransferred = header.indexOf('rxbytes') > -1;
lines.shift();

lines.forEach(function (line) {
line = line.replace(/ +/g, ' ').split(' ');
Expand All @@ -1565,7 +1567,7 @@ function networkConnections(callback) {
}
const hasState = states.indexOf(line[5]) >= 0;
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) {
result.push({
protocol: line[0],
Expand Down

0 comments on commit e91f61d

Please sign in to comment.