import 'server-only'; import { headers } from 'next/headers'; export async function clientInfo(): Promise<{ ip: string | null; userAgent: string | null }> { const h = await headers(); const xff = h.get('x-forwarded-for'); const ip = (xff ? xff.split(',').pop()?.trim() : null) ?? h.get('x-real-ip') ?? null; const userAgent = h.get('user-agent')?.slice(0, 200) ?? null; return { ip, userAgent }; } /** Short device label from a user agent, for security lists. */ export function deviceLabel(ua: string | null | undefined): string { if (!ua) return 'Unknown device'; const os = /iPhone|iPad/.test(ua) ? 'iOS' : /Android/.test(ua) ? 'Android' : /Mac OS X/.test(ua) ? 'macOS' : /Windows/.test(ua) ? 'Windows' : /Linux/.test(ua) ? 'Linux' : 'Unknown OS'; const browser = /Edg\//.test(ua) ? 'Edge' : /OPR\//.test(ua) ? 'Opera' : /Chrome\//.test(ua) ? 'Chrome' : /Safari\//.test(ua) ? 'Safari' : /Firefox\//.test(ua) ? 'Firefox' : /curl|node|undici/i.test(ua) ? 'API client' : 'Browser'; return `${browser} ยท ${os}`; }