Skip to content

Commit

Permalink
fix: connecting to local server terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
szwabodev committed Jan 6, 2025
1 parent aa3541b commit d0a5427
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 54 deletions.
54 changes: 41 additions & 13 deletions apps/dokploy/server/wss/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import { publicIpv4, publicIpv6 } from "public-ip";
import { Client, type ConnectConfig } from "ssh2";
import { WebSocketServer } from "ws";
import { setupLocalServerSSHKey } from "./utils";
import { getDockerHost } from "../utils/docker";

const COMMAND_TO_ALLOW_LOCAL_ACCESS = `
# ----------------------------------------
mkdir -p $HOME/.ssh && \\
chmod 700 $HOME/.ssh && \\
touch $HOME/.ssh/authorized_keys && \\
chmod 600 $HOME/.ssh/authorized_keys && \\
cat /etc/dokploy/ssh/auto_generated-dokploy-local.pub >> $HOME/.ssh/authorized_keys && \\
echo "✓ Dokploy SSH key added successfully. Reopen the terminal in Dokploy to reconnect."
# ----------------------------------------`;

export const getPublicIpWithFallback = async () => {
// @ts-ignore
Expand Down Expand Up @@ -73,20 +84,31 @@ export const setupTerminalWebSocketServer = (
return;
}

ws.send("Setting up private SSH key...\n");
const privateKey = await setupLocalServerSSHKey();
try {
ws.send("Setting up private SSH key...\n");
const privateKey = await setupLocalServerSSHKey();

if (!privateKey) {
if (!privateKey) {
ws.close();
return;
}

const dockerHost = await getDockerHost();

ws.send(`Found Docker host: ${dockerHost}\n`);

connectionDetails = {
host: dockerHost,
port,
username,
privateKey,
};
} catch (error) {
console.error(`Error setting up private SSH key: ${error}`);
ws.send(`Error setting up private SSH key: ${error}`);
ws.close();
return;
}

connectionDetails = {
host: "localhost",
port,
username,
privateKey,
};
} else {
const server = await findServerById(serverId);

Expand Down Expand Up @@ -161,9 +183,15 @@ export const setupTerminalWebSocketServer = (
})
.on("error", (err) => {
if (err.level === "client-authentication") {
ws.send(
`Authentication failed: Unauthorized ${isLocalServer ? "" : "private SSH key or "}username.\n❌ Error: ${err.message} ${err.level}`,
);
if (isLocalServer) {
ws.send(
`Authentication failed: Please run the command below on your server to allow access. Make sure to run it as the same user as the one configured in connection settings:${COMMAND_TO_ALLOW_LOCAL_ACCESS}\nAfter running the command, reopen this window to reconnect. This procedure is required only once.`,
);
} else {
ws.send(
`Authentication failed: Unauthorized private SSH key or username.\n❌ Error: ${err.message} ${err.level}`,
);
}
} else {
ws.send(`SSH connection error: ${err.message} ❌ `);
}
Expand Down
58 changes: 17 additions & 41 deletions apps/dokploy/server/wss/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { execAsync } from "@dokploy/server";
import { execAsync, paths } from "@dokploy/server";
import os from "node:os";
import path from "node:path";
import fs from "node:fs";

const HOME_PATH = process.env.HOME || process.env.USERPROFILE || "/";

const LOCAL_SSH_KEY_PATH = path.join(
HOME_PATH,
".ssh",
"auto_generated-dokploy-local",
);

const AUTHORIZED_KEYS_PATH = path.join(HOME_PATH, ".ssh", "authorized_keys");

export const getShell = () => {
switch (os.platform()) {
case "win32":
Expand All @@ -24,39 +14,25 @@ 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. */
export const setupLocalServerSSHKey = async () => {
try {
if (!fs.existsSync(LOCAL_SSH_KEY_PATH)) {
// Generate new SSH key if it hasn't been created yet
await execAsync(
`ssh-keygen -t rsa -b 4096 -f ${LOCAL_SSH_KEY_PATH} -N ""`,
);
}
/** Returns private SSH key for dokploy local server terminal. Uses already created SSH key or generates a new SSH key.
*
* In case of permission failures when running locally, run the command below:
const privateKey = fs.readFileSync(LOCAL_SSH_KEY_PATH, "utf8");
const publicKey = fs.readFileSync(`${LOCAL_SSH_KEY_PATH}.pub`, "utf8");
const authKeyContent = `${publicKey}\n`;
```
sudo chown -R $USER:$USER /etc/dokploy/ssh
```
if (!fs.existsSync(AUTHORIZED_KEYS_PATH)) {
// Create authorized_keys if it doesn't exist yet
fs.writeFileSync(AUTHORIZED_KEYS_PATH, authKeyContent, { mode: 0o600 });
return privateKey;
}
*/
export const setupLocalServerSSHKey = async () => {
const { SSH_PATH } = paths(true);
const sshKeyPath = path.join(SSH_PATH, "auto_generated-dokploy-local");

const existingAuthKeys = fs.readFileSync(AUTHORIZED_KEYS_PATH, "utf8");
if (existingAuthKeys.includes(publicKey)) {
return privateKey;
}
if (!fs.existsSync(sshKeyPath)) {
// Generate new SSH key if it hasn't been created yet
await execAsync(`ssh-keygen -t rsa -b 4096 -f ${sshKeyPath} -N ""`);
}

// Append the public key to authorized_keys
fs.appendFileSync(AUTHORIZED_KEYS_PATH, authKeyContent, {
mode: 0o600,
});
const privateKey = fs.readFileSync(sshKeyPath, "utf8");

return privateKey;
} catch (error) {
console.error("Error getting private SSH key for local terminal:", error);
return "";
}
return privateKey;
};

0 comments on commit d0a5427

Please sign in to comment.