Updated Win32 terminal, so an error is propagated back if terminal fails to start
Bryan Roe committed
Aug 13, 2019 at 22:48 UTC
18e58ddf4b5e7002af9288184ade82a9cc7304b9
1 file changed
+35
-15
agents/meshcore.js
+35
-15
@@ -1059,41 +1059,61 @@ function createMeshCore(agent)
1059
return;
1060
}
1061
1062
- this.end = function () {
1063
- if (process.platform == "win32") {
1062
+ this.end = function ()
1063
+ {
1064
+ if (process.platform == "win32")
1065
+ {
1066
// Unpipe the web socket
1067
this.unpipe(this.httprequest._term);
1066
- this.httprequest._term.unpipe(this);
1068
+ if (this.httprequest._term) { this.httprequest._term.unpipe(this); }
1069
1070
// Unpipe the WebRTC channel if needed (This will also be done when the WebRTC channel ends).
1069
- if (this.rtcchannel) {
1071
+ if (this.rtcchannel)
1072
+ {
1073
this.rtcchannel.unpipe(this.httprequest._term);
1071
- this.httprequest._term.unpipe(this.rtcchannel);
1074
+ if (this.httprequest._term) { this.httprequest._term.unpipe(this.rtcchannel); }
1075
}
1076
1077
// Clean up
1075
- this.httprequest._term.end();
1078
+ if (this.httprequest._term) { this.httprequest._term.end(); }
1079
this.httprequest._term = null;
1077
- } else {
1080
+ } else
1081
+ {
1082
// TODO!!
1083
}
1084
};
1085
1086
// Remote terminal using native pipes
1083
- if (process.platform == "win32") {
1084
- if ((this.httprequest.protocol == 6) && (require('win-terminal').PowerShellCapable() == true)) {
1085
- this.httprequest._term = require('win-terminal').StartPowerShell(80, 25);
1086
- } else {
1087
- this.httprequest._term = require('win-terminal').Start(80, 25);
1087
+ if (process.platform == "win32")
1088
+ {
1089
+ try
1090
+ {
1091
+ if ((this.httprequest.protocol == 6) && (require('win-terminal').PowerShellCapable() == true))
1092
+ {
1093
+ this.httprequest._term = require('win-terminal').StartPowerShell(80, 25);
1094
+ } else
1095
+ {
1096
+ this.httprequest._term = require('win-terminal').Start(80, 25);
1097
+ }
1098
+ }
1099
+ catch(e)
1100
+ {
1101
+ MeshServerLog('Failed to start remote terminal session, ' + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
1102
+ this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1103
+ this.end();
1104
+ return;
1105
}
1106
this.httprequest._term.pipe(this, { dataTypeSkip: 1 });
1107
this.pipe(this.httprequest._term, { dataTypeSkip: 1, end: false });
1108
this.prependListener('end', function () { this.httprequest._term.end(function () { console.log('Terminal was closed'); }); });
1092
- } else {
1093
- if (fs.existsSync("/bin/bash")) {
1109
+ } else
1110
+ {
1111
+ if (fs.existsSync("/bin/bash"))
1112
+ {
1113
this.httprequest.process = childProcess.execFile("/bin/bash", ["bash", "-i"], { type: childProcess.SpawnTypes.TERM });
1114
if (process.platform == 'linux') { this.httprequest.process.stdin.write("alias ls='ls --color=auto'\nclear\n"); }
1096
- } else {
1115
+ } else
1116
+ {
1117
this.httprequest.process = childProcess.execFile("/bin/sh", ["sh"], { type: childProcess.SpawnTypes.TERM });
1118
if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n"); }
1119
}