From 54f98387e50747e43da19f56422364a2ff810fe1 Mon Sep 17 00:00:00 2001 From: wusumac <736139669@qq.com> Date: Sun, 2 Aug 2026 23:50:34 +0800 Subject: [PATCH] feat(web,hub): in-place connection switching via setServerUrl + CORS allowlist for lanUrl --- hub/src/config/serverSettings.ts | 13 +++++++++++++ hub/src/configuration.ts | 5 +++++ hub/src/web/server.ts | 11 ++++++++++- web/src/App.tsx | 2 +- web/src/lib/app-context.tsx | 3 +++ web/src/routes/settings/general.tsx | 8 ++++++-- 6 files changed, 38 insertions(+), 4 deletions(-) diff --git a/hub/src/config/serverSettings.ts b/hub/src/config/serverSettings.ts index 2057d8a0..6b1b76d3 100644 --- a/hub/src/config/serverSettings.ts +++ b/hub/src/config/serverSettings.ts @@ -21,6 +21,7 @@ export interface ServerSettings { listenPort: number publicUrl: string corsOrigins: string[] + lanUrl: string | null } export interface ServerSettingsResult { @@ -33,6 +34,7 @@ export interface ServerSettingsResult { listenHost: 'env' | 'file' | 'default' listenPort: 'env' | 'file' | 'default' publicUrl: 'env' | 'file' | 'default' + lanUrl: 'file' | 'default' corsOrigins: 'env' | 'file' | 'default' } savedToFile: boolean @@ -110,6 +112,7 @@ export async function loadServerSettings(dataDir: string): Promise file > null @@ -214,6 +217,15 @@ export async function loadServerSettings(dataDir: string): Promise file > derived from publicUrl let corsOrigins: string[] if (process.env.CORS_ORIGINS) { @@ -244,6 +256,7 @@ export async function loadServerSettings(dataDir: string): Promise { + try { + return [...new Set([...corsOrigins, new URL(configuration.lanUrl).origin])] + } catch { + return corsOrigins + } + })() + : corsOrigins + const corsOriginOption = mergedCorsOrigins.includes('*') ? '*' : mergedCorsOrigins const corsMiddleware = cors({ origin: corsOriginOption, allowMethods: ['GET', 'POST', 'PATCH', 'DELETE', 'OPTIONS'], diff --git a/web/src/App.tsx b/web/src/App.tsx index 2bccf74b..3e40a635 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -449,7 +449,7 @@ function AppInner() { } return ( - + ServerUrlResult } const AppContext = createContext(null) diff --git a/web/src/routes/settings/general.tsx b/web/src/routes/settings/general.tsx index f14f5591..21c77939 100644 --- a/web/src/routes/settings/general.tsx +++ b/web/src/routes/settings/general.tsx @@ -11,7 +11,7 @@ const locales: ReadonlyArray<{ value: Locale; label: string }> = [ function ConnectionSwitch() { const { t } = useTranslation() - const { api } = useAppContext() + const { api, setServerUrl } = useAppContext() const [info, setInfo] = useState<{ publicUrl: string | null; lanUrl: string | null } | null>(null) useEffect(() => { @@ -68,7 +68,11 @@ function ConnectionSwitch() {