Merge pull request #868 from TotallyNotElite/master

[WIP] Add dynamic resizing of xterm tty
This commit is contained in:
Ylian Saint-Hilaire 2020-01-24 14:51:27 -08:00 committed by GitHub
commit 3e9543adf4
2 changed files with 20 additions and 7 deletions

View File

@ -39,8 +39,7 @@ var MESHRIGHT_LIMITEDINPUT = 4096;
function createMeshCore(agent) { function createMeshCore(agent) {
var obj = {}; 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 // Check the Agent Uninstall MetaData for correctness, as the installer may have written an incorrect value
try { try {
var writtenSize = 0, actualSize = Math.floor(require('fs').statSync(process.execPath).size / 1024); var writtenSize = 0, actualSize = Math.floor(require('fs').statSync(process.execPath).size / 1024);
@ -1224,21 +1223,28 @@ function createMeshCore(agent) {
var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: env }; var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: env };
var setupcommands = ' alias ls=\'ls --color=auto\'\n'; var setupcommands = ' alias ls=\'ls --color=auto\'\n';
if (shell == sh) setupcommands += ' stty erase ^H\n'; if (shell == sh) setupcommands += ' stty erase ^H\n';
setupcommands += ' clear\n';
if (script && shell && process.platform == 'linux') { if (script && shell && process.platform == 'linux') {
this.httprequest.process = childProcess.execFile(script, ['script', '--return', '--quiet', '-c', '"' + shell + '"', '/dev/null'], options); // Start as active user 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); this.httprequest.process.stdin.write(setupcommands);
} else if (python && shell) { } else if (python && shell) {
this.httprequest.process = childProcess.execFile(python, ['python', '-c', 'import pty; pty.spawn(["' + shell + '"])'], options); // Start as active user 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); } if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
} else if (bash) { } else if (bash) {
options.type = childProcess.SpawnTypes.TERM; options.type = childProcess.SpawnTypes.TERM;
this.httprequest.process = childProcess.execFile(bash, ['bash', '-i'], options); // Start as active user 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); } if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
} else if (sh) { } else if (sh) {
options.type = childProcess.SpawnTypes.TERM; options.type = childProcess.SpawnTypes.TERM;
this.httprequest.process = childProcess.execFile(sh, ['sh'], options); // Start as active user 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"); } if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands + "PS1='$ '\n"); }
} else { } else {
MeshServerLog("Failed to start remote terminal session, no shell found"); MeshServerLog("Failed to start remote terminal session, no shell found");
@ -1791,8 +1797,15 @@ function createMeshCore(agent) {
// TODO // TODO
} else { } else {
if (ws.httprequest.process == null) return; if (ws.httprequest.process == null) return;
//sendConsoleText('Linux-TermSize: ' + obj.cols + 'x' + obj.rows); // ILibDuktape_ChildProcess kill doesn't support sending signals
// TODO 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; break;
@ -2226,8 +2239,7 @@ function createMeshCore(agent) {
break; break;
} }
case 'ps': { case 'ps': {
processManager.getProcesses(function (plist) processManager.getProcesses(function (plist) {
{
var x = ''; var x = '';
for (var i in plist) { x += i + ((plist[i].user) ? (', ' + plist[i].user) : '') + ', ' + plist[i].cmd + '\r\n'; } for (var i in plist) { x += i + ((plist[i].user) ? (', ' + plist[i].user) : '') + ', ' + plist[i].cmd + '\r\n'; }
sendConsoleText(x, sessionid); sendConsoleText(x, sessionid);

View File

@ -180,6 +180,7 @@
break; break;
case 3: case 3:
// Connected // 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(); term.focus();
break; break;
default: default: