-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.min.js
1 lines (1 loc) · 1.73 KB
/
worker.min.js
1
export default{async fetch(e,_){let{searchParams:i}=new URL(e.url),n={userAgent:e.headers.get("user-agent")||"Unknown agent",token:i.get("token")||"",zoneID:i.get("zoneid")||"",comment:i.get("comment")||"",ipv4:{address:i.get("ipv4address")||"",name:i.get("ipv4name")||"",proxied:"true"===(i.get("ipv4proxied")||"true"),ttl:parseInt(i.get("ipv4ttl"),10)||1},ipv6:{address:i.get("ipv6address")||"",name:i.get("ipv6name")||"",proxied:"true"===(i.get("ipv6proxied")||"true"),ttl:parseInt(i.get("ipv6ttl"),10)||1}},r=`https://api.cloudflare.com/client/v4/zones/${n.zoneID}/dns_records`,s={"Content-Type":"application/json",Authorization:`Bearer ${n.token}`},a=n.comment.replace(/<userAgent>/gmi,n.userAgent);async function p(e,t){let i=await fetch(`${r}/?type=${e}&name=${t}`,{method:"GET",headers:s}).then(e=>e.json());return i?.result[0]?.id}function d(e,t,i,n,r,p,d){return fetch(e,{method:t,headers:s,body:JSON.stringify({content:i,name:n,proxied:r,type:p,comment:a,ttl:d})}).then(e=>e.json())}if(a=a.length>50?a.slice(0,47)+"...":a,n.ipv4.address){let o=await p("A",n.ipv4.name);if(o){let v=await d(`${r}/${o}`,"PUT",n.ipv4.address,n.ipv4.name,n.ipv4.proxied,"A",n.ipv4.ttl);if(!v?.success)return new Response(JSON.stringify(v))}else{let l=await d(r,"POST",n.ipv4.address,n.ipv4.name,n.ipv4.proxied,"A",n.ipv4.ttl);if(!l?.success)return new Response(JSON.stringify(l))}}if(n.ipv6.address){let u=await p("AAAA",n.ipv6.name);if(u){let g=await d(`${r}/${u}`,"PUT",n.ipv6.address,n.ipv6.name,n.ipv6.proxied,"AAAA",n.ipv6.ttl);if(!g?.success)return new Response(JSON.stringify(g))}else{let c=await d(r,"POST",n.ipv6.address,n.ipv6.name,n.ipv6.proxied,"AAAA",n.ipv6.ttl);if(!c?.success)return new Response(JSON.stringify(c))}}return new Response(JSON.stringify(n,null,4))}};