import { isIP } from 'node:net'; import { pathToFileURL } from 'node:url'; export async function checkIp(ip, apiKey) { if (!isIP(ip)) throw new Error('Supply a valid public IP address.'); if (!apiKey) throw new Error('Set PROXYRADAR_API_KEY on your server.'); const url = new URL('https://proxyradar.io/v1/check'); url.search = new URLSearchParams({ ip, format: 'json' }).toString(); let response; try { response = await fetch(url, { headers: { Authorization: `Bearer ${apiKey}`, Accept: 'application/json' }, signal: AbortSignal.timeout(15000), redirect: 'error', }); } catch { throw new Error('Lookup unavailable; result is unknown.'); } if (!response.ok) { throw new Error(`Lookup unavailable (HTTP ${response.status}); result is unknown.`); } try { const body = await response.text(); if (body.length > 65536) throw new Error('Response too large'); const data = JSON.parse(body); if (data?.status !== 'success' || ![0, 1].includes(data.proxy)) { throw new Error('Unexpected verdict'); } return { ip: data.ip, proxy: data.proxy, flags: data.flags }; } catch { throw new Error('Invalid lookup response; result is unknown.'); } } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { try { const result = await checkIp(process.argv[2] || '', process.env.PROXYRADAR_API_KEY); console.log(JSON.stringify(result, null, 2)); } catch (error) { console.error(error.message); process.exitCode = 1; } }