| 1 | import { NextResponse } from 'next/server'; |
| 2 | import { isRateLimited, getClientIp } from '@/lib/ssrf-guard'; |
| 3 | import { matchExact, type SanctionEntry } from '@/lib/sanctions'; |
| 4 | |
| 5 | // IP Geolocation + Reputation — combines multiple free sources. |
| 6 | // Cross-checks the ASN owner / ISP / org strings against the OFAC SDN |
| 7 | // list so an IP routed via a sanctioned operator surfaces a hit. |
| 8 | export async function GET(req: Request) { |
| 9 | const { searchParams } = new URL(req.url); |
| 10 | const ip = searchParams.get('ip'); |
| 11 | if (!ip) return NextResponse.json({ error: 'Missing ip parameter' }, { status: 400 }); |
| 12 | |
| 13 | const clientIp = getClientIp(req); |
| 14 | if (isRateLimited(clientIp, 20, 60_000)) { |
| 15 | return NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 }); |
| 16 | } |
| 17 | |
| 18 | // Validate IP format |
| 19 | const ipv4 = /^(\d{1,3}\.){3}\d{1,3}$/; |
| 20 | const ipv6 = /^[0-9a-fA-F:]+$/; |
| 21 | if (!ipv4.test(ip) && !ipv6.test(ip)) { |
| 22 | return NextResponse.json({ error: 'Invalid IP format' }, { status: 400 }); |
| 23 | } |
| 24 | |
| 25 | try { |
| 26 | const results: any = { ip, timestamp: new Date().toISOString() }; |
| 27 | |
| 28 | // 1. ip-api.com — geolocation (free, no key) |
| 29 | try { |
| 30 | const res = await fetch(`http://ip-api.com/json/${ip}?fields=status,message,continent,country,countryCode,region,regionName,city,zip,lat,lon,timezone,isp,org,as,asname,mobile,proxy,hosting,query`, { |
| 31 | signal: AbortSignal.timeout(5000), |
| 32 | }); |
| 33 | if (res.ok) { |
| 34 | const geo = await res.json(); |
| 35 | if (geo.status === 'success') { |
| 36 | results.geo = { |
| 37 | country: geo.country, |
| 38 | country_code: geo.countryCode, |
| 39 | region: geo.regionName, |
| 40 | city: geo.city, |
| 41 | lat: geo.lat, |
| 42 | lon: geo.lon, |
| 43 | timezone: geo.timezone, |
| 44 | isp: geo.isp, |
| 45 | org: geo.org, |
| 46 | as_number: geo.as, |
| 47 | as_name: geo.asname, |
| 48 | is_mobile: geo.mobile, |
| 49 | is_proxy: geo.proxy, |
| 50 | is_hosting: geo.hosting, |
| 51 | }; |
| 52 | } |
| 53 | } |
| 54 | } catch (e) { console.warn('[OSIRIS] Suppressed error:', e instanceof Error ? e.message : e); } |
| 55 | |
| 56 | // 2. AbuseIPDB-style check via ip-api proxy flag |
| 57 | results.reputation = { |
| 58 | is_proxy: results.geo?.is_proxy || false, |
| 59 | is_hosting: results.geo?.is_hosting || false, |
| 60 | is_mobile: results.geo?.is_mobile || false, |
| 61 | risk_level: results.geo?.is_proxy ? 'HIGH' : results.geo?.is_hosting ? 'MEDIUM' : 'LOW', |
| 62 | }; |
| 63 | |
| 64 | // OFAC SDN cross-check on ASN / ISP / org strings. |
| 65 | try { |
| 66 | const candidates = new Set<string>(); |
| 67 | if (results.geo?.org) candidates.add(results.geo.org); |
| 68 | if (results.geo?.isp) candidates.add(results.geo.isp); |
| 69 | if (results.geo?.as_name) candidates.add(results.geo.as_name); |
| 70 | const hits: Array<{ matched_value: string; entries: SanctionEntry[] }> = []; |
| 71 | for (const value of candidates) { |
| 72 | const entries = await matchExact(value); |
| 73 | if (entries.length) hits.push({ matched_value: value, entries }); |
| 74 | } |
| 75 | results.sanctions_match = hits.length |
| 76 | ? { source: 'OFAC SDN', hits } |
| 77 | : null; |
| 78 | } catch (e) { console.warn('[OSIRIS] Sanctions cross-check failed:', e instanceof Error ? e.message : e); } |
| 79 | |
| 80 | return NextResponse.json(results); |
| 81 | } catch { |
| 82 | return NextResponse.json({ error: 'IP lookup failed' }, { status: 500 }); |
| 83 | } |
| 84 | } |