| 1 | import { NextResponse } from 'next/server'; |
| 2 | import { isRateLimited, getClientIp } from '@/lib/ssrf-guard'; |
| 3 | |
| 4 | // DNS Lookup via Google DNS-over-HTTPS (free, no key) |
| 5 | export async function GET(req: Request) { |
| 6 | const { searchParams } = new URL(req.url); |
| 7 | const domain = searchParams.get('domain'); |
| 8 | if (!domain) return NextResponse.json({ error: 'Missing domain parameter' }, { status: 400 }); |
| 9 | |
| 10 | const clientIp = getClientIp(req); |
| 11 | if (isRateLimited(clientIp, 20, 60_000)) { |
| 12 | return NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 }); |
| 13 | } |
| 14 | |
| 15 | // Basic domain validation |
| 16 | if (!/^[a-zA-Z0-9][a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(domain)) { |
| 17 | return NextResponse.json({ error: 'Invalid domain format' }, { status: 400 }); |
| 18 | } |
| 19 | |
| 20 | try { |
| 21 | const types = ['A', 'AAAA', 'MX', 'NS', 'TXT', 'CNAME', 'SOA']; |
| 22 | const results: any = { domain, records: {}, timestamp: new Date().toISOString() }; |
| 23 | |
| 24 | const lookups = await Promise.allSettled( |
| 25 | types.map(async (type) => { |
| 26 | const res = await fetch(`https://dns.google/resolve?name=${encodeURIComponent(domain)}&type=${type}`, { |
| 27 | signal: AbortSignal.timeout(5000), |
| 28 | headers: { 'Accept': 'application/json' }, |
| 29 | }); |
| 30 | if (res.ok) { |
| 31 | const data = await res.json(); |
| 32 | return { type, answers: data.Answer || [], status: data.Status }; |
| 33 | } |
| 34 | return { type, answers: [], status: -1 }; |
| 35 | }) |
| 36 | ); |
| 37 | |
| 38 | for (const result of lookups) { |
| 39 | if (result.status === 'fulfilled') { |
| 40 | const { type, answers } = result.value; |
| 41 | results.records[type] = answers.map((a: any) => ({ |
| 42 | name: a.name, |
| 43 | type: a.type, |
| 44 | ttl: a.TTL, |
| 45 | data: a.data, |
| 46 | })); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Extract useful summary |
| 51 | const aRecords = results.records.A || []; |
| 52 | const mxRecords = results.records.MX || []; |
| 53 | const nsRecords = results.records.NS || []; |
| 54 | |
| 55 | results.summary = { |
| 56 | ip_addresses: aRecords.map((r: any) => r.data), |
| 57 | mail_servers: mxRecords.map((r: any) => r.data), |
| 58 | nameservers: nsRecords.map((r: any) => r.data), |
| 59 | total_records: Object.values(results.records).flat().length, |
| 60 | }; |
| 61 | |
| 62 | return NextResponse.json(results); |
| 63 | } catch { |
| 64 | return NextResponse.json({ error: 'DNS lookup failed' }, { status: 500 }); |
| 65 | } |
| 66 | } |