Skip to content

Commit 68a0555

Browse files
committed
fix: write remote traefik config via SFTP instead of exec+base64
Embedding the full YAML (base64-encoded) into a single SSH exec command silently fails once the payload passes the SSH transport's packet size ceiling (~90-100KB), truncating the dynamic config with no error surfaced anywhere. Switch to SFTP, which has no such limit.
1 parent 48e0b74 commit 68a0555

2 files changed

Lines changed: 81 additions & 21 deletions

File tree

‎packages/server/src/utils/process/execAsync.ts‎

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import { findServerById } from "@dokploy/server/services/server";
44
import { Client } from "ssh2";
55
import { ExecError } from "./ExecError";
66

7+
export class WriteFileRemoteError extends Error {
8+
constructor(
9+
message: string,
10+
public readonly context: {
11+
remotePath: string;
12+
serverId: string;
13+
originalError: Error;
14+
},
15+
) {
16+
super(message);
17+
this.name = "WriteFileRemoteError";
18+
}
19+
}
20+
721
// Re-export ExecError for easier imports
822
export { ExecError } from "./ExecError";
923

@@ -21,11 +35,11 @@ export const execAsync = async (
2135
};
2236
} catch (error) {
2337
if (error instanceof Error) {
24-
// @ts-ignore - exec error has these properties
38+
// @ts-expect-error - exec error has these properties
2539
const exitCode = error.code;
26-
// @ts-ignore
40+
// @ts-expect-error
2741
const stdout = error.stdout?.toString() || "";
28-
// @ts-ignore
42+
// @ts-expect-error
2943
const stderr = error.stderr?.toString() || "";
3044

3145
throw new ExecError(`Command execution failed: ${error.message}`, {
@@ -61,7 +75,7 @@ export const execAsyncStream = (
6175
command,
6276
stdout: stdoutComplete,
6377
stderr: stderrComplete,
64-
// @ts-ignore
78+
// @ts-expect-error
6579
exitCode: error.code,
6680
originalError: error,
6781
}),
@@ -249,6 +263,65 @@ export const execAsyncRemote = async (
249263
});
250264
};
251265

266+
export const writeFileRemote = async (
267+
serverId: string,
268+
remotePath: string,
269+
content: string,
270+
): Promise<void> => {
271+
const server = await findServerById(serverId);
272+
if (!server.sshKeyId) throw new Error("No SSH key available for this server");
273+
274+
return new Promise((resolve, reject) => {
275+
const conn = new Client();
276+
conn
277+
.once("ready", () => {
278+
conn.sftp((err, sftp) => {
279+
if (err) {
280+
conn.end();
281+
reject(
282+
new WriteFileRemoteError(`SFTP session failed: ${err.message}`, {
283+
remotePath,
284+
serverId,
285+
originalError: err,
286+
}),
287+
);
288+
return;
289+
}
290+
sftp.writeFile(remotePath, content, (writeErr) => {
291+
conn.end();
292+
if (writeErr) {
293+
reject(
294+
new WriteFileRemoteError(
295+
`Failed to write remote file ${remotePath}: ${writeErr.message}`,
296+
{ remotePath, serverId, originalError: writeErr },
297+
),
298+
);
299+
return;
300+
}
301+
resolve();
302+
});
303+
});
304+
})
305+
.on("error", (err) => {
306+
conn.end();
307+
reject(
308+
new WriteFileRemoteError(`SSH connection error: ${err.message}`, {
309+
remotePath,
310+
serverId,
311+
originalError: err,
312+
}),
313+
);
314+
})
315+
.connect({
316+
host: server.ipAddress,
317+
port: server.port,
318+
username: server.username,
319+
privateKey: server.sshKey?.privateKey,
320+
timeout: 99999,
321+
});
322+
});
323+
};
324+
252325
export const sleep = (ms: number) => {
253326
return new Promise((resolve) => setTimeout(resolve, ms));
254327
};

‎packages/server/src/utils/traefik/application.ts‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { paths } from "@dokploy/server/constants";
55
import type { Domain } from "@dokploy/server/services/domain";
66
import { quote } from "shell-quote";
77
import { parse, stringify } from "yaml";
8-
import { encodeBase64 } from "../docker/utils";
9-
import { execAsync, execAsyncRemote } from "../process/execAsync";
8+
import { execAsync, execAsyncRemote, writeFileRemote } from "../process/execAsync";
109
import type { FileConfig, HttpLoadBalancerService } from "./file-types";
1110

1211
export const createTraefikConfig = (appName: string) => {
@@ -228,11 +227,7 @@ export const writeConfigRemote = async (
228227
try {
229228
const { DYNAMIC_TRAEFIK_PATH } = paths(true);
230229
const configPath = path.join(DYNAMIC_TRAEFIK_PATH, `${appName}.yml`);
231-
const encoded = encodeBase64(traefikConfig);
232-
await execAsyncRemote(
233-
serverId,
234-
`echo "${encoded}" | base64 -d > ${quote([configPath])}`,
235-
);
230+
await writeFileRemote(serverId, configPath, traefikConfig);
236231
} catch (e) {
237232
console.error("Error saving the YAML config file:", e);
238233
}
@@ -246,11 +241,7 @@ export const writeTraefikConfigInPath = async (
246241
try {
247242
const configPath = path.join(pathFile);
248243
if (serverId) {
249-
const encoded = encodeBase64(traefikConfig);
250-
await execAsyncRemote(
251-
serverId,
252-
`echo "${encoded}" | base64 -d > ${quote([configPath])}`,
253-
);
244+
await writeFileRemote(serverId, configPath, traefikConfig);
254245
} else {
255246
fs.writeFileSync(configPath, traefikConfig, "utf8");
256247
}
@@ -282,11 +273,7 @@ export const writeTraefikConfigRemote = async (
282273
const { DYNAMIC_TRAEFIK_PATH } = paths(true);
283274
const configPath = path.join(DYNAMIC_TRAEFIK_PATH, `${appName}.yml`);
284275
const yamlStr = stringify(traefikConfig);
285-
const encoded = encodeBase64(yamlStr);
286-
await execAsyncRemote(
287-
serverId,
288-
`echo "${encoded}" | base64 -d > ${quote([configPath])}`,
289-
);
276+
await writeFileRemote(serverId, configPath, yamlStr);
290277
} catch (e) {
291278
console.error("Error saving the YAML config file:", e);
292279
}

0 commit comments

Comments
 (0)