networkConnections(): added process name (mac OS)

This commit is contained in:
Sebastian Hildebrandt
2022-11-17 22:10:30 +01:00
parent 2e1cfb0a68
commit 1001e1e14c
8 changed files with 74 additions and 46 deletions
+1 -1
View File
@@ -1088,7 +1088,7 @@ function cpuTemperature(callback) {
}
} else if (section === 'pch') {
// chipset temp
if (firstPart.indexOf('TEMP') !== -1) {
if (firstPart.indexOf('TEMP') !== -1 && !result.chipset) {
result.chipset = parseFloat(temps);
}
}
+1 -1
View File
@@ -339,7 +339,7 @@ function parseBlk(lines) {
'physical': (disk.type === 'disk' ? (disk.rota === '0' ? 'SSD' : 'HDD') : (disk.type === 'rom' ? 'CD/DVD' : '')),
'uuid': disk.uuid,
'label': disk.label,
'model': disk.model,
'model': (disk.model || '').trim(),
'serial': disk.serial,
'removable': disk.rm === '1',
'protocol': disk.tran,
+61 -41
View File
@@ -1369,6 +1369,22 @@ exports.networkStats = networkStats;
// --------------------------
// NET - connections (sockets)
function getProcessName(processes, pid) {
let cmd = '';
processes.forEach(line => {
const parts = line.split(' ');
const id = parseInt(parts[0], 10) || -1;
if (id === pid) {
parts.shift();
cmd = parts.join(' ').split(':')[0];
}
});
cmd = cmd.split(' -')[0];
// return cmd;
const cmdParts = cmd.split('/');
return cmdParts[cmdParts.length - 1];
}
function networkConnections(callback) {
return new Promise((resolve) => {
@@ -1411,7 +1427,7 @@ function networkConnections(callback) {
peerPort: peerport,
state: connstate,
pid: proc[0] && proc[0] !== '-' ? parseInt(proc[0], 10) : null,
process: proc[1] ? proc[1].split(' ')[0] : ''
process: proc[1] ? proc[1].split(' ')[0].split(':')[0] : ''
});
}
}
@@ -1453,7 +1469,7 @@ function networkConnections(callback) {
if (line.length >= 7 && line[6].indexOf('users:') > -1) {
let proc = line[6].replace('users:(("', '').replace(/"/g, '').split(',');
if (proc.length > 2) {
process = proc[0].split(' ')[0];
process = proc[0].split(' ')[0].split(':')[0];
pid = parseInt(proc[1], 10);
}
}
@@ -1486,49 +1502,53 @@ function networkConnections(callback) {
const states = 'ESTABLISHED|SYN_SENT|SYN_RECV|FIN_WAIT1|FIN_WAIT2|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');
let lines = stdout.toString().split('\n');
lines.forEach(function (line) {
line = line.replace(/ +/g, ' ').split(' ');
if (line.length >= 8) {
let localip = line[3];
let localport = '';
let localaddress = line[3].split('.');
if (localaddress.length > 1) {
localport = localaddress[localaddress.length - 1];
localaddress.pop();
localip = localaddress.join('.');
}
let peerip = line[4];
let peerport = '';
let 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';
let pid = parseInt(line[8 + (hasState ? 0 : -1)], 10);
if (connstate) {
result.push({
protocol: line[0],
localAddress: localip,
localPort: localport,
peerAddress: peerip,
peerPort: peerport,
state: connstate,
pid: pid,
process: ''
});
lines.forEach(function (line) {
line = line.replace(/ +/g, ' ').split(' ');
if (line.length >= 8) {
let localip = line[3];
let localport = '';
let localaddress = line[3].split('.');
if (localaddress.length > 1) {
localport = localaddress[localaddress.length - 1];
localaddress.pop();
localip = localaddress.join('.');
}
let peerip = line[4];
let peerport = '';
let 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';
let pid = parseInt(line[8 + (hasState ? 0 : -1)], 10);
if (connstate) {
result.push({
protocol: line[0],
localAddress: localip,
localPort: localport,
peerAddress: peerip,
peerPort: peerport,
state: connstate,
pid: pid,
process: getProcessName(processes, pid)
});
}
}
});
if (callback) {
callback(result);
}
resolve(result);
});
if (callback) {
callback(result);
}
resolve(result);
}
});
}