15 lines
480 B
TypeScript
15 lines
480 B
TypeScript
import type { NextRequest } from "next/server";
|
|
|
|
export function clientIp(req: NextRequest | Request): string | null {
|
|
const headers = "headers" in req ? req.headers : new Headers();
|
|
const fwd = headers.get("x-forwarded-for");
|
|
if (fwd) return fwd.split(",")[0]!.trim();
|
|
const real = headers.get("x-real-ip");
|
|
if (real) return real.trim();
|
|
return null;
|
|
}
|
|
|
|
export function userAgent(req: NextRequest | Request): string | null {
|
|
return req.headers.get("user-agent");
|
|
}
|