mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor: rename builders to server
This commit is contained in:
107
packages/server/src/wss/terminal.ts
Normal file
107
packages/server/src/wss/terminal.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import type http from "node:http";
|
||||
import path from "node:path";
|
||||
import { findServerById } from "@/server/services/server";
|
||||
import { spawn } from "node-pty";
|
||||
import { publicIpv4, publicIpv6 } from "public-ip";
|
||||
import { WebSocketServer } from "ws";
|
||||
import { validateWebSocketRequest } from "../auth/auth";
|
||||
import { paths } from "../constants";
|
||||
|
||||
export const getPublicIpWithFallback = async () => {
|
||||
// @ts-ignore
|
||||
let ip = null;
|
||||
try {
|
||||
ip = await publicIpv4();
|
||||
} catch (error) {
|
||||
console.log(
|
||||
"Error to obtain public IPv4 address, falling back to IPv6",
|
||||
// @ts-ignore
|
||||
error.message,
|
||||
);
|
||||
try {
|
||||
ip = await publicIpv6();
|
||||
} catch (error) {
|
||||
// @ts-ignore
|
||||
console.error("Error to obtain public IPv6 address", error.message);
|
||||
ip = null;
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
};
|
||||
|
||||
export const setupTerminalWebSocketServer = (
|
||||
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
|
||||
) => {
|
||||
const wssTerm = new WebSocketServer({
|
||||
noServer: true,
|
||||
path: "/terminal",
|
||||
});
|
||||
|
||||
server.on("upgrade", (req, socket, head) => {
|
||||
const { pathname } = new URL(req.url || "", `http://${req.headers.host}`);
|
||||
if (pathname === "/_next/webpack-hmr") {
|
||||
return;
|
||||
}
|
||||
if (pathname === "/terminal") {
|
||||
wssTerm.handleUpgrade(req, socket, head, function done(ws) {
|
||||
wssTerm.emit("connection", ws, req);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
wssTerm.on("connection", async (ws, req) => {
|
||||
const url = new URL(req.url || "", `http://${req.headers.host}`);
|
||||
const serverId = url.searchParams.get("serverId");
|
||||
const { user, session } = await validateWebSocketRequest(req);
|
||||
if (!user || !session || !serverId) {
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
|
||||
const server = await findServerById(serverId);
|
||||
|
||||
if (!server) {
|
||||
ws.close();
|
||||
return;
|
||||
}
|
||||
const { SSH_PATH } = paths();
|
||||
const privateKey = path.join(SSH_PATH, `${server.sshKeyId}_rsa`);
|
||||
const sshCommand = [
|
||||
"ssh",
|
||||
"-o",
|
||||
"StrictHostKeyChecking=no",
|
||||
"-i",
|
||||
privateKey,
|
||||
`${server.username}@${server.ipAddress}`,
|
||||
];
|
||||
const ptyProcess = spawn("ssh", sshCommand.slice(1), {
|
||||
name: "xterm-256color",
|
||||
cwd: process.env.HOME,
|
||||
env: process.env,
|
||||
encoding: "utf8",
|
||||
cols: 80,
|
||||
rows: 30,
|
||||
});
|
||||
|
||||
ptyProcess.onData((data) => {
|
||||
ws.send(data);
|
||||
});
|
||||
ws.on("message", (message) => {
|
||||
try {
|
||||
let command: string | Buffer[] | Buffer | ArrayBuffer;
|
||||
if (Buffer.isBuffer(message)) {
|
||||
command = message.toString("utf8");
|
||||
} else {
|
||||
command = message;
|
||||
}
|
||||
ptyProcess.write(command.toString());
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
});
|
||||
|
||||
ws.on("close", () => {
|
||||
ptyProcess.kill();
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user