use cloudflare real IP (#10449)
parent
2c2fd14050
commit
878f074cb6
|
@ -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
|
* @see https://github.com/vercel/examples/blob/main/edge-functions/ip-blocking/lib/get-ip.ts
|
||||||
**/
|
**/
|
||||||
export default function getIP(request: Request | NextApiRequest) {
|
export default function getIP(request: Request | NextApiRequest) {
|
||||||
const xff =
|
let xff =
|
||||||
request instanceof Request ? request.headers.get("x-forwarded-for") : request.headers["x-forwarded-for"];
|
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";
|
return xff ? parseIpFromHeaders(xff) : "127.0.0.1";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue