export default { async fetch(request) { const url = new URL(request.url); const email = request.headers.get("X-CF-Email"); const apiKey = request.headers.get("X-CF-Key"); const headers = { "X-Auth-Email": email, "X-Auth-Key": apiKey, "Content-Type": "application/json" }; try { if (url.pathname === "/api/validate") { const res = await fetch("https://api.cloudflare.com/client/v4/zones", { headers }); return new Response(JSON.stringify(await res.json()), { headers: { "Content-Type": "application/json" } }); } // Health Checker API if (url.pathname === "/api/health") { const target = url.searchParams.get("target"); try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 2500); const check = await fetch(`http://${target}`, { method: 'HEAD', signal: controller.signal }); return new Response(JSON.stringify({ status: check.ok ? 'UP' : 'DOWN' })); } catch (e) { return new Response(JSON.stringify({ status: 'DOWN' })); } } if (!email || !apiKey) return new Response(getUI(), { headers: { "Content-Type": "text/html" } }); const zoneRes = await fetch("https://api.cloudflare.com/client/v4/zones?per_page=50", { headers }); const zones = (await zoneRes.json()).result || []; const activeZoneId = url.searchParams.get("zone") || (zones.length > 0 ? zones[0].id : null); const BASE_API = `https://api.cloudflare.com/client/v4/zones/${activeZoneId}/dns_records`; if (url.pathname === "/api/get") { const res = await fetch(BASE_API + "?per_page=100", { headers }); const data = await res.json(); return new Response(JSON.stringify({ ...data, allZones: zones }), { headers: { "Content-Type": "application/json" } }); } if (url.pathname === "/api/save" && request.method === "POST") { const body = await request.json(); const res = await fetch(BASE_API, { method: "POST", headers, body: JSON.stringify({ type: body.type, name: body.name, content: body.content, ttl: 1, proxied: body.proxied }) }); return new Response(await res.text()); } if (url.pathname === "/api/del" && request.method === "DELETE") { await fetch(`${BASE_API}/${url.searchParams.get("id")}`, { method: "DELETE", headers }); return new Response("OK"); } return new Response(getUI(), { headers: { "Content-Type": "text/html" } }); } catch (e) { return new Response(e.message, { status: 500 }); } } }; function getUI() { return ` DNS Manager - Bulk Master
`; }