|
|
@@ -47,92 +47,6 @@ function resolveApiBaseUrlValue(raw: string | undefined): string {
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
-/** PM2 / server.js 常用对外误显端口;登录跳转一律不写进地址栏(本机回环除外)。 */
|
|
|
-const DEFAULT_NODE_PUBLIC_PORT = "4000";
|
|
|
-
|
|
|
-function isLoopbackHostname(hostname: string): boolean {
|
|
|
- return (
|
|
|
- hostname === "localhost" ||
|
|
|
- hostname === "127.0.0.1" ||
|
|
|
- hostname === "[::1]"
|
|
|
- );
|
|
|
-}
|
|
|
-
|
|
|
-/** 本地 / 局域网访问时保留当前 origin(含端口),避免误跳到 :80/:443 上无服务。 */
|
|
|
-function shouldPreserveCurrentOriginPort(hostname: string): boolean {
|
|
|
- if (
|
|
|
- hostname === "localhost" ||
|
|
|
- hostname === "127.0.0.1" ||
|
|
|
- hostname === "[::1]"
|
|
|
- ) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- const parts = hostname.split(".");
|
|
|
- if (
|
|
|
- parts.length === 4 &&
|
|
|
- parts.every((p) => /^\d+$/.test(p)) &&
|
|
|
- parts[0] !== undefined &&
|
|
|
- parts[1] !== undefined
|
|
|
- ) {
|
|
|
- const a = Number(parts[0]);
|
|
|
- const b = Number(parts[1]);
|
|
|
- if (a === 10) return true;
|
|
|
- if (a === 172 && b >= 16 && b <= 31) return true;
|
|
|
- if (a === 192 && b === 168) return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * 登录页跳转地址(仅浏览器)。
|
|
|
- * - 当前端口为 **4000** 且非本机回环时,强制用「协议 + 主机名(无端口)」拼 `/auth/login`,去掉地址栏里的 `:4000`。
|
|
|
- * - 若已配置 `NEXT_PUBLIC_SITE_URL` 且当前 host 与站点一致,优先用该 canonical origin。
|
|
|
- * - localhost / 局域网 IP 且非上述 4000 规则:仍用相对路径,保留端口。
|
|
|
- */
|
|
|
-export function resolveAuthLoginHref(): string {
|
|
|
- const path = "/auth/login";
|
|
|
- if (typeof window === "undefined") return path;
|
|
|
-
|
|
|
- const { protocol, hostname, port } = window.location;
|
|
|
-
|
|
|
- if (port === DEFAULT_NODE_PUBLIC_PORT && !isLoopbackHostname(hostname)) {
|
|
|
- try {
|
|
|
- return new URL(path, `${protocol}//${hostname}/`).href;
|
|
|
- } catch {
|
|
|
- /* fall through */
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- const site = process.env.NEXT_PUBLIC_SITE_URL?.trim().replace(/\/$/, "");
|
|
|
- if (site) {
|
|
|
- try {
|
|
|
- const siteUrl = new URL(site);
|
|
|
- const canonical = siteUrl.hostname;
|
|
|
- const allowedHosts = new Set([canonical, `www.${canonical}`]);
|
|
|
- try {
|
|
|
- const current = new URL(window.location.href);
|
|
|
- if (allowedHosts.has(current.hostname)) {
|
|
|
- return `${siteUrl.origin}${path}`;
|
|
|
- }
|
|
|
- } catch {
|
|
|
- /* fall through */
|
|
|
- }
|
|
|
- } catch {
|
|
|
- /* invalid NEXT_PUBLIC_SITE_URL */
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (shouldPreserveCurrentOriginPort(hostname)) {
|
|
|
- return path;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- return new URL(path, `${protocol}//${hostname}/`).href;
|
|
|
- } catch {
|
|
|
- return path;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
export function resolveAppEnv(): AppEnv {
|
|
|
const fromEnv = process.env.NEXT_PUBLIC_APP_ENV;
|
|
|
if (fromEnv === "local" || fromEnv === "test" || fromEnv === "production") {
|