extended windows support networkStats(), networkConnections()

This commit is contained in:
Sebastian Hildebrandt
2017-08-05 11:43:02 +02:00
parent ad1aee269f
commit 577ffff754
3 changed files with 94 additions and 26 deletions
+77 -11
View File
@@ -165,14 +165,12 @@ function networkStats(iface, callback) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
if (_windows) {
let error = new Error(NOT_SUPPORTED);
if (callback) { callback(NOT_SUPPORTED) }
reject(error);
}
_default_iface = _default_iface || getDefaultNetworkInterface();
iface = iface || _default_iface; // (_darwin ? 'en0' : 'eth0');
if (_windows) {
iface = 'all'
}
let result = {
iface: iface,
@@ -240,6 +238,24 @@ function networkStats(iface, callback) {
});
});
}
if (_windows) {
cmd = "netstat -e";
exec(cmd, function (error, stdout) {
const lines = stdout.split('\r\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].toLowerCase().startsWith('bytes')) {
const parts = lines[i].substr(5).trim().replace(/ +/g, " ").split(' ');
if (parts.length > 1) {
rx = parseInt(parts[0]);
tx = parseInt(parts[1]);
result = calcNetworkSpeed(iface, rx, tx, operstate);
}
}
}
if (callback) { callback(result) }
resolve(result);
});
}
} else {
result.rx = _network[iface].rx;
result.tx = _network[iface].tx;
@@ -262,12 +278,6 @@ function networkConnections(callback) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
if (_windows) {
let error = new Error(NOT_SUPPORTED);
if (callback) { callback(NOT_SUPPORTED) }
reject(error);
}
let result = [];
if (_linux) {
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'";
@@ -406,6 +416,62 @@ function networkConnections(callback) {
}
})
}
if (_windows) {
let cmd = "netstat -na";
exec(cmd, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\r\n');
lines.forEach(function (line) {
line = line.trim().replace(/ +/g, " ").split(' ');
if (line.length >= 4) {
let localip = line[1];
let localport = '';
let localaddress = line[1].split(':');
if (localaddress.length > 1) {
localport = localaddress[localaddress.length - 1];
localaddress.pop();
localip = localaddress.join(':');
}
let peerip = line[2];
let peerport = '';
let peeraddress = line[2].split(':');
if (peeraddress.length > 1) {
peerport = peeraddress[peeraddress.length - 1];
peeraddress.pop();
peerip = peeraddress.join(':');
}
let connstate = line[3];
if (connstate === 'HERGESTELLT') connstate = 'ESTABLISHED';
if (connstate.startsWith('ABH')) connstate = 'LISTEN';
if (connstate === 'SCHLIESSEN_WARTEN') connstate = 'CLOSE_WAIT';
if (connstate === 'WARTEND') connstate = 'TIME_WAIT';
if (connstate === 'SYN_GESENDET') connstate = 'SYN_SENT';
if (connstate === 'LISTENING') connstate = 'LISTEN';
if (connstate === 'SYN_RECEIVED') connstate = 'SYN_RECV';
if (connstate === 'FIN_WAIT_1') connstate = 'FIN_WAIT1';
if (connstate === 'FIN_WAIT_2') connstate = 'FIN_WAIT2';
if (connstate) {
result.push({
protocol: line[0].toLowerCase(),
localaddress: localip,
localport: localport,
peeraddress: peerip,
peerport: peerport,
state: connstate
})
}
}
});
if (callback) {
callback(result)
}
resolve(result);
}
})
}
});
});
}