From 878f074cb6055fa145ceb6d07bf3184bf998b9f6 Mon Sep 17 00:00:00 2001 From: sean-brydon <55134778+sean-brydon@users.noreply.github.com> Date: Fri, 28 Jul 2023 16:11:44 +0100 Subject: [PATCH] use cloudflare real IP (#10449) --- packages/lib/getIP.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/lib/getIP.ts b/packages/lib/getIP.ts index 31c18803aa..6b26c971ee 100644 --- a/packages/lib/getIP.ts +++ b/packages/lib/getIP.ts @@ -10,8 +10,14 @@ export function parseIpFromHeaders(value: string | string[]) { * @see https://github.com/vercel/examples/blob/main/edge-functions/ip-blocking/lib/get-ip.ts **/ export default function getIP(request: Request | NextApiRequest) { - const xff = - request instanceof Request ? request.headers.get("x-forwarded-for") : request.headers["x-forwarded-for"]; + let xff = + request instanceof Request + ? request.headers.get("cf-connecting-ip") + : request.headers["cf-connecting-ip"]; + + if (!xff) { + xff = request instanceof Request ? request.headers.get("x-real-ip") : request.headers["x-real-ip"]; + } return xff ? parseIpFromHeaders(xff) : "127.0.0.1"; }