mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor(terminal): use ssh2 instead of cmd
This commit is contained in:
@@ -1,13 +1,8 @@
|
|||||||
import type http from "node:http";
|
import type http from "node:http";
|
||||||
import path from "node:path";
|
|
||||||
import { spawn } from "node-pty";
|
|
||||||
import { publicIpv4, publicIpv6 } from "public-ip";
|
import { publicIpv4, publicIpv6 } from "public-ip";
|
||||||
import { WebSocketServer } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
import {
|
import { findServerById, validateWebSocketRequest } from "@dokploy/builders";
|
||||||
findServerById,
|
import { Client } from "ssh2";
|
||||||
validateWebSocketRequest,
|
|
||||||
paths,
|
|
||||||
} from "@dokploy/builders";
|
|
||||||
|
|
||||||
export const getPublicIpWithFallback = async () => {
|
export const getPublicIpWithFallback = async () => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -66,46 +61,80 @@ export const setupTerminalWebSocketServer = (
|
|||||||
ws.close();
|
ws.close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { SSH_PATH } = paths();
|
|
||||||
const privateKey = path.join(SSH_PATH, `${server.sshKeyId}_rsa`);
|
|
||||||
const sshCommand = [
|
|
||||||
"ssh",
|
|
||||||
"-o",
|
|
||||||
"StrictHostKeyChecking=no",
|
|
||||||
"-i",
|
|
||||||
privateKey,
|
|
||||||
"-p",
|
|
||||||
`${server.port}`,
|
|
||||||
`${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) => {
|
if (!server.sshKeyId)
|
||||||
ws.send(data);
|
throw new Error("No SSH key available for this server");
|
||||||
});
|
|
||||||
ws.on("message", (message) => {
|
const conn = new Client();
|
||||||
try {
|
let stdout = "";
|
||||||
let command: string | Buffer[] | Buffer | ArrayBuffer;
|
let stderr = "";
|
||||||
if (Buffer.isBuffer(message)) {
|
conn
|
||||||
command = message.toString("utf8");
|
.once("ready", () => {
|
||||||
|
conn.shell(
|
||||||
|
{
|
||||||
|
term: "terminal",
|
||||||
|
cols: 80,
|
||||||
|
rows: 30,
|
||||||
|
height: 30,
|
||||||
|
width: 80,
|
||||||
|
},
|
||||||
|
(err, stream) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
stream
|
||||||
|
.on("close", (code: number, signal: string) => {
|
||||||
|
ws.send(`\nContainer closed with code: ${code}\n`);
|
||||||
|
conn.end();
|
||||||
|
})
|
||||||
|
.on("data", (data: string) => {
|
||||||
|
stdout += data.toString();
|
||||||
|
ws.send(data.toString());
|
||||||
|
})
|
||||||
|
.stderr.on("data", (data) => {
|
||||||
|
stderr += data.toString();
|
||||||
|
ws.send(data.toString());
|
||||||
|
console.error("Error: ", data.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on("message", (message) => {
|
||||||
|
try {
|
||||||
|
let command: string | Buffer[] | Buffer | ArrayBuffer;
|
||||||
|
if (Buffer.isBuffer(message)) {
|
||||||
|
command = message.toString("utf8");
|
||||||
|
} else {
|
||||||
|
command = message;
|
||||||
|
}
|
||||||
|
stream.write(command.toString());
|
||||||
|
} catch (error) {
|
||||||
|
// @ts-ignore
|
||||||
|
const errorMessage = error?.message as unknown as string;
|
||||||
|
ws.send(errorMessage);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on("close", () => {
|
||||||
|
console.log("Connection closed ✅");
|
||||||
|
stream.end();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.on("error", (err) => {
|
||||||
|
if (err.level === "client-authentication") {
|
||||||
|
ws.send(
|
||||||
|
`Authentication failed: Invalid SSH private key. ❌ Error: ${err.message} ${err.level}`,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
command = message;
|
ws.send(`SSH connection error: ${err.message}`);
|
||||||
}
|
}
|
||||||
ptyProcess.write(command.toString());
|
conn.end();
|
||||||
} catch (error) {
|
})
|
||||||
console.log(error);
|
.connect({
|
||||||
}
|
host: server.ipAddress,
|
||||||
});
|
port: server.port,
|
||||||
|
username: server.username,
|
||||||
ws.on("close", () => {
|
privateKey: server.sshKey?.privateKey,
|
||||||
ptyProcess.kill();
|
timeout: 99999,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user