This commit is contained in:
parent
5cebd1239d
commit
53e2804240
|
|
@ -43,7 +43,7 @@ import { Trash2 } from "lucide-react";
|
||||||
|
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
import { getRuntimeEnv } from "@/lib/ipconfig";
|
// import { getRuntimeEnv } from "@/lib/ipconfig";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
BadgeInfo,
|
BadgeInfo,
|
||||||
|
|
@ -138,8 +138,43 @@ export function Header() {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// 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";
|
||||||
|
// 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`;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
import { getRuntimeEnv } from "@/lib/ipconfig";
|
import { getRuntimeEnv } from "@/lib/ipconfig";
|
||||||
import { headers } from "next/headers";
|
|
||||||
|
|
||||||
export async function getWsBase() {
|
export async function getWsBase() {
|
||||||
let ip = await getRuntimeEnv("SUPABASE_URL");
|
let ip = await getRuntimeEnv("SUPABASE_URL");
|
||||||
|
|
@ -152,11 +187,14 @@ export async function getWsBase() {
|
||||||
window.location &&
|
window.location &&
|
||||||
window.location.protocol === "https:"
|
window.location.protocol === "https:"
|
||||||
) {
|
) {
|
||||||
|
// ✅ 浏览器环境下
|
||||||
wsProtocol = "wss";
|
wsProtocol = "wss";
|
||||||
ip = window.location.hostname; // ✅ 浏览器下安全替换为域名
|
ip = window.location.hostname;
|
||||||
} else {
|
} else {
|
||||||
// ✅ SSR 场景
|
// ✅ 服务端环境,延迟导入 headers
|
||||||
|
const { headers } = await import("next/headers");
|
||||||
const hdrs = headers();
|
const hdrs = headers();
|
||||||
|
|
||||||
const forwardedProto = hdrs.get("x-forwarded-proto");
|
const forwardedProto = hdrs.get("x-forwarded-proto");
|
||||||
const hostHeader = hdrs.get("host");
|
const hostHeader = hdrs.get("host");
|
||||||
|
|
||||||
|
|
@ -169,7 +207,6 @@ export async function getWsBase() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 拼接最终 ws 地址
|
|
||||||
return `${wsProtocol}://${ip}/api/v1/deploy/ws`;
|
return `${wsProtocol}://${ip}/api/v1/deploy/ws`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,43 +44,61 @@
|
||||||
|
|
||||||
|
|
||||||
import { getRuntimeEnv } from "@/lib/ipconfig";
|
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") {
|
||||||
|
// // ✅ 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}`;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
export async function getBaseUrl() {
|
export async function getBaseUrl() {
|
||||||
let ip = await getRuntimeEnv("SUPABASE_URL");
|
let ip = await getRuntimeEnv("SUPABASE_URL");
|
||||||
if (!ip) throw new Error("SUPABASE_URL 获取失败,无法构建 baseUrl");
|
if (!ip) throw new Error("SUPABASE_URL 获取失败");
|
||||||
|
|
||||||
let protocol = "http";
|
if (typeof window === "undefined") {
|
||||||
let port = 80;
|
// ✅ 只在服务端导入(避免构建失败)
|
||||||
|
const { getServerBaseUrl } = await import("./getServerBaseUrl");
|
||||||
if (typeof window !== "undefined") {
|
return getServerBaseUrl(ip);
|
||||||
// ✅ 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}`;
|
// ✅ 客户端逻辑
|
||||||
|
let protocol = window.location.protocol.replace(":", "");
|
||||||
|
let hostname = window.location.hostname;
|
||||||
|
let port = protocol === "https" ? 443 : 80;
|
||||||
|
return `${protocol}://${hostname}:${port}`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
// lib/http/getServerBaseUrl.ts
|
||||||
|
import { headers } from "next/headers";
|
||||||
|
|
||||||
|
export function getServerBaseUrl(defaultIp: string) {
|
||||||
|
const h = headers();
|
||||||
|
const proto = h.get("x-forwarded-proto") || "http";
|
||||||
|
const host = h.get("host") || defaultIp;
|
||||||
|
const port = proto === "https" ? 443 : 80;
|
||||||
|
|
||||||
|
return `${proto}://${host}:${port}`;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue