diff --git a/apps/blogai/components/header.tsx b/apps/blogai/components/header.tsx index 5f6fd4c..b803524 100644 --- a/apps/blogai/components/header.tsx +++ b/apps/blogai/components/header.tsx @@ -117,26 +117,63 @@ export function Header() { ) } +// export async function getWsBase() { +// let ip = await getRuntimeEnv("SUPABASE_URL"); +// if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 wsBase"); + +// // 判断协议 +// let wsProtocol = "ws"; +// if ( +// typeof window !== "undefined" && +// window.location && +// window.location.protocol === "https:" +// ) { +// wsProtocol = "wss"; +// // ✅ HTTPS + 浏览器环境下用 hostname 避免 TLS 报错 +// ip = window.location.hostname; +// } + +// // 拼接最终 ws 地址 +// return `${wsProtocol}://${ip}/api/v1/deploy/ws`; +// } + + +import { getRuntimeEnv } from "@/lib/ipconfig"; +import { headers } from "next/headers"; + export async function getWsBase() { let ip = await getRuntimeEnv("SUPABASE_URL"); if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 wsBase"); - // 判断协议 let wsProtocol = "ws"; + if ( typeof window !== "undefined" && window.location && window.location.protocol === "https:" ) { wsProtocol = "wss"; - // ✅ HTTPS + 浏览器环境下用 hostname 避免 TLS 报错 - ip = window.location.hostname; + ip = window.location.hostname; // ✅ 浏览器下安全替换为域名 + } else { + // ✅ SSR 场景 + const hdrs = headers(); + const forwardedProto = hdrs.get("x-forwarded-proto"); + const hostHeader = hdrs.get("host"); + + if (forwardedProto === "https") { + wsProtocol = "wss"; + } + + if (hostHeader) { + ip = hostHeader.includes(":") ? hostHeader.split(":")[0] : hostHeader; + } } // 拼接最终 ws 地址 return `${wsProtocol}://${ip}/api/v1/deploy/ws`; } + export function DetailPageHeader({ data }: { data: any }) { const [loading, setLoading] = useState(false); const [statusText, setStatusText] = useState(data?.statusText || "加载中..."); diff --git a/apps/blogai/lib/http/axios_config.ts b/apps/blogai/lib/http/axios_config.ts index 57acb75..e51d64d 100644 --- a/apps/blogai/lib/http/axios_config.ts +++ b/apps/blogai/lib/http/axios_config.ts @@ -31,27 +31,77 @@ // } as any; +// import { getRuntimeEnv } from "../ipconfig"; + +// export async function getAxiosConfig() { +// let ip = await getRuntimeEnv("SUPABASE_URL"); // 直接用你 lib/ipconfig 里的方法 +// if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 axios 配置"); + +// let protocol = "http"; +// let port = 80; + +// if (typeof window !== "undefined" && window.location && window.location.protocol) { +// protocol = window.location.protocol.replace(":", ""); +// port = protocol === "https" ? 443 : 80; + +// // ✅ HTTPS + 浏览器时,替换 IP,避免 TLS/CORS 报错 +// if (protocol === "https") { +// ip = window.location.hostname; +// } +// } + +// return { +// baseURL: `${protocol}://${ip}:${port}`, // 端口如需动态可再加参数 +// method: 'post', +// timeout: 60 * 1000, +// headers: { 'Content-Type': 'application/json; charset=UTF-8' }, +// responseType: 'json' +// } as const; +// } + + import { getRuntimeEnv } from "../ipconfig"; +import { headers } from "next/headers"; export async function getAxiosConfig() { - let ip = await getRuntimeEnv("SUPABASE_URL"); // 直接用你 lib/ipconfig 里的方法 + let ip = await getRuntimeEnv("SUPABASE_URL"); if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 axios 配置"); let protocol = "http"; let port = 80; if (typeof window !== "undefined" && window.location && window.location.protocol) { + // ✅ CSR 场景 protocol = window.location.protocol.replace(":", ""); port = protocol === "https" ? 443 : 80; - // ✅ HTTPS + 浏览器时,替换 IP,避免 TLS/CORS 报错 if (protocol === "https") { ip = window.location.hostname; } + } else { + // ✅ SSR 场景,自动读取 headers + const hdrs = headers(); + const forwardedProto = hdrs.get("x-forwarded-proto"); + const hostHeader = hdrs.get("host"); + + if (forwardedProto) { + protocol = forwardedProto; + } + + if (hostHeader) { + ip = hostHeader; + if (hostHeader.includes(":")) { + const parts = hostHeader.split(":"); + ip = parts[0]; + port = parseInt(parts[1]); + } else { + port = (protocol === "https") ? 443 : 80; + } + } } return { - baseURL: `${protocol}://${ip}:${port}`, // 端口如需动态可再加参数 + baseURL: `${protocol}://${ip}:${port}`, method: 'post', timeout: 60 * 1000, headers: { 'Content-Type': 'application/json; charset=UTF-8' }, diff --git a/apps/blogai/lib/http/get-base-url.ts b/apps/blogai/lib/http/get-base-url.ts index 4bf302d..5566095 100644 --- a/apps/blogai/lib/http/get-base-url.ts +++ b/apps/blogai/lib/http/get-base-url.ts @@ -19,7 +19,32 @@ +// import { getRuntimeEnv } from "@/lib/ipconfig"; +// import { headers } from 'next/headers' + +// export async function getBaseUrl() { +// let ip = await getRuntimeEnv("SUPABASE_URL"); +// if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 baseUrl"); + +// let protocol = "http"; +// let port = 80; + +// if ( +// typeof window !== "undefined" && +// window.location && +// window.location.protocol === "https:" +// ) { +// protocol = "https"; +// ip = window.location.hostname; +// port = 443; +// } + +// return `${protocol}://${ip}:${port}`; +// } + + import { getRuntimeEnv } from "@/lib/ipconfig"; +import { headers } from 'next/headers'; export async function getBaseUrl() { let ip = await getRuntimeEnv("SUPABASE_URL"); @@ -28,14 +53,33 @@ export async function getBaseUrl() { let protocol = "http"; let port = 80; - if ( - typeof window !== "undefined" && - window.location && - window.location.protocol === "https:" - ) { - protocol = "https"; - ip = window.location.hostname; - port = 443; + if (typeof window !== "undefined") { + // ✅ CSR 模式 + if (window.location.protocol === "https:") { + protocol = "https"; + ip = window.location.hostname; + port = 443; + } + } else { + // ✅ SSR 模式,补充逻辑 + const hdrs = headers(); + const forwardedProto = hdrs.get("x-forwarded-proto"); + const hostHeader = hdrs.get("host"); + + if (forwardedProto) { + protocol = forwardedProto; + } + + if (hostHeader) { + ip = hostHeader; + if (hostHeader.includes(":")) { + const parts = hostHeader.split(":"); + ip = parts[0]; + port = parseInt(parts[1]); + } else { + port = (protocol === "https") ? 443 : 80; + } + } } return `${protocol}://${ip}:${port}`;