mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor: adapt terminal wss to allow local server connection, add status logs
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import type http from "node:http";
|
import type http from "node:http";
|
||||||
import { findServerById, validateWebSocketRequest } from "@dokploy/server";
|
import { findServerById, validateWebSocketRequest } from "@dokploy/server";
|
||||||
import { publicIpv4, publicIpv6 } from "public-ip";
|
import { publicIpv4, publicIpv6 } from "public-ip";
|
||||||
import { Client } from "ssh2";
|
import { Client, type ConnectConfig } from "ssh2";
|
||||||
import { WebSocketServer } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
|
import { getLocalServerPrivateKey } from "./utils";
|
||||||
|
|
||||||
export const getPublicIpWithFallback = async () => {
|
export const getPublicIpWithFallback = async () => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -55,21 +56,67 @@ export const setupTerminalWebSocketServer = (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const server = await findServerById(serverId);
|
let connectionDetails: ConnectConfig = {};
|
||||||
|
|
||||||
if (!server) {
|
const isLocalServer = serverId === "local";
|
||||||
ws.close();
|
|
||||||
return;
|
if (isLocalServer) {
|
||||||
|
const port = Number(url.searchParams.get("port"));
|
||||||
|
const username = url.searchParams.get("username");
|
||||||
|
|
||||||
|
if (!port || !username) {
|
||||||
|
ws.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.send("Getting private SSH key...\n");
|
||||||
|
const privateKey = await getLocalServerPrivateKey();
|
||||||
|
|
||||||
|
if (!privateKey) {
|
||||||
|
ws.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
connectionDetails = {
|
||||||
|
host: "localhost",
|
||||||
|
port,
|
||||||
|
username,
|
||||||
|
privateKey,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
ws.send("Getting server data...\n");
|
||||||
|
const server = await findServerById(serverId);
|
||||||
|
|
||||||
|
if (!server) {
|
||||||
|
ws.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { ipAddress, port, username, sshKey, sshKeyId } = server;
|
||||||
|
|
||||||
|
if (!sshKeyId) {
|
||||||
|
throw new Error("No SSH key available for this server");
|
||||||
|
}
|
||||||
|
|
||||||
|
connectionDetails = {
|
||||||
|
host: ipAddress,
|
||||||
|
port: port,
|
||||||
|
username: username,
|
||||||
|
privateKey: sshKey?.privateKey,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!server.sshKeyId)
|
|
||||||
throw new Error("No SSH key available for this server");
|
|
||||||
|
|
||||||
const conn = new Client();
|
const conn = new Client();
|
||||||
let stdout = "";
|
let stdout = "";
|
||||||
let stderr = "";
|
let stderr = "";
|
||||||
|
|
||||||
|
ws.send("Connecting...\n");
|
||||||
|
|
||||||
conn
|
conn
|
||||||
.once("ready", () => {
|
.once("ready", () => {
|
||||||
|
// Clear terminal content once connected
|
||||||
|
ws.send("\x1bc");
|
||||||
|
|
||||||
conn.shell({}, (err, stream) => {
|
conn.shell({}, (err, stream) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
@@ -112,18 +159,13 @@ export const setupTerminalWebSocketServer = (
|
|||||||
.on("error", (err) => {
|
.on("error", (err) => {
|
||||||
if (err.level === "client-authentication") {
|
if (err.level === "client-authentication") {
|
||||||
ws.send(
|
ws.send(
|
||||||
`Authentication failed: Invalid SSH private key. ❌ Error: ${err.message} ${err.level}`,
|
`Authentication failed: Unauthorized ${isLocalServer ? "" : "private SSH key or "}username.\n❌ Error: ${err.message} ${err.level}`,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
ws.send(`SSH connection error: ${err.message}`);
|
ws.send(`SSH connection error: ${err.message}`);
|
||||||
}
|
}
|
||||||
conn.end();
|
conn.end();
|
||||||
})
|
})
|
||||||
.connect({
|
.connect(connectionDetails);
|
||||||
host: server.ipAddress,
|
|
||||||
port: server.port,
|
|
||||||
username: server.username,
|
|
||||||
privateKey: server.sshKey?.privateKey,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user