Added HTTP traffic accounting.
This commit is contained in:
parent
3590290020
commit
03becc3f0f
30
webserver.js
30
webserver.js
|
@ -345,6 +345,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
|
||||||
// Traffic counters
|
// Traffic counters
|
||||||
obj.trafficStats = {
|
obj.trafficStats = {
|
||||||
httpRequestCount: 0,
|
httpRequestCount: 0,
|
||||||
|
httpWebSocketCount: 0,
|
||||||
|
httpIn: 0,
|
||||||
|
httpOut: 0,
|
||||||
relayCount: {},
|
relayCount: {},
|
||||||
relayIn: {},
|
relayIn: {},
|
||||||
relayOut: {},
|
relayOut: {},
|
||||||
|
@ -5267,8 +5270,31 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
|
||||||
// Useful for debugging reverse proxy issues
|
// Useful for debugging reverse proxy issues
|
||||||
parent.debug('httpheaders', req.method, req.url, req.headers);
|
parent.debug('httpheaders', req.method, req.url, req.headers);
|
||||||
|
|
||||||
// Count the HTTP request
|
// Perform traffic accounting
|
||||||
obj.trafficStats.httpRequestCount++;
|
if (req.headers.upgrade == 'websocket') {
|
||||||
|
// We don't count traffic on WebSockets since it's counted by the handling modules.
|
||||||
|
obj.trafficStats.httpWebSocketCount++;
|
||||||
|
} else {
|
||||||
|
// Normal HTTP traffic is counted
|
||||||
|
obj.trafficStats.httpRequestCount++;
|
||||||
|
if (typeof req.socket.xbytesRead != 'number') {
|
||||||
|
req.socket.xbytesRead = 0;
|
||||||
|
req.socket.xbytesWritten = 0;
|
||||||
|
req.socket.on('close', function () {
|
||||||
|
// Perform final accounting
|
||||||
|
obj.trafficStats.httpIn += (this.bytesRead - this.xbytesRead);
|
||||||
|
obj.trafficStats.httpOut += (this.bytesWritten - this.xbytesWritten);
|
||||||
|
this.xbytesRead = this.bytesRead;
|
||||||
|
this.xbytesWritten = this.bytesWritten;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// Update counters
|
||||||
|
obj.trafficStats.httpIn += (req.socket.bytesRead - req.socket.xbytesRead);
|
||||||
|
obj.trafficStats.httpOut += (req.socket.bytesWritten - req.socket.xbytesWritten);
|
||||||
|
req.socket.xbytesRead = req.socket.bytesRead;
|
||||||
|
req.socket.xbytesWritten = req.socket.bytesWritten;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set the real IP address of the request
|
// Set the real IP address of the request
|
||||||
// If a trusted reverse-proxy is sending us the remote IP address, use it.
|
// If a trusted reverse-proxy is sending us the remote IP address, use it.
|
||||||
|
|
Loading…
Reference in New Issue