From 622f05451de8636112ae2c003c5d1f4da2cdfdff Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Tue, 13 Aug 2019 22:48:06 -0700 Subject: [PATCH] Updated Win32 terminal, so an error is propagated back if terminal fails to start --- agents/meshcore.js | 50 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/agents/meshcore.js b/agents/meshcore.js index 27cbbcf5..adc268af 100644 --- a/agents/meshcore.js +++ b/agents/meshcore.js @@ -1059,41 +1059,61 @@ function createMeshCore(agent) return; } - this.end = function () { - if (process.platform == "win32") { + this.end = function () + { + if (process.platform == "win32") + { // Unpipe the web socket this.unpipe(this.httprequest._term); - this.httprequest._term.unpipe(this); + if (this.httprequest._term) { this.httprequest._term.unpipe(this); } // Unpipe the WebRTC channel if needed (This will also be done when the WebRTC channel ends). - if (this.rtcchannel) { + if (this.rtcchannel) + { this.rtcchannel.unpipe(this.httprequest._term); - this.httprequest._term.unpipe(this.rtcchannel); + if (this.httprequest._term) { this.httprequest._term.unpipe(this.rtcchannel); } } // Clean up - this.httprequest._term.end(); + if (this.httprequest._term) { this.httprequest._term.end(); } this.httprequest._term = null; - } else { + } else + { // TODO!! } }; // Remote terminal using native pipes - if (process.platform == "win32") { - if ((this.httprequest.protocol == 6) && (require('win-terminal').PowerShellCapable() == true)) { - this.httprequest._term = require('win-terminal').StartPowerShell(80, 25); - } else { - this.httprequest._term = require('win-terminal').Start(80, 25); + if (process.platform == "win32") + { + try + { + if ((this.httprequest.protocol == 6) && (require('win-terminal').PowerShellCapable() == true)) + { + this.httprequest._term = require('win-terminal').StartPowerShell(80, 25); + } else + { + this.httprequest._term = require('win-terminal').Start(80, 25); + } + } + catch(e) + { + MeshServerLog('Failed to start remote terminal session, ' + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest); + this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() })); + this.end(); + return; } this.httprequest._term.pipe(this, { dataTypeSkip: 1 }); this.pipe(this.httprequest._term, { dataTypeSkip: 1, end: false }); this.prependListener('end', function () { this.httprequest._term.end(function () { console.log('Terminal was closed'); }); }); - } else { - if (fs.existsSync("/bin/bash")) { + } else + { + if (fs.existsSync("/bin/bash")) + { this.httprequest.process = childProcess.execFile("/bin/bash", ["bash", "-i"], { type: childProcess.SpawnTypes.TERM }); if (process.platform == 'linux') { this.httprequest.process.stdin.write("alias ls='ls --color=auto'\nclear\n"); } - } else { + } else + { this.httprequest.process = childProcess.execFile("/bin/sh", ["sh"], { type: childProcess.SpawnTypes.TERM }); if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n"); } }