From 663d1fb7223d483e93003b5a914787add360841b Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Sun, 12 Sep 2021 13:03:03 -0700 Subject: [PATCH] Improved events and reports. --- meshdesktopmultiplex.js | 44 +++++++++++++++++++++++++++++++++++++--- meshuser.js | 17 +++++++++------- views/default.handlebars | 7 ++++--- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/meshdesktopmultiplex.js b/meshdesktopmultiplex.js index f25571ff..7e53742d 100644 --- a/meshdesktopmultiplex.js +++ b/meshdesktopmultiplex.js @@ -186,11 +186,12 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) { if (obj.viewers == null) return; if (peer == obj.agent) { //console.log('removePeer-agent', obj.nodeid); + // Agent has disconnected, disconnect everyone. + if (obj.viewers != null) { for (var i in obj.viewers) { obj.viewers[i].close(); } } + // Clean up the agent obj.agent = null; - // Agent has disconnected, disconnect everyone. - if (obj.viewers != null) { for (var i in obj.viewers) { obj.viewers[i].close(); } } dispose(); return true; } else { @@ -211,9 +212,46 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) { // Log leaving the multiplex session if (obj.startTime != null) { // Used to check if the agent has connected. If not, don't log this event since the session never really started. + // In this code, we want to compute the share of in/out traffic that belongs to this viewer. It includes all of the viewers in/out traffic + all or a portion of the agents in/out traffic. + // The agent traffic needs to get divided out between the viewers fairly. For the time that multiple viewers are present, the agent traffic is divided between the viewers. + + // Compute traffic to and from the browser + var inTraffc, outTraffc; + try { inTraffc = peer.ws._socket.bytesRead; } catch (ex) { } + try { outTraffc = peer.ws._socket.bytesWritten; } catch (ex) { } + + // Add any previous agent traffic accounting + if (peer.agentInTraffic) { inTraffc += peer.agentInTraffic; } + if (peer.outTraffc) { inTraffc += peer.agentOutTraffic; } + + // Compute traffic to and from the agent + if (obj.agent != null) { + // Get unaccounted bytes from/to the agent + var agentInTraffc, agentOutTraffc, agentInTraffc2, agentOutTraffc2; + try { agentInTraffc = agentInTraffc2 = obj.agent.ws._socket.bytesRead; } catch (ex) { } + try { agentOutTraffc = agentOutTraffc2 = obj.agent.ws._socket.bytesWritten; } catch (ex) { } + if (obj.agent.accountedBytesRead) { agentInTraffc -= obj.agent.accountedBytesRead; } + if (obj.agent.accountedBytesWritten) { agentOutTraffc -= obj.agent.accountedBytesWritten; } + obj.agent.accountedBytesRead = agentInTraffc2; + obj.agent.accountedBytesWritten = agentOutTraffc2; + + // Devide up the agent traffic amoung the viewers + var viewerPartIn = Math.floor(agentInTraffc / (obj.viewers.length + 1)); + var viewerPartOut = Math.floor(agentOutTraffc / (obj.viewers.length + 1)); + + // Add the portion to this viewer and all other viewer + inTraffc += viewerPartIn; + outTraffc += viewerPartOut; + for (var i in obj.viewers) { + if (obj.viewers[i].agentInTraffic) { obj.viewers[i].agentInTraffic += viewerPartIn; } else { obj.viewers[i].agentInTraffic = viewerPartIn; } + if (obj.viewers[i].agentOutTraffic) { obj.viewers[i].agentOutTraffic += viewerPartOut; } else { obj.viewers[i].agentOutTraffic = viewerPartOut; } + } + } + //var event = { etype: 'relay', action: 'relaylog', domain: domain.id, nodeid: obj.nodeid, userid: peer.user._id, username: peer.user.name, msgid: 5, msg: "Left the desktop multiplex session", protocol: 2 }; const sessionSeconds = Math.floor((Date.now() - peer.startTime) / 1000); - var event = { etype: 'relay', action: 'relaylog', domain: domain.id, nodeid: obj.nodeid, userid: peer.user._id, username: peer.user.name, msgid: 122, msgArgs: [sessionSeconds], msg: "Left the desktop multiplex session after " + sessionSeconds + " second(s).", protocol: 2 }; + var event = { etype: 'relay', action: 'relaylog', domain: domain.id, nodeid: obj.nodeid, userid: peer.user._id, username: peer.user.name, msgid: 122, msgArgs: [sessionSeconds], msg: "Left the desktop multiplex session after " + sessionSeconds + " second(s).", protocol: 2, bytesin: inTraffc, bytesout: outTraffc }; + if (peer.guestName) { event.guestname = peer.guestName; } parent.parent.DispatchEvent(['*', obj.nodeid, peer.user._id, obj.meshid], obj, event); } diff --git a/meshuser.js b/meshuser.js index 538996cb..399ca85d 100644 --- a/meshuser.js +++ b/meshuser.js @@ -4694,8 +4694,8 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use expireTime = command.end * 1000; } - //var cookie = { a: 5, p: command.p, uid: user._id, gn: command.guestname, nid: node._id, cf: command.consent, pid: publicid }; - var cookie = { a: 6, pid: publicid }; + //var cookie = { a: 5, p: command.p, uid: user._id, gn: command.guestname, nid: node._id, cf: command.consent, pid: publicid }; // Old style sharing cookie + var cookie = { a: 6, pid: publicid }; // New style sharing cookie if ((startTime != null) && (expireTime != null)) { command.start = startTime; command.expire = cookie.e = expireTime; } const inviteCookie = parent.parent.encodeCookie(cookie, parent.parent.invitationLinkEncryptionKey); if (inviteCookie == null) { if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'createDeviceShareLink', responseid: command.responseid, result: 'Unable to generate shareing cookie' })); } catch (ex) { } } return; } @@ -5475,20 +5475,23 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use // Columns if (command.groupBy == 1) { data.groupFormat = 'user'; - data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ]; + data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'guestname', title: "guest", align: 'center' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ]; } else if (command.groupBy == 2) { data.groupFormat = 'node'; - data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'userid', title: "user", format: 'user' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ]; + data.columns = [{ id: 'time', title: "time", format: 'datetime' }, { id: 'userid', title: "user", format: 'user' }, { id: 'guestname', title: "guest", align: 'center' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ]; } else if (command.groupBy == 3) { - data.columns = [{ id: 'time', title: "time", format: 'time' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'userid', title: "user", format: 'user' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ]; + data.columns = [{ id: 'time', title: "time", format: 'time' }, { id: 'nodeid', title: "device", format: 'node' }, { id: 'guestname', title: "guest", align: 'center' }, { id: 'userid', title: "user", format: 'user' }, { id: 'protocol', title: "session", format: 'protocol', align: 'center' }, { id: 'length', title: "length", format: 'seconds', align: 'center', sumBy: 'protocol' } ]; } - // Add traffic colums + // Add traffic columns if (command.showTraffic) { data.columns.push({ id: 'bytesin', title: "bytesin", format: 'bytes', align: 'center', sumBy: 'protocol' }); data.columns.push({ id: 'bytesout', title: "bytesout", format: 'bytes', align: 'center', sumBy: 'protocol' }); } + // Remote guest column is not needed + if (!command.showGuestName) { data.columns.splice(2, 1); } + // Rows for (var i in docs) { // If MySQL or MariaDB query, we can't filter on MsgID, so we have to do it here. @@ -5506,7 +5509,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use if (command.showTraffic) { entry.bytesin = docs[i].bytesin; entry.bytesout = docs[i].bytesout; } // Add guest name if present - if (docs[i].nodeid.guestname != null) { entry.guestname = docs[i].nodeid.guestname; } + if (docs[i].guestname != null) { entry.guestname = docs[i].guestname; } // Session length if (((docs[i].msgid >= 10) && (docs[i].msgid <= 12)) && (docs[i].msgArgs != null) && (typeof docs[i].msgArgs == 'object') && (typeof docs[i].msgArgs[3] == 'number')) { entry.length = docs[i].msgArgs[3]; } diff --git a/views/default.handlebars b/views/default.handlebars index d5a55d7b..f0dbc8d8 100644 --- a/views/default.handlebars +++ b/views/default.handlebars @@ -14951,6 +14951,7 @@ x += '