From ca243d72592a47d4f776c5d302a6bec7dec4a6c8 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sat, 20 Sep 2025 23:57:38 -0600 Subject: [PATCH] refactor: replace getPublicIpWithFallback with getLocalServerIp for improved local IP retrieval --- apps/dokploy/server/api/routers/cluster.ts | 6 +++--- apps/dokploy/server/wss/terminal.ts | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/apps/dokploy/server/api/routers/cluster.ts b/apps/dokploy/server/api/routers/cluster.ts index 7dde96df5..6c118d802 100644 --- a/apps/dokploy/server/api/routers/cluster.ts +++ b/apps/dokploy/server/api/routers/cluster.ts @@ -7,7 +7,7 @@ import { } from "@dokploy/server"; import { TRPCError } from "@trpc/server"; import { z } from "zod"; -import { getPublicIpWithFallback } from "@/server/wss/terminal"; +import { getLocalServerIp } from "@/server/wss/terminal"; import { createTRPCRouter, protectedProcedure } from "../trpc"; export const clusterRouter = createTRPCRouter({ getNodes: protectedProcedure @@ -61,7 +61,7 @@ export const clusterRouter = createTRPCRouter({ const result = await docker.swarmInspect(); const docker_version = await docker.version(); - let ip = await getPublicIpWithFallback(); + let ip = await getLocalServerIp(); if (input.serverId) { const server = await findServerById(input.serverId); ip = server?.ipAddress; @@ -85,7 +85,7 @@ export const clusterRouter = createTRPCRouter({ const result = await docker.swarmInspect(); const docker_version = await docker.version(); - let ip = await getPublicIpWithFallback(); + let ip = await getLocalServerIp(); if (input.serverId) { const server = await findServerById(input.serverId); ip = server?.ipAddress; diff --git a/apps/dokploy/server/wss/terminal.ts b/apps/dokploy/server/wss/terminal.ts index c24be3122..fa37d492b 100644 --- a/apps/dokploy/server/wss/terminal.ts +++ b/apps/dokploy/server/wss/terminal.ts @@ -1,5 +1,10 @@ import type http from "node:http"; -import { findServerById, IS_CLOUD, validateRequest } from "@dokploy/server"; +import { + execAsync, + findServerById, + IS_CLOUD, + validateRequest, +} from "@dokploy/server"; import { publicIpv4, publicIpv6 } from "public-ip"; import { Client, type ConnectConfig } from "ssh2"; import { WebSocketServer } from "ws"; @@ -44,6 +49,21 @@ export const getPublicIpWithFallback = async () => { return ip; }; +export const getLocalServerIp = async () => { + try { + const command = `ip addr show | grep -E "inet (192\.168\.|10\.|172\.1[6-9]\.|172\.2[0-9]\.|172\.3[0-1]\.)" | head -n1 | awk '{print $2}' | cut -d/ -f1`; + const { stdout } = await execAsync(command); + const ip = stdout.trim(); + return ( + ip || + "We were unable to obtain the local server IP, please use your private IP address" + ); + } catch (error) { + console.error("Error to obtain local server IP", error); + return "We were unable to obtain the local server IP, please use your private IP address"; + } +}; + export const setupTerminalWebSocketServer = ( server: http.Server, ) => {