refactor: code cleanup

This commit is contained in:
UndefinedPony
2024-12-22 19:01:02 +01:00
parent fd0a9b8b58
commit 228d12a61c
4 changed files with 10 additions and 12 deletions

View File

@@ -66,7 +66,7 @@ export const TerminalModal = ({ children, serverId }: Props) => {
)} )}
<div className="flex flex-col gap-4 h-[552px]"> <div className="flex flex-col gap-4 h-[552px]">
<Terminal key={terminalKey} id="terminal" serverId={serverId} /> <Terminal id="terminal" key={terminalKey} serverId={serverId} />
</div> </div>
</DialogContent> </DialogContent>
</Dialog> </Dialog>

View File

@@ -41,12 +41,10 @@ export const Terminal: React.FC<Props> = ({ id, serverId }) => {
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const isLocalServer = serverId === "local";
const urlParams = new URLSearchParams(); const urlParams = new URLSearchParams();
urlParams.set("serverId", serverId); urlParams.set("serverId", serverId);
if (isLocalServer) { if (serverId === "local") {
const { port, username } = getLocalServerData(); const { port, username } = getLocalServerData();
urlParams.set("port", port.toString()); urlParams.set("port", port.toString());
urlParams.set("username", username); urlParams.set("username", username);

View File

@@ -3,7 +3,7 @@ import { findServerById, validateWebSocketRequest } from "@dokploy/server";
import { publicIpv4, publicIpv6 } from "public-ip"; import { publicIpv4, publicIpv6 } from "public-ip";
import { Client, type ConnectConfig } from "ssh2"; import { Client, type ConnectConfig } from "ssh2";
import { WebSocketServer } from "ws"; import { WebSocketServer } from "ws";
import { getLocalServerPrivateKey } from "./utils"; import { setupLocalServerSSHKey } from "./utils";
export const getPublicIpWithFallback = async () => { export const getPublicIpWithFallback = async () => {
// @ts-ignore // @ts-ignore
@@ -69,8 +69,8 @@ export const setupTerminalWebSocketServer = (
return; return;
} }
ws.send("Getting private SSH key...\n"); ws.send("Setting up private SSH key...\n");
const privateKey = await getLocalServerPrivateKey(); const privateKey = await setupLocalServerSSHKey();
if (!privateKey) { if (!privateKey) {
ws.close(); ws.close();
@@ -92,16 +92,16 @@ export const setupTerminalWebSocketServer = (
return; return;
} }
const { ipAddress, port, username, sshKey, sshKeyId } = server; const { ipAddress: host, port, username, sshKey, sshKeyId } = server;
if (!sshKeyId) { if (!sshKeyId) {
throw new Error("No SSH key available for this server"); throw new Error("No SSH key available for this server");
} }
connectionDetails = { connectionDetails = {
host: ipAddress, host,
port: port, port,
username: username, username,
privateKey: sshKey?.privateKey, privateKey: sshKey?.privateKey,
}; };
} }

View File

@@ -25,7 +25,7 @@ export const getShell = () => {
}; };
/** Returns private SSH key for dokploy local server terminal. Uses already created SSH key or generates a new SSH key, also automatically appends the public key to `authorized_keys`, creating the file if needed. */ /** Returns private SSH key for dokploy local server terminal. Uses already created SSH key or generates a new SSH key, also automatically appends the public key to `authorized_keys`, creating the file if needed. */
export const getLocalServerPrivateKey = async () => { export const setupLocalServerSSHKey = async () => {
try { try {
if (!fs.existsSync(LOCAL_SSH_KEY_PATH)) { if (!fs.existsSync(LOCAL_SSH_KEY_PATH)) {
// Generate new SSH key if it hasn't been created yet // Generate new SSH key if it hasn't been created yet