Started work on traffic accounting.

Ylian Saint-Hilaire committed May 5, 2021 at 02:24 UTC b6e3b51be24e3fd0a7b54038c526c80f04798fb3
3 files changed +57 -2
meshrelay.js
+34 -1
@@ -966,6 +966,10 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
966 obj.ws = ws;
967 obj.user = user;
968
969 + // Check the protocol in use
970 + var protocolInUse = parseInt(req.query.p);
971 + if (typeof protocolInUse != 'number') { protocolInUse = 0; }
972 +
973 // If there is no authentication, drop this connection
974 if (obj.user == null) { try { ws.close(); parent.parent.debug('relay', 'Relay: Connection with no authentication'); } catch (e) { console.log(e); } return; }
975
@@ -997,6 +1001,8 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
1001
1002 // Hold traffic until we connect to the target
1003 ws._socket.pause();
1004 + ws._socket.bytesReadEx = 0;
1005 + ws._socket.bytesWrittenEx = 0;
1006
1007 // Mesh Rights
1008 const MESHRIGHT_EDITMESH = 1;
@@ -1020,11 +1026,28 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
1026 // Clean a IPv6 address that encodes a IPv4 address
1027 function cleanRemoteAddr(addr) { if (addr.startsWith('::ffff:')) { return addr.substring(7); } else { return addr; } }
1028
1029 + // Perform data accounting
1030 + function dataAccounting() {
1031 + const datain = ((obj.client.bytesRead - obj.client.bytesReadEx) + (ws._socket.bytesRead - ws._socket.bytesReadEx));
1032 + const dataout = ((obj.client.bytesWritten - obj.client.bytesWrittenEx) + (ws._socket.bytesWritten - ws._socket.bytesWrittenEx));
1033 + obj.client.bytesReadEx = obj.client.bytesRead;
1034 + obj.client.bytesWrittenEx = obj.client.bytesWritten;
1035 + ws._socket.bytesReadEx = ws._socket.bytesRead;
1036 + ws._socket.bytesWrittenEx = ws._socket.bytesWritten;
1037 +
1038 + // Add to counters
1039 + if (parent.trafficStats.localRelayIn[protocolInUse]) { parent.trafficStats.localRelayIn[protocolInUse] += datain; } else { parent.trafficStats.localRelayIn[protocolInUse] = datain; }
1040 + if (parent.trafficStats.localRelayOut[protocolInUse]) { parent.trafficStats.localRelayOut[protocolInUse] += dataout; } else { parent.trafficStats.localRelayOut[protocolInUse] = dataout; }
1041 + }
1042 +
1043 // Disconnect
1044 obj.close = function (arg) {
1045 // If the web socket is already closed, stop here.
1046 if (obj.ws == null) return;
1047
1048 + // Perform data accounting
1049 + dataAccounting();
1050 +
1051 // Collect how many raw bytes where received and sent.
1052 // We sum both the websocket and TCP client in this case.
1053 var inTraffc = obj.ws._socket.bytesRead, outTraffc = obj.ws._socket.bytesWritten;
@@ -1083,17 +1106,27 @@ function CreateLocalRelayEx(parent, ws, req, domain, user, cookie) {
1106
1107 // Setup TCP client
1108 obj.client = new net.Socket();
1109 + obj.client.bytesReadEx = 0;
1110 + obj.client.bytesWrittenEx = 0;
1111 obj.client.connect(obj.tcpport, node.host, function () {
1112 // Log the start of the connection
1113 obj.time = Date.now();
1114 var event = { etype: 'relay', action: 'relaylog', domain: domain.id, userid: obj.user._id, username: obj.user.name, msgid: 13, msgArgs: [obj.id, obj.req.clientIp, obj.host], msg: 'Started relay session \"' + obj.id + '\" from ' + obj.req.clientIp + ' to ' + obj.host, nodeid: req.query.nodeid, protocol: req.query.p };
1115 parent.parent.DispatchEvent(['*', obj.user._id, obj.meshid, obj.nodeid], obj, event);
1116
1117 + // Count the session
1118 + if (parent.trafficStats.localRelayCount[protocolInUse]) { parent.trafficStats.localRelayCount[protocolInUse] += 1; } else { parent.trafficStats.localRelayCount[protocolInUse] = 1; }
1119 +
1120 // Start the session
1121 ws.send('c');
1122 ws._socket.resume();
1123 });
1096 - obj.client.on('data', function (data) { try { this.pause(); ws.send(data, this.clientResume); } catch (ex) { console.log(ex); } }); // Perform relay
1124 + obj.client.on('data', function (data) {
1125 + // Perform data accounting
1126 + dataAccounting();
1127 + // Perform relay
1128 + try { this.pause(); ws.send(data, this.clientResume); } catch (ex) { console.log(ex); }
1129 + });
1130 obj.client.on('close', function () { obj.close(); });
1131 obj.client.on('error', function (err) { obj.close(); });
1132 obj.client.clientResume = function () { try { obj.client.resume(); } catch (ex) { console.log(ex); } };
meshuser.js
+8 -1
@@ -905,7 +905,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
905
906 switch (cmd) {
907 case 'help': {
908 - var fin = '', f = '', availcommands = 'help,maintenance,info,versions,resetserver,usersessions,closeusersessions,tasklimiter,setmaxtasks,cores,migrationagents,agentstats,agentissues,webstats,mpsstats,swarmstats,acceleratorsstats,updatecheck,serverupdate,nodeconfig,heapdump,relays,autobackup,backupconfig,dupagents,dispatchtable,badlogins,showpaths,le,lecheck,leevents,dbstats,dbcounters,sms,amtacm,certhashes,watchdog,amtmanager,amtpasswords,certexpire';
908 + var fin = '', f = '', availcommands = 'help,maintenance,info,versions,resetserver,usersessions,closeusersessions,tasklimiter,setmaxtasks,cores,migrationagents,agentstats,agentissues,webstats,trafficstats,mpsstats,swarmstats,acceleratorsstats,updatecheck,serverupdate,nodeconfig,heapdump,relays,autobackup,backupconfig,dupagents,dispatchtable,badlogins,showpaths,le,lecheck,leevents,dbstats,dbcounters,sms,amtacm,certhashes,watchdog,amtmanager,amtpasswords,certexpire';
909 if (parent.parent.config.settings.heapdump === true) { availcommands += ',heapdump'; }
910 availcommands = availcommands.split(',').sort();
911 while (availcommands.length > 0) { if (f.length > 80) { fin += (f + ',\r\n'); f = ''; } f += (((f != '') ? ', ' : ' ') + availcommands.shift()); }
@@ -1113,6 +1113,13 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1113 }
1114 break;
1115 }
1116 + case 'trafficstats': {
1117 + var stats = parent.getTrafficStats();
1118 + for (var i in stats) {
1119 + if (typeof stats[i] == 'object') { r += (i + ': ' + JSON.stringify(stats[i]) + '\r\n'); } else { r += (i + ': ' + stats[i] + '\r\n'); }
1120 + }
1121 + break;
1122 + }
1123 case 'watchdog': {
1124 if (parent.parent.watchdog == null) {
1125 r = 'Server watchdog not active.';
webserver.js
+15
@@ -342,6 +342,18 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
342 }
343 obj.getAgentStats = function () { return obj.agentStats; }
344
345 + // Traffic counters
346 + obj.trafficStats = {
347 + httpRequestCount: 0,
348 + relayCount: {},
349 + relayIn: {},
350 + relayOut: {},
351 + localRelayCount: {},
352 + localRelayIn: {},
353 + localRelayOut: {}
354 + }
355 + obj.getTrafficStats = function () { return obj.trafficStats; }
356 +
357 // Keep a record of the last agent issues.
358 obj.getAgentIssues = function () { return obj.agentIssues; }
359 obj.setAgentIssue = function (agent, issue) { obj.agentIssues.push([new Date().toLocaleTimeString(), agent.remoteaddrport, issue]); while (obj.setAgentIssue.length > 50) { obj.agentIssues.shift(); } }
@@ -5327,6 +5339,9 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
5339 // Useful for debugging reverse proxy issues
5340 parent.debug('httpheaders', req.method, req.url, req.headers);
5341
5342 + // Count the HTTP request
5343 + obj.trafficStats.httpRequestCount++;
5344 +
5345 // Set the real IP address of the request
5346 // If a trusted reverse-proxy is sending us the remote IP address, use it.
5347 var ipex = '0.0.0.0', xforwardedhost = req.headers.host;