16 lines
496 B
TypeScript
16 lines
496 B
TypeScript
import { createHmac } from "node:crypto";
|
|
import { isIP } from "node:net";
|
|
|
|
export function getClientAddress(headers: Headers) {
|
|
const forwardedAddress = headers
|
|
.get("x-forwarded-for")
|
|
?.split(",", 1)[0]
|
|
?.trim();
|
|
const address = forwardedAddress || headers.get("x-real-ip")?.trim();
|
|
return address && isIP(address) ? address : null;
|
|
}
|
|
|
|
export function hashClientAddress(address: string, secret: string) {
|
|
return createHmac("sha256", secret).update(address).digest("hex");
|
|
}
|