Fixed bug on windows, where user consent for terminal wasn't working anymore

Bryan Roe committed Apr 15, 2020 at 19:05 UTC 1eb8bd2ae90d95d1cc75c954e39fba5927fc49be
1 file changed +61 -36
agents/meshcore.js
+61 -36
@@ -1135,6 +1135,9 @@ function createMeshCore(agent) {
1135 return;
1136 }
1137
1138 + var prom = require('promise');
1139 + this.httprequest.tpromise = new prom(function (res, rej) { this._res = res; this._rej = rej; });
1140 +
1141 this.end = function () {
1142 if (process.platform == 'win32') {
1143 // Unpipe the web socket
@@ -1184,12 +1187,14 @@ function createMeshCore(agent) {
1187 this.ws._term = c;
1188 c.pipe(this.ws, { dataTypeSkip: 1 });
1189 this.ws.pipe(c, { dataTypeSkip: 1 });
1190 + this.ws.httprequest.tpromise._res();
1191 });
1192 }
1193 else
1194 {
1195 // Legacy Terminal
1196 this.httprequest._term = require('win-terminal')[this.httprequest.protocol == 6 ? 'StartPowerShell' : 'Start'](cols, rows);
1197 + this.httprequest.tpromise._res(this.httprequest._term);
1198 }
1199 }
1200 else
@@ -1219,6 +1224,7 @@ function createMeshCore(agent) {
1224 this.ws._term = c;
1225 c.pipe(this.ws, { dataTypeSkip: 1 });
1226 this.ws.pipe(c, { dataTypeSkip: 1 });
1227 + this.ws.httprequest.tpromise._res();
1228 });
1229 }
1230 });
@@ -1235,6 +1241,7 @@ function createMeshCore(agent) {
1241 this.httprequest._term.pipe(this, { dataTypeSkip: 1 });
1242 this.pipe(this.httprequest._term, { dataTypeSkip: 1, end: false });
1243 this.prependListener('end', function () { this.httprequest._term.end(function () { console.log("Terminal was closed"); }); });
1244 + this.httprequest.tpromise._res();
1245 }
1246 }
1247 else
@@ -1272,6 +1279,7 @@ function createMeshCore(agent) {
1279 else
1280 {
1281 MeshServerLog("Failed to start remote terminal session, no shell found");
1282 + this.httprequest.tpromise._rej()
1283 return;
1284 }
1285 } catch (e)
@@ -1279,6 +1287,7 @@ function createMeshCore(agent) {
1287 MeshServerLog("Failed to start remote terminal session, " + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
1288 this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1289 this.end();
1290 + this.httprequest.tpromise._rej();
1291 return;
1292 }
1293
@@ -1288,45 +1297,60 @@ function createMeshCore(agent) {
1297 this.httprequest.process.stdout.pipe(this, { dataTypeSkip: 1 }); // 0 = Binary, 1 = Text.
1298 this.pipe(this.httprequest.process.stdin, { dataTypeSkip: 1, end: false }); // 0 = Binary, 1 = Text.
1299 this.prependListener('end', function () { this.httprequest.process.kill(); });
1300 + this.httprequest.tpromise._res();
1301 }
1302
1293 - // Perform notification if needed. Toast messages may not be supported on all platforms.
1294 - if (this.httprequest.consent && (this.httprequest.consent & 16)) {
1295 - // User Consent Prompt is required
1296 - // Send a console message back using the console channel, "\n" is supported.
1297 - this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: "Waiting for user to grant access..." }));
1298 - var pr = require('message-box').create('MeshCentral', this.httprequest.username + " requesting Terminal Access. Grant access?", 30);
1299 - pr.ws = this;
1300 - this.pause();
1303 + this.httprequest.tpromise.that = this;
1304 + this.httprequest.tpromise.then(function ()
1305 + {
1306 + var that = this.that;
1307
1302 - pr.then(
1303 - function () {
1304 - // Success
1305 - MeshServerLog("Starting remote terminal after local user accepted (" + this.ws.httprequest.remoteaddr + ")", this.ws.httprequest);
1306 - this.ws.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: null }));
1307 - if (this.ws.httprequest.consent && (this.ws.httprequest.consent & 2)) {
1308 - // User Notifications is required
1309 - try { require('toaster').Toast('MeshCentral', this.ws.httprequest.username + " started a remote terminal session."); } catch (ex) { }
1310 - }
1311 - this.ws.resume();
1312 - },
1313 - function (e) {
1314 - // User Consent Denied/Failed
1315 - MeshServerLog("Failed to start remote terminal after local user rejected (" + this.ws.httprequest.remoteaddr + ")", this.ws.httprequest);
1316 - this.ws.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1317 - this.ws.end();
1318 - });
1319 - } else {
1320 - // User Consent Prompt is not required
1321 - if (this.httprequest.consent && (this.httprequest.consent & 2)) {
1322 - // User Notifications is required
1323 - MeshServerLog('Started remote terminal with toast notification (' + this.httprequest.remoteaddr + ')', this.httprequest);
1324 - try { require('toaster').Toast('MeshCentral', this.httprequest.username + ' started a remote terminal session.'); } catch (ex) { }
1325 - } else {
1326 - MeshServerLog('Started remote terminal without notification (' + this.httprequest.remoteaddr + ')', this.httprequest);
1308 + // Perform notification if needed. Toast messages may not be supported on all platforms.
1309 + if (that.httprequest.consent && (that.httprequest.consent & 16))
1310 + {
1311 + // User Consent Prompt is required
1312 + // Send a console message back using the console channel, "\n" is supported.
1313 + that.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: "Waiting for user to grant access..." }));
1314 + var pr = require('message-box').create('MeshCentral', that.httprequest.username + " requesting Terminal Access. Grant access?", 30);
1315 + pr.ws = that;
1316 + that.pause();
1317 +
1318 + pr.then(
1319 + function ()
1320 + {
1321 + // Success
1322 + MeshServerLog("Starting remote terminal after local user accepted (" + this.ws.httprequest.remoteaddr + ")", this.ws.httprequest);
1323 + this.ws.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: null }));
1324 + if (this.ws.httprequest.consent && (this.ws.httprequest.consent & 2))
1325 + {
1326 + // User Notifications is required
1327 + try { require('toaster').Toast('MeshCentral', this.ws.httprequest.username + " started a remote terminal session."); } catch (ex) { }
1328 + }
1329 + this.ws.resume();
1330 + },
1331 + function (e)
1332 + {
1333 + // User Consent Denied/Failed
1334 + MeshServerLog("Failed to start remote terminal after local user rejected (" + this.ws.httprequest.remoteaddr + ")", this.ws.httprequest);
1335 + this.ws.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1336 + this.ws.end();
1337 + });
1338 }
1328 - this.resume();
1329 - }
1339 + else
1340 + {
1341 + // User Consent Prompt is not required
1342 + if (that.httprequest.consent && (that.httprequest.consent & 2))
1343 + {
1344 + // User Notifications is required
1345 + MeshServerLog('Started remote terminal with toast notification (' + that.httprequest.remoteaddr + ')', that.httprequest);
1346 + try { require('toaster').Toast('MeshCentral', that.httprequest.username + ' started a remote terminal session.'); } catch (ex) { }
1347 + } else
1348 + {
1349 + MeshServerLog('Started remote terminal without notification (' + that.httprequest.remoteaddr + ')', that.httprequest);
1350 + }
1351 + that.resume();
1352 + }
1353 + }, function () { });
1354
1355 this.removeAllListeners('data');
1356 this.on('data', onTunnelControlData);
@@ -1823,7 +1847,8 @@ function createMeshCore(agent) {
1847 if (ws.httprequest._dispatcher == null) return;
1848 //sendConsoleText('Win32-TermSize: ' + obj.cols + 'x' + obj.rows);
1849 if (ws.httprequest._dispatcher.invoke) { ws.httprequest._dispatcher.invoke('resizeTerminal', [obj.cols, obj.rows]); }
1826 - } else {
1850 + } else
1851 + {
1852 if (ws.httprequest.process == null || ws.httprequest.process.pty == 0) return;
1853 //sendConsoleText('Linux Resize: ' + obj.cols + 'x' + obj.rows);
1854