2023-11-11 14:18:37 +00:00
|
|
|
import { DockgeServer } from "../dockge-server";
|
2023-12-25 10:02:46 +00:00
|
|
|
import { callbackError, callbackResult, checkLogin, DockgeSocket, ValidationError } from "../util-server";
|
2023-11-11 14:18:37 +00:00
|
|
|
import { log } from "../log";
|
|
|
|
import { InteractiveTerminal, MainTerminal, Terminal } from "../terminal";
|
|
|
|
import { Stack } from "../stack";
|
2023-12-20 02:11:00 +00:00
|
|
|
import { AgentSocketHandler } from "../agent-socket-handler";
|
|
|
|
import { AgentSocket } from "../../common/agent-socket";
|
2023-11-11 14:18:37 +00:00
|
|
|
|
2023-12-20 02:11:00 +00:00
|
|
|
export class TerminalSocketHandler extends AgentSocketHandler {
|
|
|
|
create(s : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
|
2023-11-11 14:18:37 +00:00
|
|
|
|
2023-12-25 10:02:46 +00:00
|
|
|
agentSocket.on("terminalInput", async (terminalName : unknown, cmd : unknown, callback) => {
|
2023-11-11 14:18:37 +00:00
|
|
|
try {
|
2023-12-20 02:11:00 +00:00
|
|
|
checkLogin(s);
|
2023-11-11 14:18:37 +00:00
|
|
|
|
|
|
|
if (typeof(terminalName) !== "string") {
|
|
|
|
throw new Error("Terminal name must be a string.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(cmd) !== "string") {
|
|
|
|
throw new Error("Command must be a string.");
|
|
|
|
}
|
|
|
|
|
|
|
|
let terminal = Terminal.getTerminal(terminalName);
|
|
|
|
if (terminal instanceof InteractiveTerminal) {
|
|
|
|
//log.debug("terminalInput", "Terminal found, writing to terminal.");
|
|
|
|
terminal.write(cmd);
|
|
|
|
} else {
|
|
|
|
throw new Error("Terminal not found or it is not a Interactive Terminal.");
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2023-12-25 10:02:46 +00:00
|
|
|
callbackError(e, callback);
|
2023-11-11 14:18:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Main Terminal
|
2023-12-20 02:11:00 +00:00
|
|
|
agentSocket.on("mainTerminal", async (terminalName : unknown, callback) => {
|
2023-11-11 14:18:37 +00:00
|
|
|
try {
|
2023-12-20 02:11:00 +00:00
|
|
|
checkLogin(s);
|
2023-11-11 14:18:37 +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-12-25 10:02:46 +00:00
|
|
|
log.debug("mainTerminal", "Terminal name: " + terminalName);
|
2023-11-11 14:18:37 +00:00
|
|
|
|
|
|
|
let terminal = Terminal.getTerminal(terminalName);
|
|
|
|
|
|
|
|
if (!terminal) {
|
|
|
|
terminal = new MainTerminal(server, terminalName);
|
|
|
|
terminal.rows = 50;
|
2023-12-25 10:02:46 +00:00
|
|
|
log.debug("mainTerminal", "Terminal created");
|
2023-11-11 14:18:37 +00:00
|
|
|
}
|
|
|
|
|
2023-12-25 10:02:46 +00:00
|
|
|
terminal.join(s);
|
2023-11-11 14:18:37 +00:00
|
|
|
terminal.start();
|
|
|
|
|
2023-12-25 10:02:46 +00:00
|
|
|
callbackResult({
|
2023-11-11 14:18:37 +00:00
|
|
|
ok: true,
|
2023-12-25 10:02:46 +00:00
|
|
|
}, callback);
|
2023-11-11 14:18:37 +00:00
|
|
|
} catch (e) {
|
|
|
|
callbackError(e, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Interactive Terminal for containers
|
2023-12-20 02:11:00 +00:00
|
|
|
agentSocket.on("interactiveTerminal", async (stackName : unknown, serviceName : unknown, shell : unknown, callback) => {
|
2023-11-11 14:18:37 +00:00
|
|
|
try {
|
2023-12-25 10:02:46 +00:00
|
|
|
checkLogin(s);
|
2023-11-11 14:18:37 +00:00
|
|
|
|
|
|
|
if (typeof(stackName) !== "string") {
|
|
|
|
throw new ValidationError("Stack name must be a string.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(serviceName) !== "string") {
|
|
|
|
throw new ValidationError("Service name must be a string.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(shell) !== "string") {
|
|
|
|
throw new ValidationError("Shell must be a string.");
|
|
|
|
}
|
|
|
|
|
|
|
|
log.debug("interactiveTerminal", "Stack name: " + stackName);
|
|
|
|
log.debug("interactiveTerminal", "Service name: " + serviceName);
|
|
|
|
|
|
|
|
// Get stack
|
2023-12-03 13:30:50 +00:00
|
|
|
const stack = await Stack.getStack(server, stackName);
|
2023-12-25 10:02:46 +00:00
|
|
|
stack.joinContainerTerminal(s, serviceName, shell);
|
2023-11-11 14:18:37 +00:00
|
|
|
|
2023-12-25 10:02:46 +00:00
|
|
|
callbackResult({
|
2023-11-11 14:18:37 +00:00
|
|
|
ok: true,
|
2023-12-25 10:02:46 +00:00
|
|
|
}, callback);
|
2023-11-11 14:18:37 +00:00
|
|
|
} catch (e) {
|
|
|
|
callbackError(e, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Join Output Terminal
|
2023-12-20 02:11:00 +00:00
|
|
|
agentSocket.on("terminalJoin", async (terminalName : unknown, callback) => {
|
2023-11-11 14:18:37 +00:00
|
|
|
if (typeof(callback) !== "function") {
|
|
|
|
log.debug("console", "Callback is not a function.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-12-20 02:11:00 +00:00
|
|
|
checkLogin(s);
|
2023-11-11 14:18:37 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-11-24 18:04:16 +00:00
|
|
|
// Leave Combined Terminal
|
2023-12-20 02:11:00 +00:00
|
|
|
agentSocket.on("leaveCombinedTerminal", async (stackName : unknown, callback) => {
|
2023-11-24 18:04:16 +00:00
|
|
|
try {
|
2023-12-20 02:11:00 +00:00
|
|
|
checkLogin(s);
|
2023-11-24 18:04:16 +00:00
|
|
|
|
|
|
|
log.debug("leaveCombinedTerminal", "Stack name: " + stackName);
|
|
|
|
|
|
|
|
if (typeof(stackName) !== "string") {
|
|
|
|
throw new ValidationError("Stack name must be a string.");
|
|
|
|
}
|
2023-11-11 14:18:37 +00:00
|
|
|
|
2023-12-03 13:30:50 +00:00
|
|
|
const stack = await Stack.getStack(server, stackName);
|
2023-12-20 02:11:00 +00:00
|
|
|
await stack.leaveCombinedTerminal(s);
|
2023-11-24 18:04:16 +00:00
|
|
|
|
2023-12-25 10:02:46 +00:00
|
|
|
callbackResult({
|
2023-11-24 18:04:16 +00:00
|
|
|
ok: true,
|
2023-12-25 10:02:46 +00:00
|
|
|
}, callback);
|
2023-11-24 18:04:16 +00:00
|
|
|
} catch (e) {
|
|
|
|
callbackError(e, callback);
|
|
|
|
}
|
2023-11-11 14:18:37 +00:00
|
|
|
});
|
|
|
|
|
2023-12-16 09:57:21 +00:00
|
|
|
// Resize Terminal
|
2023-12-20 02:11:00 +00:00
|
|
|
agentSocket.on("terminalResize", async (terminalName: unknown, rows: unknown, cols: unknown) => {
|
|
|
|
log.info("terminalResize", `Terminal: ${terminalName}`);
|
|
|
|
try {
|
2023-12-25 10:02:46 +00:00
|
|
|
checkLogin(s);
|
2023-12-20 02:11:00 +00:00
|
|
|
if (typeof terminalName !== "string") {
|
|
|
|
throw new Error("Terminal name must be a string.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof rows !== "number") {
|
|
|
|
throw new Error("Command must be a number.");
|
|
|
|
}
|
|
|
|
if (typeof cols !== "number") {
|
|
|
|
throw new Error("Command must be a number.");
|
|
|
|
}
|
|
|
|
|
|
|
|
let terminal = Terminal.getTerminal(terminalName);
|
|
|
|
|
|
|
|
// log.info("terminal", terminal);
|
|
|
|
if (terminal instanceof Terminal) {
|
|
|
|
//log.debug("terminalInput", "Terminal found, writing to terminal.");
|
|
|
|
terminal.rows = rows;
|
|
|
|
terminal.cols = cols;
|
|
|
|
} else {
|
|
|
|
throw new Error(`${terminalName} Terminal not found.`);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log.debug("terminalResize",
|
2023-12-16 09:57:21 +00:00
|
|
|
// Added to prevent the lint error when adding the type
|
|
|
|
// and ts type checker saying type is unknown.
|
|
|
|
// @ts-ignore
|
|
|
|
`Error on ${terminalName}: ${e.message}`
|
2023-12-20 02:11:00 +00:00
|
|
|
);
|
2023-12-16 09:57:21 +00:00
|
|
|
}
|
2023-12-20 02:11:00 +00:00
|
|
|
});
|
2023-11-11 14:18:37 +00:00
|
|
|
}
|
|
|
|
}
|