Added agent control channel data accounting.

Ylian Saint-Hilaire committed May 5, 2021 at 02:30 UTC 82dc7a456ff68b3fe2f23e0194be5f789f7e404b
2 files changed +21 -1
meshagent.js
+18
@@ -38,12 +38,29 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
38 //obj.connectTime = null;
39 //obj.agentInfo = null;
40
41 + ws._socket.bytesReadEx = 0;
42 + ws._socket.bytesWrittenEx = 0;
43 +
44 + // Perform data accounting
45 + function dataAccounting() {
46 + const datain = (ws._socket.bytesRead - ws._socket.bytesReadEx);
47 + const dataout = (ws._socket.bytesWritten - ws._socket.bytesWrittenEx);
48 + ws._socket.bytesReadEx = ws._socket.bytesRead;
49 + ws._socket.bytesWrittenEx = ws._socket.bytesWritten;
50 +
51 + // Add to counters
52 + parent.trafficStats.AgentCtrlIn += datain;
53 + parent.trafficStats.AgentCtrlOut += dataout;
54 + }
55 +
56 // Send a message to the mesh agent
57 obj.send = function (data, func) { try { if (typeof data == 'string') { ws.send(Buffer.from(data), func); } else { ws.send(data, func); } } catch (e) { } };
58 obj.sendBinary = function (data, func) { try { if (typeof data == 'string') { ws.send(Buffer.from(data, 'binary'), func); } else { ws.send(data, func); } } catch (e) { } };
59
60 // Disconnect this agent
61 obj.close = function (arg) {
62 + dataAccounting();
63 +
64 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
65 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
66 // 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) {
142
143 // When data is received from the mesh agent web socket
144 ws.on('message', function (msg) {
145 + dataAccounting();
146 if (msg.length < 2) return;
147 if (typeof msg == 'object') { msg = msg.toString('binary'); } // TODO: Could change this entire method to use Buffer instead of binary string
148 if (obj.authenticated == 2) { // We are authenticated
webserver.js
+3 -1
@@ -350,7 +350,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
350 relayOut: {},
351 localRelayCount: {},
352 localRelayIn: {},
353 - localRelayOut: {}
353 + localRelayOut: {},
354 + AgentCtrlIn: 0,
355 + AgentCtrlOut: 0
356 }
357 obj.getTrafficStats = function () { return obj.trafficStats; }
358