More work on pre-user session recording. #3064

Ylian Saint-Hilaire committed Aug 26, 2021 at 13:54 UTC 31e8c12bca36686a3fb9ceb2b0bf1253b5009a20
2 files changed +61 -54
meshdesktopmultiplex.js
+60 -53
@@ -129,22 +129,27 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) {
129 if ((typeof sr == 'number') && (sr > 0) && (sr < 1000)) { peer.slowRelay = sr; }
130 }
131
132 - // Indicated we are connected
133 - obj.sendToViewer(peer, obj.recordingFile ? 'cr' : 'c');
134 -
135 - // If the agent sent display information or console message, send it to the viewer
136 - if (obj.lastDisplayInfoData != null) { obj.sendToViewer(peer, obj.lastDisplayInfoData); }
137 - if (obj.lastDisplayLocationData != null) { obj.sendToViewer(peer, obj.lastDisplayLocationData); }
138 - if (obj.lastConsoleMessage != null) { obj.sendToViewer(peer, obj.lastConsoleMessage); }
139 -
140 - // Log joining the multiplex session
141 - if (obj.startTime != null) {
142 - var event = { etype: 'relay', action: 'relaylog', domain: domain.id, nodeid: obj.nodeid, userid: peer.user._id, username: peer.user.name, msgid: 4, msg: "Joined desktop multiplex session", protocol: 2 };
143 - parent.parent.DispatchEvent(['*', obj.nodeid, peer.user._id, obj.meshid], obj, event);
144 - }
132 + // Check session recording
133 + var startRecord = false;
134 + if ((domain.sessionrecording.onlyselectedusers === true) && (peer.user != null) && (peer.user.flags != null) && ((peer.user.flags & 2) != 0)) { startRecord = true; }
135 + startRecording(domain, startRecord, function () {
136 + // Indicated we are connected
137 + obj.sendToViewer(peer, obj.recordingFile ? 'cr' : 'c');
138 +
139 + // If the agent sent display information or console message, send it to the viewer
140 + if (obj.lastDisplayInfoData != null) { obj.sendToViewer(peer, obj.lastDisplayInfoData); }
141 + if (obj.lastDisplayLocationData != null) { obj.sendToViewer(peer, obj.lastDisplayLocationData); }
142 + if (obj.lastConsoleMessage != null) { obj.sendToViewer(peer, obj.lastConsoleMessage); }
143 +
144 + // Log joining the multiplex session
145 + if (obj.startTime != null) {
146 + var event = { etype: 'relay', action: 'relaylog', domain: domain.id, nodeid: obj.nodeid, userid: peer.user._id, username: peer.user.name, msgid: 4, msg: "Joined desktop multiplex session", protocol: 2 };
147 + parent.parent.DispatchEvent(['*', obj.nodeid, peer.user._id, obj.meshid], obj, event);
148 + }
149
146 - // Send an updated list of all peers to all viewers
147 - obj.sendSessionMetadata();
150 + // Send an updated list of all peers to all viewers
151 + obj.sendSessionMetadata();
152 + });
153 } else {
154 //console.log('addPeer-agent', obj.nodeid);
155 if (obj.agent != null) { parent.parent.debug('relay', 'DesktopRelay: Error, duplicate agent connection'); return false; }
@@ -253,7 +258,7 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) {
258
259 // Add a event entry about this recording
260 var basefile = parent.parent.path.basename(filename);
256 - var event = { etype: 'relay', action: 'recording', domain: domain.id, nodeid: obj.nodeid, msgid: 7, mshArgs: [obj.sessionLength], msg: "Finished recording session" + (obj.sessionLength ? (', ' + obj.sessionLength + ' second(s)') : ''), filename: basefile, size: obj.recordingFileSize, protocol: 2, icon: obj.icon, name: obj.name, meshid: obj.meshid, userids: obj.userIds, multiplex: true };
261 + var event = { etype: 'relay', action: 'recording', domain: domain.id, nodeid: obj.nodeid, msgid: 7, msgArgs: [obj.sessionLength], msg: "Finished recording session" + (obj.sessionLength ? (', ' + obj.sessionLength + ' second(s)') : ''), filename: basefile, size: obj.recordingFileSize, protocol: 2, icon: obj.icon, name: obj.name, meshid: obj.meshid, userids: obj.userIds, multiplex: true };
262 var mesh = parent.meshes[obj.meshid];
263 if (mesh != null) { event.meshname = mesh.name; }
264 if (obj.sessionStart) { event.startTime = obj.sessionStart; event.lengthTime = obj.sessionLength; }
@@ -726,57 +731,59 @@ function CreateDesktopMultiplexor(parent, domain, nodeid, func) {
731 }
732 }
733
734 + function startRecording(domain, start, func) {
735 + if ((obj.pendingRecording == 1) || (obj.recordingFile != null)) { func(true); return; } // Check if already recording
736 + if (start == false) { func(false); return; } // Just skip this
737 + obj.pendingRecording = 1;
738 + var now = new Date(Date.now());
739 + var recFilename = 'desktopSession' + ((domain.id == '') ? '' : '-') + domain.id + '-' + now.getUTCFullYear() + '-' + parent.common.zeroPad(now.getUTCMonth(), 2) + '-' + parent.common.zeroPad(now.getUTCDate(), 2) + '-' + parent.common.zeroPad(now.getUTCHours(), 2) + '-' + parent.common.zeroPad(now.getUTCMinutes(), 2) + '-' + parent.common.zeroPad(now.getUTCSeconds(), 2) + '-' + obj.nodeid.split('/')[2] + '.mcrec'
740 + var recFullFilename = null;
741 + if (domain.sessionrecording.filepath) {
742 + try { parent.parent.fs.mkdirSync(domain.sessionrecording.filepath); } catch (e) { }
743 + recFullFilename = parent.parent.path.join(domain.sessionrecording.filepath, recFilename);
744 + } else {
745 + try { parent.parent.fs.mkdirSync(parent.parent.recordpath); } catch (e) { }
746 + recFullFilename = parent.parent.path.join(parent.parent.recordpath, recFilename);
747 + }
748 + parent.parent.fs.open(recFullFilename, 'w', function (err, fd) {
749 + delete obj.pendingRecording;
750 + if (err != null) {
751 + parent.parent.debug('relay', 'Relay: Unable to record to file: ' + recFullFilename);
752 + func(false);
753 + return;
754 + }
755 + // Write the recording file header
756 + parent.parent.debug('relay', 'Relay: Started recoding to file: ' + recFullFilename);
757 + var metadata = { magic: 'MeshCentralRelaySession', ver: 1, nodeid: obj.nodeid, meshid: obj.meshid, time: new Date().toLocaleString(), protocol: 2, devicename: obj.name, devicegroup: obj.meshname };
758 + var firstBlock = JSON.stringify(metadata);
759 + recordingEntry(fd, 1, 0, firstBlock, function () {
760 + obj.recordingFile = { fd: fd, filename: recFullFilename };
761 + obj.recordingFileWriting = false;
762 + func(true);
763 + });
764 + });
765 + }
766 +
767 function recordingSetup(domain, func) {
768 + var record = false;
769 +
770 // Setup session recording
771 if ((domain.sessionrecording == true || ((typeof domain.sessionrecording == 'object') && ((domain.sessionrecording.protocols == null) || (domain.sessionrecording.protocols.indexOf(2) >= 0))))) {
772 + record = true;
773
774 // Check again to make sure we need to start recording
775 if ((domain.sessionrecording.onlyselecteddevicegroups === true) || (domain.sessionrecording.onlyselectedusers === true)) {
735 - var record = false;
736 -
737 - // Check user recording
738 - if (domain.sessionrecording.onlyselectedusers === true) {
739 - // TODO: Check recording ???
740 - }
776 + record = false;
777
778 // Check device group recording
779 if (domain.sessionrecording.onlyselecteddevicegroups === true) {
780 var mesh = parent.meshes[obj.meshid];
781 if ((mesh.flags != null) && ((mesh.flags & 4) != 0)) { record = true; }
782 }
747 -
748 - if (record == false) { func(false); return; } // Do not record the session
783 }
750 -
751 - var now = new Date(Date.now());
752 - var recFilename = 'desktopSession' + ((domain.id == '') ? '' : '-') + domain.id + '-' + now.getUTCFullYear() + '-' + parent.common.zeroPad(now.getUTCMonth(), 2) + '-' + parent.common.zeroPad(now.getUTCDate(), 2) + '-' + parent.common.zeroPad(now.getUTCHours(), 2) + '-' + parent.common.zeroPad(now.getUTCMinutes(), 2) + '-' + parent.common.zeroPad(now.getUTCSeconds(), 2) + '-' + obj.nodeid.split('/')[2] + '.mcrec'
753 - var recFullFilename = null;
754 - if (domain.sessionrecording.filepath) {
755 - try { parent.parent.fs.mkdirSync(domain.sessionrecording.filepath); } catch (e) { }
756 - recFullFilename = parent.parent.path.join(domain.sessionrecording.filepath, recFilename);
757 - } else {
758 - try { parent.parent.fs.mkdirSync(parent.parent.recordpath); } catch (e) { }
759 - recFullFilename = parent.parent.path.join(parent.parent.recordpath, recFilename);
760 - }
761 - parent.parent.fs.open(recFullFilename, 'w', function (err, fd) {
762 - if (err != null) {
763 - parent.parent.debug('relay', 'Relay: Unable to record to file: ' + recFullFilename);
764 - func(false);
765 - return;
766 - }
767 - // Write the recording file header
768 - parent.parent.debug('relay', 'Relay: Started recoding to file: ' + recFullFilename);
769 - var metadata = { magic: 'MeshCentralRelaySession', ver: 1, nodeid: obj.nodeid, meshid: obj.meshid, time: new Date().toLocaleString(), protocol: 2, devicename: obj.name, devicegroup: obj.meshname };
770 - var firstBlock = JSON.stringify(metadata);
771 - recordingEntry(fd, 1, 0, firstBlock, function () {
772 - obj.recordingFile = { fd: fd, filename: recFullFilename };
773 - obj.recordingFileWriting = false;
774 - func(true);
775 - });
776 - });
777 - } else {
778 - func(false);
784 }
785 +
786 + startRecording(domain, record, func);
787 }
788
789 // Record data to the recording file
views/default.handlebars
+1 -1
@@ -9944,7 +9944,7 @@
9944 if (mode == 3) { eventList = currentUserEvents; }
9945 for (var i in eventList) { if (eventList[i].h == h) { xevent = eventList[i]; break; } }
9946 if (xevent) {
9947 - var x = '<div style=overflow-y:auto>';
9947 + var x = '<div style=overflow-y:auto;max-height:300px>';
9948 for (var i in xevent) {
9949 if ((i == 'h') || (i == '_id') || (i == 'ids') || (i == 'domain') || (xevent[i] == null) || (typeof xevent[i] == 'object')) continue;
9950 x += addHtmlValue3(EscapeHtml(i), EscapeHtml(xevent[i]));