From 413ed5c893c7830dc0d42008795aac017719bcbd Mon Sep 17 00:00:00 2001 From: TotallyNotElite <1yourexperiment@protonmail.com> Date: Fri, 24 Jan 2020 23:01:58 +0100 Subject: [PATCH] Add dynamic resizing of xterm --- agents/meshcore.js | 26 +++++++++++++++++++------- views/xterm.handlebars | 1 + 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/agents/meshcore.js b/agents/meshcore.js index 5f5c60c8..f9af4f4e 100644 --- a/agents/meshcore.js +++ b/agents/meshcore.js @@ -39,8 +39,7 @@ var MESHRIGHT_LIMITEDINPUT = 4096; function createMeshCore(agent) { var obj = {}; - if (process.platform == 'win32' && require('user-sessions').isRoot()) - { + if (process.platform == 'win32' && require('user-sessions').isRoot()) { // Check the Agent Uninstall MetaData for correctness, as the installer may have written an incorrect value try { var writtenSize = 0, actualSize = Math.floor(require('fs').statSync(process.execPath).size / 1024); @@ -1220,21 +1219,28 @@ function createMeshCore(agent) { var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: env }; var setupcommands = ' alias ls=\'ls --color=auto\'\n'; if (shell == sh) setupcommands += ' stty erase ^H\n'; - setupcommands += ' clear\n'; if (script && shell && process.platform == 'linux') { this.httprequest.process = childProcess.execFile(script, ['script', '--return', '--quiet', '-c', '"' + shell + '"', '/dev/null'], options); // Start as active user + // Is child process the shell? Needed for resizing. + this.httprequest.process.isChildShell = true; this.httprequest.process.stdin.write(setupcommands); } else if (python && shell) { this.httprequest.process = childProcess.execFile(python, ['python', '-c', 'import pty; pty.spawn(["' + shell + '"])'], options); // Start as active user + // Is child process the shell? Needed for resizing. + this.httprequest.process.isChildShell = true; if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); } } else if (bash) { options.type = childProcess.SpawnTypes.TERM; this.httprequest.process = childProcess.execFile(bash, ['bash', '-i'], options); // Start as active user + // Is child process the shell? Needed for resizing. + this.httprequest.process.isChildShell = false; if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); } } else if (sh) { options.type = childProcess.SpawnTypes.TERM; this.httprequest.process = childProcess.execFile(sh, ['sh'], options); // Start as active user + // Is child process the shell? Needed for resizing. + this.httprequest.process.isChildShell = false; if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands + "PS1='$ '\n"); } } else { MeshServerLog("Failed to start remote terminal session, no shell found"); @@ -1787,8 +1793,15 @@ function createMeshCore(agent) { // TODO } else { if (ws.httprequest.process == null) return; - //sendConsoleText('Linux-TermSize: ' + obj.cols + 'x' + obj.rows); - // TODO + // ILibDuktape_ChildProcess kill doesn't support sending signals + if (fs.existsSync("/bin/kill")) + { + if (ws.httprequest.process.isChildShell) + // We need to send signal to the child of the process, since the child is the shell + childProcess.execFile('/bin/bash', ['bash', "-c", "kill -SIGWINCH $(pgrep -P " + ws.httprequest.process.pid + ")"]); + else + childProcess.execFile('/bin/bash', ['bash', "-c", "kill -SIGWINCH " + ws.httprequest.process.pid]); + } } } break; @@ -2222,8 +2235,7 @@ function createMeshCore(agent) { break; } case 'ps': { - processManager.getProcesses(function (plist) - { + processManager.getProcesses(function (plist) { var x = ''; for (var i in plist) { x += i + ((plist[i].user) ? (', ' + plist[i].user) : '') + ', ' + plist[i].cmd + '\r\n'; } sendConsoleText(x, sessionid); diff --git a/views/xterm.handlebars b/views/xterm.handlebars index 7e4ce0e4..169108a2 100644 --- a/views/xterm.handlebars +++ b/views/xterm.handlebars @@ -171,6 +171,7 @@ break; case 3: // Connected + 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`); term.focus(); break; default: