Added traffic accounting to desktop multiplexor
Ylian Saint-Hilaire committed
May 5, 2021 at 13:17 UTC
a25ce3c0dbe6c0b1718a3c98dc208a44c1da6e34
2 files changed
+22
-6
meshagent.js
+2
-6
@@ -43,14 +43,10 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
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);
46
+ parent.trafficStats.AgentCtrlIn += (ws._socket.bytesRead - ws._socket.bytesReadEx);
47
+ parent.trafficStats.AgentCtrlOut += (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;
50
}
51
52
// Send a message to the mesh agent
meshdesktopmultiplex.js
+20
@@ -91,6 +91,9 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) {
91
obj.startTime = null; // Starting time of the multiplex session.
92
obj.userIds = []; // List of userid's that have intertracted with this session.
93
94
+ // Accounting
95
+ parent.trafficStats.desktopMultiplex.sessions++;
96
+
97
// Add an agent or viewer
98
obj.addPeer = function (peer) {
99
if (obj.viewers == null) { parent.parent.debug('relay', 'DesktopRelay: Error, addingPeer on disposed session'); return; }
@@ -877,6 +880,11 @@ function CreateMeshRelayEx2(parent, ws, req, domain, user, cookie) {
880
obj.req = req; // Used in multi-server.js
881
obj.viewOnly = ((cookie != null) && (cookie.vo == 1)); // set view only mode
882
883
+ // Setup traffic accounting
884
+ if (parent.trafficStats.desktopMultiplex == null) { parent.trafficStats.desktopMultiplex = { connections: 1, sessions: 0, in: 0, out: 0 }; } else { parent.trafficStats.desktopMultiplex.connections++; }
885
+ ws._socket.bytesReadEx = 0;
886
+ ws._socket.bytesWrittenEx = 0;
887
+
888
// Setup subscription for desktop sharing public identifier
889
// If the identifier is removed, drop the connection
890
if ((cookie != null) && (typeof cookie.pid == 'string')) {
@@ -1067,6 +1075,12 @@ function CreateMeshRelayEx2(parent, ws, req, domain, user, cookie) {
1075
1076
// When data is received from the mesh relay web socket
1077
ws.on('message', function (data) {
1078
+ // Data accounting
1079
+ parent.trafficStats.desktopMultiplex.in += (this._socket.bytesRead - this._socket.bytesReadEx);
1080
+ parent.trafficStats.desktopMultiplex.out += (this._socket.bytesWritten - this._socket.bytesWrittenEx);
1081
+ this._socket.bytesReadEx = this._socket.bytesRead;
1082
+ this._socket.bytesWrittenEx = this._socket.bytesWritten;
1083
+
1084
// If this data was received by the agent, decode it.
1085
if (this.me.deskMultiplexor != null) { this.me.deskMultiplexor.processData(this.me, data); }
1086
});
@@ -1081,6 +1095,12 @@ function CreateMeshRelayEx2(parent, ws, req, domain, user, cookie) {
1095
1096
// If the relay web socket is closed, close both sides.
1097
ws.on('close', function (req) {
1098
+ // Data accounting
1099
+ parent.trafficStats.desktopMultiplex.in += (this._socket.bytesRead - this._socket.bytesReadEx);
1100
+ parent.trafficStats.desktopMultiplex.out += (this._socket.bytesWritten - this._socket.bytesWrittenEx);
1101
+ this._socket.bytesReadEx = this._socket.bytesRead;
1102
+ this._socket.bytesWrittenEx = this._socket.bytesWritten;
1103
+
1104
//console.log('ws-close', req);
1105
obj.close();
1106
});