networkConnections added PID, process

This commit is contained in:
Sebastian Hildebrandt 2019-06-30 21:25:41 +02:00
parent dbd51d91d7
commit 29203851d0
2 changed files with 36 additions and 13 deletions

2
lib/index.d.ts vendored
View File

@ -336,6 +336,8 @@ export namespace Systeminformation {
peeraddress: string; peeraddress: string;
peerport: string; peerport: string;
state: string; state: string;
pid: number;
process: string;
} }
interface InetChecksiteData { interface InetChecksiteData {

View File

@ -74,7 +74,7 @@ function getDefaultNetworkInterface() {
defaultIp = parts[parts.length - 2]; defaultIp = parts[parts.length - 2];
} }
} }
}) });
if (defaultIp) { if (defaultIp) {
for (let dev in ifaces) { for (let dev in ifaces) {
if (ifaces.hasOwnProperty(dev)) { if (ifaces.hasOwnProperty(dev)) {
@ -305,7 +305,7 @@ function getDarwinNics() {
} }
function testVirtualNic(iface, ifaceName, mac) { function testVirtualNic(iface, ifaceName, mac) {
const virtualMacs = ['00:00:00:00:00:00', '00:03:FF', '00:05:69', '00:0C:29', '00:0F:4B', '00:0F:4B', '00:13:07', '00:13:BE', '00:15:5d', '00:16:3E', '00:1C:42', '00:21:F6', '00:21:F6', '00:24:0B', '00:24:0B', '00:50:56', '00:A0:B1', '00:E0:C8', '08:00:27', '0A:00:27', '18:92:2C', '16:DF:49', '3C:F3:92', '54:52:00', 'FC:15:97'] const virtualMacs = ['00:00:00:00:00:00', '00:03:FF', '00:05:69', '00:0C:29', '00:0F:4B', '00:0F:4B', '00:13:07', '00:13:BE', '00:15:5d', '00:16:3E', '00:1C:42', '00:21:F6', '00:21:F6', '00:24:0B', '00:24:0B', '00:50:56', '00:A0:B1', '00:E0:C8', '08:00:27', '0A:00:27', '18:92:2C', '16:DF:49', '3C:F3:92', '54:52:00', 'FC:15:97'];
if (mac) { if (mac) {
return virtualMacs.filter(item => { return mac.toUpperCase().toUpperCase().startsWith(item.substr(0, mac.length)); }).length > 0 || return virtualMacs.filter(item => { return mac.toUpperCase().toUpperCase().startsWith(item.substr(0, mac.length)); }).length > 0 ||
iface.toLowerCase().indexOf(' virtual ') > -1 || iface.toLowerCase().indexOf(' virtual ') > -1 ||
@ -778,8 +778,8 @@ function networkConnections(callback) {
process.nextTick(() => { process.nextTick(() => {
let result = []; let result = [];
if (_linux || _freebsd || _openbsd || _netbsd) { if (_linux || _freebsd || _openbsd || _netbsd) {
let cmd = 'netstat -tuna | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"'; 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';
if (_freebsd || _openbsd || _netbsd) cmd = 'netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"'; 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 * 20000 }, function (error, stdout) { exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
if (!error) { if (!error) {
let lines = stdout.toString().split('\n'); let lines = stdout.toString().split('\n');
@ -803,7 +803,9 @@ function networkConnections(callback) {
peerip = peeraddress.join(':'); peerip = peeraddress.join(':');
} }
let connstate = line[5]; let connstate = line[5];
if (connstate === 'VERBUNDEN') connstate = 'ESTABLISHED'; // if (connstate === 'VERBUNDEN') connstate = 'ESTABLISHED';
let proc = line[6].split('/');
if (connstate) { if (connstate) {
result.push({ result.push({
protocol: line[0], protocol: line[0],
@ -811,7 +813,9 @@ function networkConnections(callback) {
localport: localport, localport: localport,
peeraddress: peerip, peeraddress: peerip,
peerport: peerport, peerport: peerport,
state: connstate state: connstate,
pid: proc[0] ? parseInt(proc[0], 10) : -1,
process: proc[1] ? proc[1].split(' ')[0] : ''
}); });
} }
} }
@ -821,7 +825,7 @@ function networkConnections(callback) {
} }
resolve(result); resolve(result);
} else { } else {
cmd = 'ss -tuna | 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 * 20000 }, function (error, stdout) { exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
if (!error) { if (!error) {
@ -848,6 +852,15 @@ function networkConnections(callback) {
let connstate = line[1]; let connstate = line[1];
if (connstate === 'ESTAB') connstate = 'ESTABLISHED'; if (connstate === 'ESTAB') connstate = 'ESTABLISHED';
if (connstate === 'TIME-WAIT') connstate = 'TIME_WAIT'; if (connstate === 'TIME-WAIT') connstate = 'TIME_WAIT';
let pid = -1;
let process = '';
if (line.length >= 7 && line[6].indexOf('users:') > -1) {
let proc = line[6].replace('users:(("', '').replace('"', '').split(',');
if (proc.length > 2) {
process = proc[0].split(' ')[0];
pid = parseInt(proc[1], 10);
}
}
if (connstate) { if (connstate) {
result.push({ result.push({
protocol: line[0], protocol: line[0],
@ -855,7 +868,9 @@ function networkConnections(callback) {
localport: localport, localport: localport,
peeraddress: peerip, peeraddress: peerip,
peerport: peerport, peerport: peerport,
state: connstate state: connstate,
pid,
process
}); });
} }
} }
@ -870,7 +885,7 @@ function networkConnections(callback) {
}); });
} }
if (_darwin) { if (_darwin) {
let cmd = 'netstat -nat | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"'; let cmd = 'netstat -natv | grep "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) { exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) {
if (!error) { if (!error) {
@ -878,7 +893,7 @@ function networkConnections(callback) {
lines.forEach(function (line) { lines.forEach(function (line) {
line = line.replace(/ +/g, ' ').split(' '); line = line.replace(/ +/g, ' ').split(' ');
if (line.length >= 6) { if (line.length >= 8) {
let localip = line[3]; let localip = line[3];
let localport = ''; let localport = '';
let localaddress = line[3].split('.'); let localaddress = line[3].split('.');
@ -896,6 +911,7 @@ function networkConnections(callback) {
peerip = peeraddress.join('.'); peerip = peeraddress.join('.');
} }
let connstate = line[5]; let connstate = line[5];
let pid = parseInt(line[8], 10);
if (connstate) { if (connstate) {
result.push({ result.push({
protocol: line[0], protocol: line[0],
@ -903,7 +919,9 @@ function networkConnections(callback) {
localport: localport, localport: localport,
peeraddress: peerip, peeraddress: peerip,
peerport: peerport, peerport: peerport,
state: connstate state: connstate,
pid: pid,
process: ''
}); });
} }
} }
@ -916,7 +934,7 @@ function networkConnections(callback) {
}); });
} }
if (_windows) { if (_windows) {
let cmd = 'netstat -na'; let cmd = 'netstat -nao';
try { try {
exec(cmd, util.execOptsWin, function (error, stdout) { exec(cmd, util.execOptsWin, function (error, stdout) {
if (!error) { if (!error) {
@ -942,6 +960,7 @@ function networkConnections(callback) {
peeraddress.pop(); peeraddress.pop();
peerip = peeraddress.join(':'); peerip = peeraddress.join(':');
} }
let pid = line[4];
let connstate = line[3]; let connstate = line[3];
if (connstate === 'HERGESTELLT') connstate = 'ESTABLISHED'; if (connstate === 'HERGESTELLT') connstate = 'ESTABLISHED';
if (connstate.startsWith('ABH')) connstate = 'LISTEN'; if (connstate.startsWith('ABH')) connstate = 'LISTEN';
@ -960,7 +979,9 @@ function networkConnections(callback) {
localport: localport, localport: localport,
peeraddress: peerip, peeraddress: peerip,
peerport: peerport, peerport: peerport,
state: connstate state: connstate,
pid,
process: ''
}); });
} }
} }