From 82dc7a456ff68b3fe2f23e0194be5f789f7e404b Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Wed, 5 May 2021 02:30:23 -0700 Subject: [PATCH] Added agent control channel data accounting. --- meshagent.js | 18 ++++++++++++++++++ webserver.js | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/meshagent.js b/meshagent.js index 870f5311..124df2a6 100644 --- a/meshagent.js +++ b/meshagent.js @@ -38,12 +38,29 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { //obj.connectTime = null; //obj.agentInfo = null; + ws._socket.bytesReadEx = 0; + ws._socket.bytesWrittenEx = 0; + + // Perform data accounting + function dataAccounting() { + const datain = (ws._socket.bytesRead - ws._socket.bytesReadEx); + const dataout = (ws._socket.bytesWritten - ws._socket.bytesWrittenEx); + ws._socket.bytesReadEx = ws._socket.bytesRead; + ws._socket.bytesWrittenEx = ws._socket.bytesWritten; + + // Add to counters + parent.trafficStats.AgentCtrlIn += datain; + parent.trafficStats.AgentCtrlOut += dataout; + } + // Send a message to the mesh agent obj.send = function (data, func) { try { if (typeof data == 'string') { ws.send(Buffer.from(data), func); } else { ws.send(data, func); } } catch (e) { } }; obj.sendBinary = function (data, func) { try { if (typeof data == 'string') { ws.send(Buffer.from(data, 'binary'), func); } else { ws.send(data, func); } } catch (e) { } }; // Disconnect this agent obj.close = function (arg) { + dataAccounting(); + if ((arg == 1) || (arg == null)) { try { ws.close(); if (obj.nodeid != null) { parent.parent.debug('agent', 'Soft disconnect ' + obj.nodeid + ' (' + obj.remoteaddrport + ')'); } } catch (e) { console.log(e); } } // Soft close, close the websocket if (arg == 2) { try { ws._socket._parent.end(); if (obj.nodeid != null) { parent.parent.debug('agent', 'Hard disconnect ' + obj.nodeid + ' (' + obj.remoteaddrport + ')'); } } catch (e) { console.log(e); } } // Hard close, close the TCP socket // If arg == 3, don't communicate with this agent anymore, but don't disconnect (Duplicate agent). @@ -125,6 +142,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { // When data is received from the mesh agent web socket ws.on('message', function (msg) { + dataAccounting(); if (msg.length < 2) return; if (typeof msg == 'object') { msg = msg.toString('binary'); } // TODO: Could change this entire method to use Buffer instead of binary string if (obj.authenticated == 2) { // We are authenticated diff --git a/webserver.js b/webserver.js index 374293df..25daee82 100644 --- a/webserver.js +++ b/webserver.js @@ -350,7 +350,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) { relayOut: {}, localRelayCount: {}, localRelayIn: {}, - localRelayOut: {} + localRelayOut: {}, + AgentCtrlIn: 0, + AgentCtrlOut: 0 } obj.getTrafficStats = function () { return obj.trafficStats; }