Updated AutoLock

Bryan Roe committed Sep 3, 2021 at 00:55 UTC 7e5c608b600f5a2a5daddaad295da7605bb88335
1 file changed +99 -18
agents/meshcore.js
+99 -18
@@ -87,6 +87,72 @@ if (require('MeshAgent').ARCHID == null) {
87 }
88 if (id != null) { Object.defineProperty(require('MeshAgent'), 'ARCHID', { value: id }); }
89 }
90 +function lockDesktop(uid)
91 +{
92 + switch (process.platform)
93 + {
94 + case 'linux':
95 + if (uid != null)
96 + {
97 + var name = require('user-sessions').getUsername(uid);
98 + var child = require('child_process').execFile('/bin/sh', ['sh']);
99 + child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
100 + child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
101 + child.stdin.write('loginctl show-user -p Sessions ' + name + " | awk '{");
102 + child.stdin.write('gsub(/^Sessions=/,"",$0);');
103 + child.stdin.write('cmd = sprintf("loginctl lock-session %s",$0);');
104 + child.stdin.write('system(cmd);');
105 + child.stdin.write("}'\nexit\n");
106 + child.waitExit();
107 + }
108 + else
109 + {
110 + var child = require('child_process').execFile('/bin/sh', ['sh']);
111 + child.stdout.str = ''; child.stdout.on('data', function (chunk) { this.str += chunk.toString(); });
112 + child.stderr.str = ''; child.stderr.on('data', function (chunk) { this.str += chunk.toString(); });
113 + child.stdin.write('loginctl lock-sessions\nexit\n');
114 + child.waitExit();
115 + }
116 + break;
117 + case 'win32':
118 + {
119 + var options = { type: 1, uid: uid };
120 + var child = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe', ['/c', 'RunDll32.exe user32.dll,LockWorkStation'], options);
121 + child.waitExit();
122 + }
123 + break;
124 + default:
125 + break;
126 + }
127 +}
128 +var writable = require('stream').Writable;
129 +function destopLockHelper_pipe(httprequest)
130 +{
131 + if (process.platform != 'linux' && process.platform != 'freebsd') { return; }
132 +
133 + if (httprequest.unlockerHelper == null && httprequest.desktop != null && httprequest.desktop.kvm != null)
134 + {
135 + httprequest.unlockerHelper = new writable(
136 + {
137 + 'write': function (chunk, flush)
138 + {
139 + if (chunk.readUInt16BE(0) == 65)
140 + {
141 + delete this.request.autolock;
142 + }
143 + flush();
144 + return (true);
145 + },
146 + 'final': function (flush)
147 + {
148 + flush();
149 + }
150 + });
151 + httprequest.unlockerHelper.request = httprequest;
152 + httprequest.desktop.kvm.pipe(httprequest.unlockerHelper);
153 + }
154 +}
155 +
156 var obj = { serverInfo: {} };
157 var agentFileHttpRequests = {}; // Currently active agent HTTPS GET requests from the server.
158 var agentFileHttpPendingRequests = []; // Pending HTTPS GET requests from the server.
@@ -1709,13 +1775,8 @@ function onTunnelClosed() {
1775 if ((this.httprequest.xoptions != null) && (typeof this.httprequest.xoptions.tsid == 'number')) { tsid = this.httprequest.xoptions.tsid; }
1776
1777 // Lock the current user out of the desktop
1712 - try {
1713 - if (process.platform == 'win32') {
1714 - MeshServerLogEx(53, null, "Locking remote user out of desktop", this.httprequest);
1715 - var child = require('child_process');
1716 - child.execFile(process.env['windir'] + '\\system32\\cmd.exe', ['/c', 'RunDll32.exe user32.dll,LockWorkStation'], { type: 1, uid: tsid });
1717 - }
1718 - } catch (ex) { }
1778 + MeshServerLogEx(53, null, "Locking remote user out of desktop", this.httprequest);
1779 + lockDesktop(tsid);
1780 }
1781
1782 // If this is a routing session, clean up and send the new session counts.
@@ -2283,6 +2344,10 @@ function onTunnelData(data) {
2344 }
2345 }
2346 this.ws.httprequest.desktop.kvm.pipe(this.ws, { dataTypeSkip: 1 });
2347 + if (this.ws.httprequest.autolock)
2348 + {
2349 + destopLockHelper_pipe(this.ws.httprequest);
2350 + }
2351 this.ws.resume();
2352 },
2353 function (e) {
@@ -2330,6 +2395,10 @@ function onTunnelData(data) {
2395 }
2396 }
2397 this.httprequest.desktop.kvm.pipe(this, { dataTypeSkip: 1 });
2398 + if (this.httprequest.autolock)
2399 + {
2400 + destopLockHelper_pipe(this.httprequest);
2401 + }
2402 }
2403
2404 this.removeAllListeners('data');
@@ -2741,18 +2810,24 @@ function onTunnelControlData(data, ws) {
2810 if ((ws.httprequest.xoptions != null) && (typeof ws.httprequest.xoptions.tsid == 'number')) { tsid = ws.httprequest.xoptions.tsid; }
2811
2812 // Lock the current user out of the desktop
2744 - try {
2745 - if (process.platform == 'win32') {
2746 - MeshServerLogEx(53, null, "Locking remote user out of desktop", ws.httprequest);
2747 - var child = require('child_process');
2748 - child.execFile(process.env['windir'] + '\\system32\\cmd.exe', ['/c', 'RunDll32.exe user32.dll,LockWorkStation'], { type: 1, uid: tsid });
2749 - }
2750 - } catch (e) { }
2813 + MeshServerLogEx(53, null, "Locking remote user out of desktop", ws.httprequest);
2814 + lockDesktop(tsid);
2815 break;
2816 }
2817 case 'autolock': {
2818 // Set the session to auto lock on disconnect
2755 - if (obj.value === true) { ws.httprequest.autolock = true; } else { delete ws.httprequest.autolock; }
2819 + if (obj.value === true)
2820 + {
2821 + ws.httprequest.autolock = true;
2822 + if (ws.httprequest.unlockerHelper == null)
2823 + {
2824 + destopLockHelper_pipe(ws.httprequest);
2825 + }
2826 + }
2827 + else
2828 + {
2829 + delete ws.httprequest.autolock;
2830 + }
2831 break;
2832 }
2833 case 'options': {
@@ -2765,7 +2840,14 @@ function onTunnelControlData(data, ws) {
2840 if ((obj != null) && (typeof obj.consent == 'number')) { ws.httprequest.consent |= obj.consent; }
2841
2842 // Set autolock
2768 - if ((obj != null) && (obj.autolock === true)) { ws.httprequest.autolock = true; }
2843 + if ((obj != null) && (obj.autolock === true))
2844 + {
2845 + ws.httprequest.autolock = true;
2846 + if (ws.httprequest.unlockerHelper == null)
2847 + {
2848 + destopLockHelper_pipe(ws.httprequest);
2849 + }
2850 + }
2851
2852 break;
2853 }
@@ -3918,8 +4000,7 @@ function processConsoleCommand(cmd, args, rights, sessionid) {
4000 break;
4001 }
4002 case 'lock': { // Lock the current user out of the desktop
3921 - if (process.platform == 'win32') { var child = require('child_process'); child.execFile(process.env['windir'] + '\\system32\\cmd.exe', ['/c', 'RunDll32.exe user32.dll,LockWorkStation'], { type: 1 }); response = 'Ok'; }
3922 - else { response = 'Not supported on the platform'; }
4003 + lockDesktop();
4004 break;
4005 }
4006 case 'amt': { // Show Intel AMT status