Add dynamic resizing of xterm

TotallyNotElite committed Jan 24, 2020 at 23:01 UTC 5f0c0b38da9bb2b8c7d571bb75b40906bf992141
2 files changed +20 -7
agents/meshcore.js
+19 -7
@@ -39,8 +39,7 @@ var MESHRIGHT_LIMITEDINPUT = 4096;
39
40 function createMeshCore(agent) {
41 var obj = {};
42 - if (process.platform == 'win32' && require('user-sessions').isRoot())
43 - {
42 + if (process.platform == 'win32' && require('user-sessions').isRoot()) {
43 // Check the Agent Uninstall MetaData for correctness, as the installer may have written an incorrect value
44 try {
45 var writtenSize = 0, actualSize = Math.floor(require('fs').statSync(process.execPath).size / 1024);
@@ -1220,21 +1219,28 @@ function createMeshCore(agent) {
1219 var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: env };
1220 var setupcommands = ' alias ls=\'ls --color=auto\'\n';
1221 if (shell == sh) setupcommands += ' stty erase ^H\n';
1223 - setupcommands += ' clear\n';
1222
1223 if (script && shell && process.platform == 'linux') {
1224 this.httprequest.process = childProcess.execFile(script, ['script', '--return', '--quiet', '-c', '"' + shell + '"', '/dev/null'], options); // Start as active user
1225 + // Is child process the shell? Needed for resizing.
1226 + this.httprequest.process.isChildShell = true;
1227 this.httprequest.process.stdin.write(setupcommands);
1228 } else if (python && shell) {
1229 this.httprequest.process = childProcess.execFile(python, ['python', '-c', 'import pty; pty.spawn(["' + shell + '"])'], options); // Start as active user
1230 + // Is child process the shell? Needed for resizing.
1231 + this.httprequest.process.isChildShell = true;
1232 if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
1233 } else if (bash) {
1234 options.type = childProcess.SpawnTypes.TERM;
1235 this.httprequest.process = childProcess.execFile(bash, ['bash', '-i'], options); // Start as active user
1236 + // Is child process the shell? Needed for resizing.
1237 + this.httprequest.process.isChildShell = false;
1238 if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
1239 } else if (sh) {
1240 options.type = childProcess.SpawnTypes.TERM;
1241 this.httprequest.process = childProcess.execFile(sh, ['sh'], options); // Start as active user
1242 + // Is child process the shell? Needed for resizing.
1243 + this.httprequest.process.isChildShell = false;
1244 if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands + "PS1='$ '\n"); }
1245 } else {
1246 MeshServerLog("Failed to start remote terminal session, no shell found");
@@ -1787,8 +1793,15 @@ function createMeshCore(agent) {
1793 // TODO
1794 } else {
1795 if (ws.httprequest.process == null) return;
1790 - //sendConsoleText('Linux-TermSize: ' + obj.cols + 'x' + obj.rows);
1791 - // TODO
1796 + // ILibDuktape_ChildProcess kill doesn't support sending signals
1797 + if (fs.existsSync("/bin/kill"))
1798 + {
1799 + if (ws.httprequest.process.isChildShell)
1800 + // We need to send signal to the child of the process, since the child is the shell
1801 + childProcess.execFile('/bin/bash', ['bash', "-c", "kill -SIGWINCH $(pgrep -P " + ws.httprequest.process.pid + ")"]);
1802 + else
1803 + childProcess.execFile('/bin/bash', ['bash', "-c", "kill -SIGWINCH " + ws.httprequest.process.pid]);
1804 + }
1805 }
1806 }
1807 break;
@@ -2222,8 +2235,7 @@ function createMeshCore(agent) {
2235 break;
2236 }
2237 case 'ps': {
2225 - processManager.getProcesses(function (plist)
2226 - {
2238 + processManager.getProcesses(function (plist) {
2239 var x = '';
2240 for (var i in plist) { x += i + ((plist[i].user) ? (', ' + plist[i].user) : '') + ', ' + plist[i].cmd + '\r\n'; }
2241 sendConsoleText(x, sessionid);
views/xterm.handlebars
+1
@@ -171,6 +171,7 @@
171 break;
172 case 3:
173 // Connected
174 + tunnel.sendText(`mcresize() { old=$(stty -g);stty raw -echo min 0 time 5;printf '\\0337\\033[r\\033[999;999H\\033[6n\\0338' > /dev/tty;IFS='[;R' read -r _ rows cols _ < /dev/tty;stty "$old";stty cols "$cols" rows "$rows"; };trap mcresize SIGWINCH;clear\n`);
175 term.focus();
176 break;
177 default: