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

142 lines
4.4 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,
getComposeTerminalName,
isDev,
PROGRESS_TERMINAL_ROWS
} from "../util-common";
import { MainTerminal, Terminal } from "../terminal";
2023-10-26 07:23:45 +02:00
export class TerminalSocketHandler extends SocketHandler {
create(socket : DockgeSocket, server : DockgeServer) {
socket.on("terminalInputRaw", async (key : unknown) => {
try {
checkLogin(socket);
if (typeof(key) !== "string") {
throw new Error("Key must be a string.");
}
if (allowedRawKeys.includes(key)) {
server.terminal.write(key);
}
} catch (e) {
}
});
socket.on("terminalInput", async (terminalName : unknown, cmd : unknown, errorCallback : unknown) => {
try {
checkLogin(socket);
if (typeof(cmd) !== "string") {
throw new Error("Command must be a string.");
}
// Check if the command is allowed
const cmdParts = cmd.split(" ");
const executable = cmdParts[0].trim();
log.debug("console", "Executable: " + executable);
log.debug("console", "Executable length: " + executable.length);
if (!allowedCommandList.includes(executable)) {
throw new Error("Command not allowed.");
}
server.terminal.write(cmd);
} catch (e) {
if (typeof(errorCallback) === "function") {
errorCallback({
ok: false,
msg: e.message,
});
}
}
});
// Create Terminal
2023-10-29 08:25:52 +01:00
socket.on("mainTerminal", async (terminalName : unknown, callback) => {
try {
checkLogin(socket);
if (typeof(terminalName) !== "string") {
throw new ValidationError("Terminal name must be a string.");
}
log.debug("deployStack", "Terminal name: " + terminalName);
2023-10-26 07:23:45 +02:00
2023-10-29 08:25:52 +01:00
let terminal = Terminal.getTerminal(terminalName);
if (!terminal) {
terminal = new MainTerminal(server, terminalName);
terminal.rows = 50;
log.debug("deployStack", "Terminal created");
}
terminal.join(socket);
terminal.start();
callback({
ok: true,
});
} catch (e) {
callbackError(e, callback);
}
2023-10-26 07:23:45 +02:00
});
// Join 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) => {
});
// Resize Terminal
socket.on("terminalResize", async (rows : unknown) => {
try {
checkLogin(socket);
if (typeof(rows) !== "number") {
throw new Error("Rows must be a number.");
}
log.debug("console", "Resize terminal to " + rows + " rows.");
server.terminal.resize(rows);
} catch (e) {
}
});
}
}