dockge/backend/socket-handlers/terminal-socket-handler.ts

152 lines
5.0 KiB
TypeScript
Raw Normal View History

2023-10-26 07:23:45 +02:00
import { SocketHandler } from "../socket-handler.js";
import { DockgeServer } from "../dockge-server";
import { callbackError, checkLogin, DockgeSocket, ValidationError } from "../util-server";
import { log } from "../log";
import yaml from "yaml";
import path from "path";
import fs from "fs";
2023-10-29 08:25:52 +01:00
import {
allowedCommandList,
allowedRawKeys,
2023-11-05 18:18:02 +01:00
getComposeTerminalName, getContainerExecTerminalName,
2023-10-29 08:25:52 +01:00
isDev,
PROGRESS_TERMINAL_ROWS
} from "../util-common";
2023-11-05 18:18:02 +01:00
import { InteractiveTerminal, MainTerminal, Terminal } from "../terminal";
2023-11-06 08:15:55 +01:00
import { Stack } from "../stack";
2023-10-26 07:23:45 +02:00
export class TerminalSocketHandler extends SocketHandler {
create(socket : DockgeSocket, server : DockgeServer) {
2023-11-05 18:18:02 +01:00
socket.on("terminalInput", async (terminalName : unknown, cmd : unknown, errorCallback) => {
2023-10-26 07:23:45 +02:00
try {
checkLogin(socket);
2023-11-05 18:18:02 +01:00
if (typeof(terminalName) !== "string") {
throw new Error("Terminal name must be a string.");
2023-10-26 07:23:45 +02:00
}
2023-11-05 18:18:02 +01:00
if (typeof(cmd) !== "string") {
throw new Error("Command must be a string.");
2023-10-26 07:23:45 +02:00
}
2023-11-05 18:18:02 +01:00
let terminal = Terminal.getTerminal(terminalName);
if (terminal instanceof InteractiveTerminal) {
2023-11-06 14:24:06 +01:00
//log.debug("terminalInput", "Terminal found, writing to terminal.");
2023-11-05 18:18:02 +01:00
terminal.write(cmd);
} else {
throw new Error("Terminal not found or it is not a Interactive Terminal.");
}
} catch (e) {
errorCallback({
ok: false,
msg: e.message,
});
2023-10-26 07:23:45 +02:00
}
});
2023-11-05 18:18:02 +01:00
// Main Terminal
socket.on("mainTerminal", async (terminalName : unknown, callback) => {
2023-10-26 07:23:45 +02:00
try {
checkLogin(socket);
2023-11-05 18:18:02 +01:00
// TODO: Reset the name here, force one main terminal for now
terminalName = "console";
if (typeof(terminalName) !== "string") {
throw new ValidationError("Terminal name must be a string.");
2023-10-26 07:23:45 +02:00
}
2023-11-05 18:18:02 +01:00
log.debug("deployStack", "Terminal name: " + terminalName);
let terminal = Terminal.getTerminal(terminalName);
2023-10-26 07:23:45 +02:00
2023-11-05 18:18:02 +01:00
if (!terminal) {
terminal = new MainTerminal(server, terminalName);
terminal.rows = 50;
log.debug("deployStack", "Terminal created");
2023-10-26 07:23:45 +02:00
}
2023-11-05 18:18:02 +01:00
terminal.join(socket);
terminal.start();
callback({
ok: true,
});
2023-10-26 07:23:45 +02:00
} catch (e) {
2023-11-05 18:18:02 +01:00
callbackError(e, callback);
2023-10-26 07:23:45 +02:00
}
});
2023-11-05 18:18:02 +01:00
// Interactive Terminal for containers
2023-11-06 14:24:06 +01:00
socket.on("interactiveTerminal", async (stackName : unknown, serviceName : unknown, shell : unknown, callback) => {
2023-10-29 08:25:52 +01:00
try {
checkLogin(socket);
2023-11-05 18:18:02 +01:00
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string.");
2023-10-29 08:25:52 +01:00
}
2023-11-05 18:18:02 +01:00
if (typeof(serviceName) !== "string") {
throw new ValidationError("Service name must be a string.");
}
2023-10-26 07:23:45 +02:00
2023-11-06 14:24:06 +01:00
if (typeof(shell) !== "string") {
throw new ValidationError("Shell must be a string.");
}
2023-11-06 08:15:55 +01:00
log.debug("interactiveTerminal", "Stack name: " + stackName);
log.debug("interactiveTerminal", "Service name: " + serviceName);
2023-10-29 08:25:52 +01:00
2023-11-06 08:15:55 +01:00
// Get stack
const stack = Stack.getStack(server, stackName);
2023-11-06 14:24:06 +01:00
stack.joinContainerTerminal(socket, serviceName, shell);
2023-10-29 08:25:52 +01:00
callback({
ok: true,
});
} catch (e) {
callbackError(e, callback);
}
2023-10-26 07:23:45 +02:00
});
2023-11-05 18:18:02 +01:00
// Join Output Terminal
2023-10-29 08:25:52 +01:00
socket.on("terminalJoin", async (terminalName : unknown, callback) => {
2023-10-26 07:23:45 +02:00
if (typeof(callback) !== "function") {
log.debug("console", "Callback is not a function.");
return;
}
try {
checkLogin(socket);
if (typeof(terminalName) !== "string") {
throw new ValidationError("Terminal name must be a string.");
}
let buffer : string = Terminal.getTerminal(terminalName)?.getBuffer() ?? "";
if (!buffer) {
log.debug("console", "No buffer found.");
}
callback({
ok: true,
buffer,
});
} catch (e) {
callbackError(e, callback);
}
});
// Close Terminal
socket.on("terminalClose", async (terminalName : unknown, callback : unknown) => {
});
2023-11-05 18:18:02 +01:00
// TODO: Resize Terminal
2023-10-26 07:23:45 +02:00
socket.on("terminalResize", async (rows : unknown) => {
});
}
}