use cloudflare real IP (#10449)

pull/10450/head
sean-brydon 2023-07-28 16:11:44 +01:00 committed by GitHub
parent 2c2fd14050
commit 878f074cb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 2 deletions

View File

@ -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";
}