Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
Add support for multiple IPs in a call
  • Loading branch information
cyberpower678 authored and willswire committed Apr 20, 2024
1 parent 9be7f75 commit 40ce8f3
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,24 @@ async function handleRequest(request) {
const hostnames = hostnameParam?.split(",");

// fallback to connecting IP address
const ip = params?.get("ip") || params?.get("myip") || request.headers.get("Cf-Connecting-Ip");
const ipsParam = params.get("ips") || params.get("ip") || params.get("myip") || request.headers.get("Cf-Connecting-Ip");
const ips = ipsParam?.split(",");

if (!hostnames || hostnames.length === 0) {
throw new BadRequestException("You must specify a hostname");
if (!hostnames || hostnames.length === 0 || !ips || ips.length === 0) {
throw new BadRequestException("You must specify both hostname(s) and IP address(es)");
}

const response = await informAPI(hostnames, ip, username, password);
return response;
// Iterate over each IP and update DNS records for all hostnames
for (const ip of ips) {
await informAPI(hostnames, ip.trim(), username, token);
}
return new Response("good", {
status: 200,
headers: {
"Content-Type": "text/plain;charset=UTF-8",
"Cache-Control": "no-store",
},
});
}

async function informAPI(hostnames, ip, name, token) {
Expand All @@ -152,14 +162,6 @@ async function informAPI(hostnames, ip, name, token) {
const record = await cloudflare.findRecord(zone, hostname, isIPV4);
await cloudflare.updateRecord(record, ip);
}

return new Response("good", {
status: 200,
headers: {
"Content-Type": "text/plain;charset=UTF-8",
"Cache-Control": "no-store",
},
});
}

export default {
Expand Down

0 comments on commit 40ce8f3

Please sign in to comment.