From 2c38af1df0311a1c203189bfb6c083ec90ce52c0 Mon Sep 17 00:00:00 2001 From: Luan Estradioto Date: Tue, 1 Oct 2024 01:42:09 -0300 Subject: [PATCH] - fixes: no more exit fallback to server --- docker/coolify-realtime/terminal-server.js | 8 +++----- resources/js/terminal.js | 23 +++++++++------------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/docker/coolify-realtime/terminal-server.js b/docker/coolify-realtime/terminal-server.js index d6b820583..8658ecdf8 100755 --- a/docker/coolify-realtime/terminal-server.js +++ b/docker/coolify-realtime/terminal-server.js @@ -132,13 +132,10 @@ async function handleCommand(ws, command, userId) { // NOTE: - Initiates a process within the Terminal container // Establishes an SSH connection to root@coolify with RequestTTY enabled // Executes the 'docker exec' command to connect to a specific container - // If the user types 'exit', it terminates the container connection and reverts to the server. - const ptyProcess = pty.spawn('ssh', sshArgs.concat(['bash']), options); + const ptyProcess = pty.spawn('ssh', sshArgs.concat([hereDocContent]), options); + userSession.ptyProcess = ptyProcess; userSession.isActive = true; - ptyProcess.write(hereDocContent + '\n'); - // clear the terminal if the user has clear command - ptyProcess.write('command -v clear >/dev/null 2>&1 && clear\n'); ws.send('pty-ready'); @@ -147,6 +144,7 @@ async function handleCommand(ws, command, userId) { // when parent closes ptyProcess.onExit(({ exitCode, signal }) => { console.error(`Process exited with code ${exitCode} and signal ${signal}`); + ws.send('pty-exited'); userSession.isActive = false; }); diff --git a/resources/js/terminal.js b/resources/js/terminal.js index 21854ed63..fb69628c0 100644 --- a/resources/js/terminal.js +++ b/resources/js/terminal.js @@ -125,6 +125,10 @@ export function initializeTerminalComponent() { if (this.term) this.term.reset(); this.terminalActive = false; this.message = '(sorry, something went wrong, please try again)'; + } else if (event.data === 'pty-exited') { + this.terminalActive = false; + this.term.reset(); + this.commandBuffer = ''; } else { this.pendingWrites++; this.term.write(event.data, this.flowControlCallback.bind(this)); @@ -136,9 +140,12 @@ export function initializeTerminalComponent() { if (this.pendingWrites > this.MAX_PENDING_WRITES && !this.paused) { this.paused = true; this.socket.send(JSON.stringify({ pause: true })); - } else if (this.pendingWrites <= this.MAX_PENDING_WRITES && this.paused) { + return; + } + if (this.pendingWrites <= this.MAX_PENDING_WRITES && this.paused) { this.paused = false; this.socket.send(JSON.stringify({ resume: true })); + return; } }, @@ -147,15 +154,7 @@ export function initializeTerminalComponent() { this.term.onData((data) => { this.socket.send(JSON.stringify({ message: data })); - // Handle CTRL + D or exit command - if (data === '\x04' || (data === '\r' && this.stripAnsiCommands(this.commandBuffer).trim().includes('exit'))) { - this.checkIfProcessIsRunningAndKillIt(); - setTimeout(() => { - this.terminalActive = false; - this.term.reset(); - }, 500); - this.commandBuffer = ''; - } else if (data === '\r') { + if (data === '\r') { this.commandBuffer = ''; } else { this.commandBuffer += data; @@ -183,10 +182,6 @@ export function initializeTerminalComponent() { }); }, - stripAnsiCommands(input) { - return input.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, ''); - }, - keepAlive() { if (this.socket && this.socket.readyState === WebSocket.OPEN) { this.socket.send(JSON.stringify({ ping: true }));