This commit is contained in:
hailin 2025-06-15 19:56:34 +08:00
parent ec9360a4bb
commit 5cebd1239d
3 changed files with 145 additions and 14 deletions

View File

@ -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 || "加载中...");

View File

@ -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' },

View File

@ -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}`;