@@ -3,7 +3,9 @@ import { promisify } from "node:util";
33import { db } from "@dokploy/server/db" ;
44import { getWebServerSettings } from "@dokploy/server/services/web-server-settings" ;
55import { generateRandomDomain } from "@dokploy/server/templates" ;
6+ import { execAsyncRemote } from "@dokploy/server/utils/process/execAsync" ;
67import { manageDomain } from "@dokploy/server/utils/traefik/domain" ;
8+ import { getPublicIpWithFallback } from "@dokploy/server/wss/utils" ;
79import { TRPCError } from "@trpc/server" ;
810import { eq } from "drizzle-orm" ;
911import type { z } from "zod" ;
@@ -154,7 +156,7 @@ const resolveDns = promisify(dns.resolve4);
154156
155157export 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