- fixes: no more exit fallback to server

This commit is contained in:
Luan Estradioto
2024-10-01 01:42:09 -03:00
parent 852e881805
commit 2c38af1df0
2 changed files with 12 additions and 19 deletions

View File

@@ -132,13 +132,10 @@ async function handleCommand(ws, command, userId) {
// NOTE: - Initiates a process within the Terminal container // NOTE: - Initiates a process within the Terminal container
// Establishes an SSH connection to root@coolify with RequestTTY enabled // Establishes an SSH connection to root@coolify with RequestTTY enabled
// Executes the 'docker exec' command to connect to a specific container // 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([hereDocContent]), options);
const ptyProcess = pty.spawn('ssh', sshArgs.concat(['bash']), options);
userSession.ptyProcess = ptyProcess; userSession.ptyProcess = ptyProcess;
userSession.isActive = true; 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'); ws.send('pty-ready');
@@ -147,6 +144,7 @@ async function handleCommand(ws, command, userId) {
// when parent closes // when parent closes
ptyProcess.onExit(({ exitCode, signal }) => { ptyProcess.onExit(({ exitCode, signal }) => {
console.error(`Process exited with code ${exitCode} and signal ${signal}`); console.error(`Process exited with code ${exitCode} and signal ${signal}`);
ws.send('pty-exited');
userSession.isActive = false; userSession.isActive = false;
}); });

View File

@@ -125,6 +125,10 @@ export function initializeTerminalComponent() {
if (this.term) this.term.reset(); if (this.term) this.term.reset();
this.terminalActive = false; this.terminalActive = false;
this.message = '(sorry, something went wrong, please try again)'; this.message = '(sorry, something went wrong, please try again)';
} else if (event.data === 'pty-exited') {
this.terminalActive = false;
this.term.reset();
this.commandBuffer = '';
} else { } else {
this.pendingWrites++; this.pendingWrites++;
this.term.write(event.data, this.flowControlCallback.bind(this)); 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) { if (this.pendingWrites > this.MAX_PENDING_WRITES && !this.paused) {
this.paused = true; this.paused = true;
this.socket.send(JSON.stringify({ pause: 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.paused = false;
this.socket.send(JSON.stringify({ resume: true })); this.socket.send(JSON.stringify({ resume: true }));
return;
} }
}, },
@@ -147,15 +154,7 @@ export function initializeTerminalComponent() {
this.term.onData((data) => { this.term.onData((data) => {
this.socket.send(JSON.stringify({ message: data })); this.socket.send(JSON.stringify({ message: data }));
// Handle CTRL + D or exit command if (data === '\r') {
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') {
this.commandBuffer = ''; this.commandBuffer = '';
} else { } else {
this.commandBuffer += data; this.commandBuffer += data;
@@ -183,10 +182,6 @@ export function initializeTerminalComponent() {
}); });
}, },
stripAnsiCommands(input) {
return input.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, '');
},
keepAlive() { keepAlive() {
if (this.socket && this.socket.readyState === WebSocket.OPEN) { if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.socket.send(JSON.stringify({ ping: true })); this.socket.send(JSON.stringify({ ping: true }));