mirror of
https://github.com/whyour/qinglong.git
synced 2026-08-16 14:20:51 +08:00
30 lines
625 B
TypeScript
30 lines
625 B
TypeScript
import { Request } from 'express';
|
|
|
|
const IPV4_MAPPED_PREFIX = '::ffff:';
|
|
|
|
export function normalizeClientIp(value?: string): string {
|
|
let ip = (value || '').trim().toLowerCase();
|
|
if (!ip) {
|
|
return '';
|
|
}
|
|
|
|
if (ip.startsWith('[') && ip.endsWith(']')) {
|
|
ip = ip.slice(1, -1);
|
|
}
|
|
|
|
const zoneIndex = ip.indexOf('%');
|
|
if (zoneIndex !== -1) {
|
|
ip = ip.slice(0, zoneIndex);
|
|
}
|
|
|
|
if (ip.startsWith(IPV4_MAPPED_PREFIX)) {
|
|
return ip.slice(IPV4_MAPPED_PREFIX.length);
|
|
}
|
|
|
|
return ip;
|
|
}
|
|
|
|
export function getClientIp(req: Request): string {
|
|
return normalizeClientIp(req.ip || req.socket.remoteAddress);
|
|
}
|