From 85f2b9934564f43a7e9adb216dca8504e0d15be1 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Wed, 1 Jul 2026 14:15:05 +0900 Subject: [PATCH 1/2] =?UTF-8?q?260701=5F01=5FENH:=20=EC=82=AC=EC=9D=B4?= =?UTF-8?q?=ED=8A=B8=20=EC=A0=91=EA=B7=BC=20=EC=8B=9C=20=ED=97=88=EC=9A=A9?= =?UTF-8?q?=20IP=EB=A7=8C=20=EC=A0=91=EA=B7=BC=ED=95=98=EB=8F=84=EB=A1=9D?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 3 +- .env.local | 3 +- .env.production | 3 +- src/app/403/page.tsx | 21 ++++++++ src/proxy.ts | 111 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 src/app/403/page.tsx create mode 100644 src/proxy.ts diff --git a/.env.development b/.env.development index f84ff2c..cf78f6c 100644 --- a/.env.development +++ b/.env.development @@ -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 \ No newline at end of file +API_BACK_SERVER=http://127.0.0.1:6103/api/v1 +IP_WHITELIST=127.0.0.1,::1 \ No newline at end of file diff --git a/.env.local b/.env.local index 8f7c60d..f829a29 100644 --- a/.env.local +++ b/.env.local @@ -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 \ No newline at end of file +API_BACK_SERVER=http://localhost:6103/api/v1 +IP_WHITELIST=127.0.0.1,::1 \ No newline at end of file diff --git a/.env.production b/.env.production index a955d85..94c34c6 100644 --- a/.env.production +++ b/.env.production @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/src/app/403/page.tsx b/src/app/403/page.tsx new file mode 100644 index 0000000..f00141c --- /dev/null +++ b/src/app/403/page.tsx @@ -0,0 +1,21 @@ +import type { Metadata } from "next"; + +export const metadata: Metadata = { + title: "403 Forbidden", +}; + +const ForbiddenPage = () => { + return ( +
+
+

403

+

외부에서 접속할 수 없습니다.

+

+ 해당 사이트는 외부의 접근을 허용하지 않습니다. +

+
+
+ ); +}; + +export default ForbiddenPage; diff --git a/src/proxy.ts b/src/proxy.ts new file mode 100644 index 0000000..6770949 --- /dev/null +++ b/src/proxy.ts @@ -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((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|.*\\..*).*)"], +}; -- 2.49.0.windows.1 From 5502415825aacf6f58ec282f7d60c01cde05ba8f Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Wed, 1 Jul 2026 14:17:23 +0900 Subject: [PATCH 2/2] =?UTF-8?q?260701=5F02=5FENH:=20=EC=82=AC=EC=9D=B4?= =?UTF-8?q?=ED=8A=B8=20=EC=A0=91=EA=B7=BC=20=ED=97=88=EC=9A=A9=20IP?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.development b/.env.development index cf78f6c..c49d460 100644 --- a/.env.development +++ b/.env.development @@ -3,4 +3,4 @@ NEXT_PUBLIC_API_BASE=https://test-kfpa-dash.kric.com/api ENV=prod API_BACK_SERVER=http://127.0.0.1:6103/api/v1 -IP_WHITELIST=127.0.0.1,::1 \ No newline at end of file +IP_WHITELIST=127.0.0.1,::1,211.37.10.198,211.254.145.130,1.235.195.3 \ No newline at end of file -- 2.49.0.windows.1