Improved events and reports.

This commit is contained in:
Ylian Saint-Hilaire 2021-09-12 13:03:03 -07:00
parent 902e71a96f
commit 663d1fb722
3 changed files with 55 additions and 13 deletions

View File

@ -186,11 +186,12 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) {
if (obj.viewers == null) return; if (obj.viewers == null) return;
if (peer == obj.agent) { if (peer == obj.agent) {
//console.log('removePeer-agent', obj.nodeid); //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 // Clean up the agent
obj.agent = null; obj.agent = null;
// Agent has disconnected, disconnect everyone.
if (obj.viewers != null) { for (var i in obj.viewers) { obj.viewers[i].close(); } }
dispose(); dispose();
return true; return true;
} else { } else {
@ -211,9 +212,46 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) {
// Log leaving the multiplex session // 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. 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 }; //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); 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); parent.parent.DispatchEvent(['*', obj.nodeid, peer.user._id, obj.meshid], obj, event);
} }

View File

@ -4694,8 +4694,8 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
expireTime = command.end * 1000; 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: 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 }; var cookie = { a: 6, pid: publicid }; // New style sharing cookie
if ((startTime != null) && (expireTime != null)) { command.start = startTime; command.expire = cookie.e = expireTime; } if ((startTime != null) && (expireTime != null)) { command.start = startTime; command.expire = cookie.e = expireTime; }
const inviteCookie = parent.parent.encodeCookie(cookie, parent.parent.invitationLinkEncryptionKey); 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; } 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 // Columns
if (command.groupBy == 1) { if (command.groupBy == 1) {
data.groupFormat = 'user'; 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) { } else if (command.groupBy == 2) {
data.groupFormat = 'node'; 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) { } 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) { if (command.showTraffic) {
data.columns.push({ id: 'bytesin', title: "bytesin", format: 'bytes', align: 'center', sumBy: 'protocol' }); 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' }); 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 // Rows
for (var i in docs) { for (var i in docs) {
// If MySQL or MariaDB query, we can't filter on MsgID, so we have to do it here. // 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; } if (command.showTraffic) { entry.bytesin = docs[i].bytesin; entry.bytesout = docs[i].bytesout; }
// Add guest name if present // 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 // 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]; } 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]; }

View File

@ -14951,6 +14951,7 @@
x += '<div id=d2showTrafficDiv style=display:none>'; x += '<div id=d2showTrafficDiv style=display:none>';
x += addHtmlValue("", '<div style=width:250px><label><input type=checkbox id=d2showTraffic ' + ((settings.showTraffic) ? 'checked' : '') + '>' + "Show Traffic" + '</label><div>'); x += addHtmlValue("", '<div style=width:250px><label><input type=checkbox id=d2showTraffic ' + ((settings.showTraffic) ? 'checked' : '') + '>' + "Show Traffic" + '</label><div>');
x += addHtmlValue("", '<div style=width:250px><label><input type=checkbox id=d2showGuestName ' + ((settings.showGuestName) ? 'checked' : '') + '>' + "Show Guest Name" + '</label><div>');
x += '</div>'; x += '</div>';
setDialogMode(2, "Generate Report", 3, generateReportDialogEx, x); setDialogMode(2, "Generate Report", 3, generateReportDialogEx, x);
@ -14981,12 +14982,12 @@
try { tz = Intl.DateTimeFormat().resolvedOptions().timeZone; } catch (ex) {} try { tz = Intl.DateTimeFormat().resolvedOptions().timeZone; } catch (ex) {}
var devGroup = decodeURIComponent(Q('d2groupId').value); var devGroup = decodeURIComponent(Q('d2groupId').value);
if (devGroup == 0) { devGroup = null; } if (devGroup == 0) { devGroup = null; }
putstore('_ReportSettings', JSON.stringify({ type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), timeRange: parseInt(Q('d2timeRange').value), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked })); putstore('_ReportSettings', JSON.stringify({ type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), timeRange: parseInt(Q('d2timeRange').value), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked, showGuestName: Q('d2showGuestName').checked }));
meshserver.send({ action: 'report', type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), start: start, end: end, tz: tz, tf: new Date().getTimezoneOffset(), l: getLang(), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked }); meshserver.send({ action: 'report', type: parseInt(Q('d2reportType').value), groupBy: parseInt(Q('d2groupBy').value), start: start, end: end, tz: tz, tf: new Date().getTimezoneOffset(), l: getLang(), devGroup: devGroup, showTraffic: Q('d2showTraffic').checked, showGuestName: Q('d2showGuestName').checked });
} }
function renderReport(r) { function renderReport(r) {
var colTranslation = { time: "Time", device: "Device", session: "Session", user: "User", length: "Length", bytesin: "Bytes In", bytesout: "Bytes Out" } var colTranslation = { time: "Time", device: "Device", session: "Session", user: "User", guest: "Guest", length: "Length", bytesin: "Bytes In", bytesout: "Bytes Out" }
var x = '<table style=width:100%>', firstItem; var x = '<table style=width:100%>', firstItem;
var sumByCol = null; // Indicate by what colum we sum by var sumByCol = null; // Indicate by what colum we sum by
var sumByValues = []; // Indicate by what values we sum by var sumByValues = []; // Indicate by what values we sum by