From 29203851d085018ab205d2c4af91e392fa18d7a8 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Sun, 30 Jun 2019 21:25:41 +0200 Subject: [PATCH] networkConnections added PID, process --- lib/index.d.ts | 2 ++ lib/network.js | 47 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index d4df0f7..e874fe4 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -336,6 +336,8 @@ export namespace Systeminformation { peeraddress: string; peerport: string; state: string; + pid: number; + process: string; } interface InetChecksiteData { diff --git a/lib/network.js b/lib/network.js index 59e99a3..e673fa9 100644 --- a/lib/network.js +++ b/lib/network.js @@ -74,7 +74,7 @@ function getDefaultNetworkInterface() { defaultIp = parts[parts.length - 2]; } } - }) + }); if (defaultIp) { for (let dev in ifaces) { if (ifaces.hasOwnProperty(dev)) { @@ -305,7 +305,7 @@ function getDarwinNics() { } 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) { return virtualMacs.filter(item => { return mac.toUpperCase().toUpperCase().startsWith(item.substr(0, mac.length)); }).length > 0 || iface.toLowerCase().indexOf(' virtual ') > -1 || @@ -778,8 +778,8 @@ function networkConnections(callback) { process.nextTick(() => { let result = []; 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"'; - 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"'; + 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 = '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) { if (!error) { let lines = stdout.toString().split('\n'); @@ -803,7 +803,9 @@ function networkConnections(callback) { peerip = peeraddress.join(':'); } let connstate = line[5]; - if (connstate === 'VERBUNDEN') connstate = 'ESTABLISHED'; + // if (connstate === 'VERBUNDEN') connstate = 'ESTABLISHED'; + let proc = line[6].split('/'); + if (connstate) { result.push({ protocol: line[0], @@ -811,7 +813,9 @@ function networkConnections(callback) { localport: localport, peeraddress: peerip, 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); } 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) { if (!error) { @@ -848,6 +852,15 @@ function networkConnections(callback) { let connstate = line[1]; if (connstate === 'ESTAB') connstate = 'ESTABLISHED'; 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) { result.push({ protocol: line[0], @@ -855,7 +868,9 @@ function networkConnections(callback) { localport: localport, peeraddress: peerip, peerport: peerport, - state: connstate + state: connstate, + pid, + process }); } } @@ -870,7 +885,7 @@ function networkConnections(callback) { }); } 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) { if (!error) { @@ -878,7 +893,7 @@ function networkConnections(callback) { lines.forEach(function (line) { line = line.replace(/ +/g, ' ').split(' '); - if (line.length >= 6) { + if (line.length >= 8) { let localip = line[3]; let localport = ''; let localaddress = line[3].split('.'); @@ -896,6 +911,7 @@ function networkConnections(callback) { peerip = peeraddress.join('.'); } let connstate = line[5]; + let pid = parseInt(line[8], 10); if (connstate) { result.push({ protocol: line[0], @@ -903,7 +919,9 @@ function networkConnections(callback) { localport: localport, peeraddress: peerip, peerport: peerport, - state: connstate + state: connstate, + pid: pid, + process: '' }); } } @@ -916,7 +934,7 @@ function networkConnections(callback) { }); } if (_windows) { - let cmd = 'netstat -na'; + let cmd = 'netstat -nao'; try { exec(cmd, util.execOptsWin, function (error, stdout) { if (!error) { @@ -942,6 +960,7 @@ function networkConnections(callback) { peeraddress.pop(); peerip = peeraddress.join(':'); } + let pid = line[4]; let connstate = line[3]; if (connstate === 'HERGESTELLT') connstate = 'ESTABLISHED'; if (connstate.startsWith('ABH')) connstate = 'LISTEN'; @@ -960,7 +979,9 @@ function networkConnections(callback) { localport: localport, peeraddress: peerip, peerport: peerport, - state: connstate + state: connstate, + pid, + process: '' }); } }