Skip to content

Commit 9761616

Browse files
committed
fix: domain validation false negative on servers with multiple IPs
Validate against the server's SSH IP plus its detected public egress IP, instead of only the stored SSH address. Fixes #4658
1 parent bfc180d commit 9761616

3 files changed

Lines changed: 57 additions & 10 deletions

File tree

‎apps/dokploy/components/dashboard/application/domains/show-domains.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ export const ShowDomains = ({ id, type }: Props) => {
191191
try {
192192
const result = await validateDomain({
193193
domain: host,
194-
serverIp:
195-
application?.server?.ipAddress?.toString() || ip?.toString() || "",
194+
serverId: application?.serverId ?? undefined,
196195
});
197196

198197
setValidationStates((prev) => ({

‎apps/dokploy/server/api/routers/domain.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
findPreviewDeploymentById,
88
findServerById,
99
generateTraefikMeDomain,
10+
getServerIpCandidates,
1011
getWebServerSettings,
1112
manageDomain,
1213
removeDomain,
@@ -248,10 +249,11 @@ export const domainRouter = createTRPCRouter({
248249
.input(
249250
z.object({
250251
domain: z.string(),
251-
serverIp: z.string().optional(),
252+
serverId: z.string().optional(),
252253
}),
253254
)
254255
.mutation(async ({ input }) => {
255-
return validateDomain(input.domain, input.serverIp);
256+
const expectedIps = await getServerIpCandidates(input.serverId);
257+
return validateDomain(input.domain, expectedIps);
256258
}),
257259
});

‎packages/server/src/services/domain.ts‎

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { promisify } from "node:util";
33
import { db } from "@dokploy/server/db";
44
import { getWebServerSettings } from "@dokploy/server/services/web-server-settings";
55
import { generateRandomDomain } from "@dokploy/server/templates";
6+
import { execAsyncRemote } from "@dokploy/server/utils/process/execAsync";
67
import { manageDomain } from "@dokploy/server/utils/traefik/domain";
8+
import { getPublicIpWithFallback } from "@dokploy/server/wss/utils";
79
import { TRPCError } from "@trpc/server";
810
import { eq } from "drizzle-orm";
911
import type { z } from "zod";
@@ -154,7 +156,7 @@ const resolveDns = promisify(dns.resolve4);
154156

155157
export const validateDomain = async (
156158
domain: string,
157-
expectedIp?: string,
159+
expectedIps?: string[],
158160
): Promise<{
159161
isValid: boolean;
160162
resolvedIp?: string;
@@ -186,13 +188,13 @@ export const validateDomain = async (
186188
};
187189
}
188190

189-
// If we have an expected IP, validate against it
190-
if (expectedIp) {
191+
if (expectedIps && expectedIps.length > 0) {
192+
const isValid = resolvedIps.some((ip) => expectedIps.includes(ip));
191193
return {
192-
isValid: resolvedIps.includes(expectedIp),
194+
isValid,
193195
resolvedIp: resolvedIps.join(", "),
194-
error: !resolvedIps.includes(expectedIp)
195-
? `Domain resolves to ${resolvedIps.join(", ")} but should point to ${expectedIp}`
196+
error: !isValid
197+
? `Domain resolves to ${resolvedIps.join(", ")} but should point to ${expectedIps.join(" or ")}`
196198
: undefined,
197199
};
198200
}
@@ -210,3 +212,47 @@ export const validateDomain = async (
210212
};
211213
}
212214
};
215+
216+
export const getServerIpCandidates = async (
217+
serverId?: string | null,
218+
): Promise<string[]> => {
219+
const candidates = new Set<string>();
220+
221+
if (serverId) {
222+
const server = await findServerById(serverId);
223+
if (server.ipAddress) {
224+
candidates.add(server.ipAddress);
225+
}
226+
227+
const publicIp = await withTimeout(
228+
execAsyncRemote(
229+
serverId,
230+
"curl -s -m 5 https://ifconfig.me || curl -s -m 5 https://icanhazip.com",
231+
),
232+
7000,
233+
);
234+
const detectedIp = publicIp?.stdout?.trim();
235+
if (detectedIp) {
236+
candidates.add(detectedIp);
237+
}
238+
} else {
239+
const settings = await getWebServerSettings();
240+
if (settings?.serverIp) {
241+
candidates.add(settings.serverIp);
242+
}
243+
244+
const publicIp = await withTimeout(getPublicIpWithFallback(), 7000);
245+
if (publicIp) {
246+
candidates.add(publicIp);
247+
}
248+
}
249+
250+
return Array.from(candidates);
251+
};
252+
253+
const withTimeout = <T>(promise: Promise<T>, ms: number): Promise<T | null> => {
254+
return Promise.race([
255+
promise,
256+
new Promise<null>((resolve) => setTimeout(() => resolve(null), ms)),
257+
]).catch(() => null);
258+
};

0 commit comments

Comments
 (0)