Merge pull request 'develop' (#3) from develop into master
Reviewed-on: #3
This commit is contained in:
@@ -2,4 +2,5 @@ NEXT_PUBLIC_ENV=prod
|
||||
NEXT_PUBLIC_API_BASE=https://test-kfpa-dash.kric.com/api
|
||||
|
||||
ENV=prod
|
||||
API_BACK_SERVER=http://127.0.0.1:6103/api/v1
|
||||
API_BACK_SERVER=http://127.0.0.1:6103/api/v1
|
||||
IP_WHITELIST=127.0.0.1,::1,211.37.10.198,211.254.145.130,1.235.195.3
|
||||
@@ -2,4 +2,5 @@ NEXT_PUBLIC_ENV=local
|
||||
NEXT_PUBLIC_API_BASE=http://localhost:6102/api
|
||||
|
||||
ENV=local
|
||||
API_BACK_SERVER=http://localhost:6103/api/v1
|
||||
API_BACK_SERVER=http://localhost:6103/api/v1
|
||||
IP_WHITELIST=127.0.0.1,::1
|
||||
@@ -2,4 +2,5 @@ NEXT_PUBLIC_ENV=prod
|
||||
NEXT_PUBLIC_API_BASE=https://kfpa-dash.kric.com/api
|
||||
|
||||
ENV=prod
|
||||
API_BACK_SERVER=http://127.0.0.1:6103/api/v1
|
||||
API_BACK_SERVER=http://127.0.0.1:6103/api/v1
|
||||
IP_WHITELIST=127.0.0.1,::1,211.37.10.198,211.254.145.130,1.235.195.3
|
||||
21
src/app/403/page.tsx
Normal file
21
src/app/403/page.tsx
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { Metadata } from "next";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "403 Forbidden",
|
||||
};
|
||||
|
||||
const ForbiddenPage = () => {
|
||||
return (
|
||||
<main className="flex min-h-dvh items-center justify-center bg-background px-6">
|
||||
<section className="w-full max-w-md border border-border bg-card p-8 text-center shadow-sm">
|
||||
<p className="text-sm font-semibold text-primary">403</p>
|
||||
<h1 className="mt-3 text-2xl font-bold text-foreground">외부에서 접속할 수 없습니다.</h1>
|
||||
<p className="mt-4 text-sm leading-6 text-muted-foreground">
|
||||
해당 사이트는 외부의 접근을 허용하지 않습니다.
|
||||
</p>
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
};
|
||||
|
||||
export default ForbiddenPage;
|
||||
111
src/proxy.ts
Normal file
111
src/proxy.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const IP_WHITELIST_ENV = "IP_WHITELIST";
|
||||
const LOCAL_IPS = new Set(["127.0.0.1", "::1"]);
|
||||
|
||||
const normalizeIp = (ip: string) => {
|
||||
const trimmed = ip.trim();
|
||||
|
||||
if (!trimmed) return "";
|
||||
if (trimmed.startsWith("::ffff:")) return trimmed.slice("::ffff:".length);
|
||||
|
||||
const withoutBrackets = trimmed.replace(/^\[|\]$/g, "");
|
||||
const portSeparatorIndex = withoutBrackets.lastIndexOf(":");
|
||||
if (
|
||||
portSeparatorIndex > -1 &&
|
||||
withoutBrackets.indexOf(":") === portSeparatorIndex &&
|
||||
/^\d+$/.test(withoutBrackets.slice(portSeparatorIndex + 1))
|
||||
) {
|
||||
return withoutBrackets.slice(0, portSeparatorIndex);
|
||||
}
|
||||
|
||||
return withoutBrackets;
|
||||
};
|
||||
|
||||
const getClientIp = (request: NextRequest) => {
|
||||
const forwardedFor = request.headers.get("x-forwarded-for");
|
||||
if (forwardedFor) {
|
||||
return normalizeIp(forwardedFor.split(",")[0]);
|
||||
}
|
||||
|
||||
return normalizeIp(
|
||||
request.headers.get("x-real-ip") ??
|
||||
request.headers.get("cf-connecting-ip") ??
|
||||
request.headers.get("true-client-ip") ??
|
||||
""
|
||||
);
|
||||
};
|
||||
|
||||
const ipv4ToNumber = (ip: string) => {
|
||||
const parts = ip.split(".");
|
||||
if (parts.length !== 4) return null;
|
||||
|
||||
return parts.reduce<number | null>((acc, part) => {
|
||||
if (acc === null || !/^\d+$/.test(part)) return null;
|
||||
|
||||
const value = Number(part);
|
||||
if (value < 0 || value > 255) return null;
|
||||
|
||||
return ((acc << 8) + value) >>> 0;
|
||||
}, 0);
|
||||
};
|
||||
|
||||
const isIpv4InCidr = (ip: string, cidr: string) => {
|
||||
const [rangeIp, prefixRaw] = cidr.split("/");
|
||||
const prefix = Number(prefixRaw);
|
||||
const target = ipv4ToNumber(ip);
|
||||
const range = ipv4ToNumber(rangeIp);
|
||||
|
||||
if (target === null || range === null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const mask = prefix === 0 ? 0 : (0xffffffff << (32 - prefix)) >>> 0;
|
||||
return (target & mask) === (range & mask);
|
||||
};
|
||||
|
||||
const parseWhitelist = () =>
|
||||
(process.env[IP_WHITELIST_ENV] ?? "")
|
||||
.split(",")
|
||||
.map((ip) => normalizeIp(ip))
|
||||
.filter(Boolean);
|
||||
|
||||
const isAllowedIp = (clientIp: string) => {
|
||||
const whitelist = parseWhitelist();
|
||||
|
||||
if (whitelist.length === 0) {
|
||||
return process.env.ENV === "local" && LOCAL_IPS.has(clientIp);
|
||||
}
|
||||
|
||||
return whitelist.some((allowedIp) => {
|
||||
if (allowedIp.includes("/")) {
|
||||
return isIpv4InCidr(clientIp, allowedIp);
|
||||
}
|
||||
|
||||
return clientIp === allowedIp;
|
||||
});
|
||||
};
|
||||
|
||||
export function proxy(request: NextRequest) {
|
||||
const pathname = request.nextUrl.pathname;
|
||||
|
||||
if (pathname === "/403") {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const clientIp = getClientIp(request);
|
||||
|
||||
if (clientIp && isAllowedIp(clientIp)) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const url = request.nextUrl.clone();
|
||||
url.pathname = "/403";
|
||||
url.search = "";
|
||||
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|.*\\..*).*)"],
|
||||
};
|
||||
Reference in New Issue
Block a user