export function getClientIp(req: any): string {
  let ip =
    req.ip ||
    (Array.isArray(req.headers['x-forwarded-for'])
      ? req.headers['x-forwarded-for'][0]
      : req.headers['x-forwarded-for']) ||
    req.socket.remoteAddress;

  // 如果是 IPv4-mapped IPv6 地址，转换为 IPv4
  if (typeof ip === 'string' && ip.startsWith('::ffff:')) {
    ip = ip.substring(7);
  }

  return ip || '';
}
