260702_01_ENH: 리스트 작업 도중 공용 ui를 적용하기 위한 중간 커밋
This commit is contained in:
74
src/proxy/catchRedirectPageLanding.ts
Normal file
74
src/proxy/catchRedirectPageLanding.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { NextResponse, type NextFetchEvent, type NextRequest } from "next/server";
|
||||
import { getClientIp } from "@kri/common/server";
|
||||
import { DEFAULT_HTTP_TIMEOUT_MS } from "@kri/common/http";
|
||||
import { ALLOW_REDIRECT } from "@/const/commonConst";
|
||||
|
||||
export const catchRedirectPageLanding = async (req: NextRequest, event: NextFetchEvent) => {
|
||||
void event;
|
||||
|
||||
const pathname = req.nextUrl.pathname;
|
||||
const slug = pathname.split("/")[2] ?? "";
|
||||
const search = req.nextUrl.search;
|
||||
|
||||
if (!ALLOW_REDIRECT.has(slug)) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const h = req.headers;
|
||||
const orig = {
|
||||
remoteAddr: getClientIp(req),
|
||||
uri: pathname,
|
||||
url: req.url,
|
||||
userAgent: h.get("user-agent"),
|
||||
referer: h.get("referer"),
|
||||
origin: h.get("origin"),
|
||||
xForwardedFor: h.get("x-forwarded-for"),
|
||||
forwarded: h.get("forwarded"),
|
||||
};
|
||||
|
||||
const endpoint = new URL(req.url);
|
||||
endpoint.pathname = `/api/external/${encodeURIComponent(slug)}`;
|
||||
endpoint.search = search;
|
||||
|
||||
const controller = new AbortController();
|
||||
const t = setTimeout(() => controller.abort(), DEFAULT_HTTP_TIMEOUT_MS);
|
||||
|
||||
try {
|
||||
const res = await fetch(endpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"x-entry-type": "redirect",
|
||||
"x-orig-uri": orig.uri,
|
||||
"x-orig-url": orig.url,
|
||||
"x-orig-user-agent": orig.userAgent ?? "",
|
||||
"x-orig-referer": orig.referer ?? "",
|
||||
"x-orig-origin": orig.origin ?? "",
|
||||
"x-orig-ip": orig.remoteAddr ?? "",
|
||||
"x-orig-x-forwarded-for": orig.xForwardedFor ?? "",
|
||||
"x-orig-forwarded": orig.forwarded ?? "",
|
||||
"x-internal-log": "1",
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
signal: controller.signal,
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (res.status === 200 || res.status === 204) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
return goToError(req);
|
||||
} catch {
|
||||
return goToError(req);
|
||||
} finally {
|
||||
clearTimeout(t);
|
||||
}
|
||||
};
|
||||
|
||||
const goToError = (req: NextRequest) => {
|
||||
const errUrl = new URL("/redirect/error", req.url);
|
||||
errUrl.searchParams.set("code", "fetch_error");
|
||||
errUrl.searchParams.set("sid", req.nextUrl.searchParams.get("sid") ?? "");
|
||||
return NextResponse.redirect(errUrl, 307);
|
||||
};
|
||||
Reference in New Issue
Block a user