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

151 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-10-26 05:23:45 +00: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 07:25:52 +00:00
import {
allowedCommandList,
allowedRawKeys,
2023-11-05 17:18:02 +00:00
getComposeTerminalName, getContainerExecTerminalName,
2023-10-29 07:25:52 +00:00
isDev,
PROGRESS_TERMINAL_ROWS
} from "../util-common";
2023-11-05 17:18:02 +00:00
import { InteractiveTerminal, MainTerminal, Terminal } from "../terminal";
2023-10-26 05:23:45 +00:00
export class TerminalSocketHandler extends SocketHandler {
create(socket : DockgeSocket, server : DockgeServer) {
2023-11-05 17:18:02 +00:00
socket.on("terminalInput", async (terminalName : unknown, cmd : unknown, errorCallback) => {
2023-10-26 05:23:45 +00:00
try {
checkLogin(socket);
2023-11-05 17:18:02 +00:00
if (typeof(terminalName) !== "string") {
throw new Error("Terminal name must be a string.");
2023-10-26 05:23:45 +00:00
}
2023-11-05 17:18:02 +00:00
if (typeof(cmd) !== "string") {
throw new Error("Command must be a string.");
2023-10-26 05:23:45 +00:00
}
2023-11-05 17:18:02 +00:00
let terminal = Terminal.getTerminal(terminalName);
if (terminal instanceof InteractiveTerminal) {
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 05:23:45 +00:00
}
});
2023-11-05 17:18:02 +00:00
// Main Terminal
socket.on("mainTerminal", async (terminalName : unknown, callback) => {
2023-10-26 05:23:45 +00:00
try {
checkLogin(socket);
2023-11-05 17:18:02 +00: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 05:23:45 +00:00
}
2023-11-05 17:18:02 +00:00
log.debug("deployStack", "Terminal name: " + terminalName);
let terminal = Terminal.getTerminal(terminalName);
2023-10-26 05:23:45 +00:00
2023-11-05 17:18:02 +00:00
if (!terminal) {
terminal = new MainTerminal(server, terminalName);
terminal.rows = 50;
log.debug("deployStack", "Terminal created");
2023-10-26 05:23:45 +00:00
}
2023-11-05 17:18:02 +00:00
terminal.join(socket);
terminal.start();
callback({
ok: true,
});
2023-10-26 05:23:45 +00:00
} catch (e) {
2023-11-05 17:18:02 +00:00
callbackError(e, callback);
2023-10-26 05:23:45 +00:00
}
});
2023-11-05 17:18:02 +00:00
// Interactive Terminal for containers
socket.on("interactiveTerminal", async (stackName : unknown, serviceName : unknown, callback) => {
2023-10-29 07:25:52 +00:00
try {
checkLogin(socket);
2023-11-05 17:18:02 +00:00
if (typeof(stackName) !== "string") {
throw new ValidationError("Stack name must be a string.");
2023-10-29 07:25:52 +00:00
}
2023-11-05 17:18:02 +00:00
if (typeof(serviceName) !== "string") {
throw new ValidationError("Service name must be a string.");
}
2023-10-26 05:23:45 +00:00
2023-11-05 17:18:02 +00:00
const terminalName = getContainerExecTerminalName(stackName, serviceName, 0);
2023-10-29 07:25:52 +00:00
let terminal = Terminal.getTerminal(terminalName);
if (!terminal) {
2023-11-05 17:18:02 +00:00
terminal = new InteractiveTerminal(server, terminalName);
2023-10-29 07:25:52 +00:00
terminal.rows = 50;
log.debug("deployStack", "Terminal created");
}
terminal.join(socket);
terminal.start();
callback({
ok: true,
});
} catch (e) {
callbackError(e, callback);
}
2023-10-26 05:23:45 +00:00
});
2023-11-05 17:18:02 +00:00
// Join Output Terminal
2023-10-29 07:25:52 +00:00
socket.on("terminalJoin", async (terminalName : unknown, callback) => {
2023-10-26 05:23:45 +00: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 17:18:02 +00:00
// TODO: Resize Terminal
2023-10-26 05:23:45 +00:00
socket.on("terminalResize", async (rows : unknown) => {
});
}
}