Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP. Co-authored-by: Cursor <cursoragent@cursor.com>
111 lines
3.4 KiB
TypeScript
111 lines
3.4 KiB
TypeScript
import { NextResponse } from "next/server";
|
||
import type { NextRequest } from "next/server";
|
||
|
||
const PUBLIC = ["/login", "/privacy", "/terms"];
|
||
const PUBLIC_ASSETS = [
|
||
"/manifest.webmanifest",
|
||
"/sw.js",
|
||
"/favicon.ico",
|
||
"/icon-192.png",
|
||
"/icon-512.png",
|
||
"/apple-touch-icon.png",
|
||
];
|
||
|
||
/** True for loopback / RFC1918 — edge proxy may reach Next as 10.x or 127.0.0.1. */
|
||
function hostIsPrivate(host: string): boolean {
|
||
const h = host.split(",")[0]?.trim().split(":")[0]?.toLowerCase() || "";
|
||
if (!h || h === "localhost" || h.endsWith(".local")) return true;
|
||
if (h === "127.0.0.1" || h.startsWith("127.")) return true;
|
||
if (h.startsWith("10.") || h.startsWith("192.168.")) return true;
|
||
// 172.16.0.0 – 172.31.255.255
|
||
const m = /^172\.(\d+)\./.exec(h);
|
||
if (m) {
|
||
const n = Number(m[1]);
|
||
if (n >= 16 && n <= 31) return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Public site origin for login redirects behind Caddy/Nginx.
|
||
* Prefer the request Host (dy.nomadro.cn vs .com) so dual-edge stays on the same domain.
|
||
* Fall back to DY_PUBLIC_BASE_URL only when Host is private/loopback.
|
||
*/
|
||
function publicOrigin(request: NextRequest): string {
|
||
const xfProto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim() || "https";
|
||
const xfHost = request.headers.get("x-forwarded-host")?.split(",")[0]?.trim() || "";
|
||
const host = (xfHost || request.headers.get("host") || "").trim();
|
||
if (host && !hostIsPrivate(host)) {
|
||
let h = host;
|
||
if (xfProto === "https" && h.endsWith(":443")) h = h.slice(0, -4);
|
||
if (xfProto === "http" && h.endsWith(":80")) h = h.slice(0, -3);
|
||
return `${xfProto}://${h}`;
|
||
}
|
||
|
||
const fromEnv = String(process.env.DY_PUBLIC_BASE_URL || process.env.NEXT_PUBLIC_SITE_URL || "").trim();
|
||
if (fromEnv) return fromEnv.replace(/\/$/, "");
|
||
|
||
return request.nextUrl.origin;
|
||
}
|
||
|
||
export function middleware(request: NextRequest) {
|
||
const { pathname } = request.nextUrl;
|
||
// ShellCrash / MetaCubeX dashboard + Clash REST — must bypass login (Caddy may fall through).
|
||
const clashPass =
|
||
pathname.startsWith("/_next") ||
|
||
pathname.startsWith("/favicon") ||
|
||
pathname.startsWith("/shellcrash-ui") ||
|
||
pathname === "/ui" ||
|
||
pathname.startsWith("/ui/") ||
|
||
[
|
||
"/version",
|
||
"/proxies",
|
||
"/configs",
|
||
"/rules",
|
||
"/connections",
|
||
"/providers",
|
||
"/traffic",
|
||
"/logs",
|
||
"/memory",
|
||
"/cache",
|
||
"/dns",
|
||
"/debug",
|
||
"/restart",
|
||
"/upgrade",
|
||
"/group",
|
||
"/script",
|
||
"/fakeip",
|
||
"/endpoint",
|
||
"/runtime",
|
||
"/meta",
|
||
"/schema",
|
||
"/flushfakeip",
|
||
"/externaloverride",
|
||
"/storage",
|
||
].some((p) => pathname === p || pathname.startsWith(`${p}/`)) ||
|
||
PUBLIC_ASSETS.includes(pathname) ||
|
||
pathname.match(/\.(ico|png|jpg|jpeg|svg|webp|css|js|webmanifest|map)$/);
|
||
if (clashPass) {
|
||
return NextResponse.next();
|
||
}
|
||
|
||
const session = request.cookies.get("dy_session")?.value;
|
||
const isLogin = PUBLIC.some((p) => pathname === p || pathname.startsWith(`${p}/`));
|
||
const origin = publicOrigin(request);
|
||
|
||
if (!session && !isLogin) {
|
||
const loginUrl = new URL("/login", origin);
|
||
loginUrl.searchParams.set("next", pathname);
|
||
return NextResponse.redirect(loginUrl);
|
||
}
|
||
|
||
if (session && isLogin) {
|
||
return NextResponse.redirect(new URL("/", origin));
|
||
}
|
||
|
||
return NextResponse.next();
|
||
}
|
||
export const config = {
|
||
matcher: ["/((?!_next/static|_next/image).*)"],
|
||
};
|