Skip to content

Commit

Permalink
Move the TLD check
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunoneill-r7 committed Jul 8, 2023
1 parent 02e966e commit 99c20e0
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const WEBHOOK_URL = process.env.WEBHOOK_URL;
const WEBHOOK_METHOD = process.env.WEBHOOK_METHOD;
const IP_OVERRIDE = process.env.IP_OVERRIDE;

// Find out the TLD of the DNS_URL
const PARSED_TLD = DNS_URL.split('.').slice(-2).join('.');
console.log(`Parsed TLD: ${PARSED_TLD}`);

let job = null;

const start = async () => {
Expand All @@ -41,17 +45,12 @@ const start = async () => {
console.log(`Public IP Address: ${publicIpAddress.ip}`);
}

// Find out the TLD of the DNS_URL
const tld = DNS_URL.split('.').slice(-2).join('.');

console.log(`TLD: ${tld}`);

// Get the zones from Cloudflare and find the zone ID for the DNS_URL
const zones = await getCloudflareZones();

const zone = zones.result.find((zone) => zone.name === tld);
const zone = zones.result.find((zone) => zone.name === PARSED_TLD);
if (!zone) {
return console.log(`Unable to find zone for ${tld}. Please ensure that the domain is registered with Cloudflare.`);
sendWebhookRequest({status: 'error', message: `Unable to find zone for ${PARSED_TLD}. Please ensure that the domain is registered with Cloudflare.`});
return console.log(`Unable to find zone for ${PARSED_TLD}. Please ensure that the domain is registered with Cloudflare.`);
}
console.log(`Found Zone: ${zone.name} (${zone.id})`);

Expand All @@ -66,7 +65,14 @@ const start = async () => {
if (dnsRecord.content === publicIpAddress.ip && dnsRecord.proxied === PROXIED) {
console.log(`DNS record for ${DNS_URL} is already up to date. No action required.`);
} else {
console.log(`DNS record for ${DNS_URL} needs to be updated.`);
if (dnsRecord.content !== publicIpAddress.ip) {
console.log(`DNS record for ${DNS_URL} needs to be updated. IP Address: ${dnsRecord.content} -> ${publicIpAddress.ip}`);
}
if (dnsRecord.proxied !== PROXIED) {
console.log(`DNS record for ${DNS_URL} needs to be updated. Proxied: ${dnsRecord.proxied} -> ${PROXIED}`);
}
sendWebhookRequest({status: 'progress', message: `DNS record for ${DNS_URL} needs to be updated. IP Address: ${dnsRecord.content} -> ${publicIpAddress.ip}, Proxied: ${dnsRecord.proxied} -> ${PROXIED}`});

// Update the DNS record
const updatedDnsRecord = await updateCloudflareZoneRecord(zone.id, dnsRecord.id, {
type: dnsRecord.type,
Expand All @@ -75,8 +81,8 @@ const start = async () => {
proxied: PROXIED,
});

console.log(`DNS record for ${DNS_URL} has been updated. IP Address: ${updatedDnsRecord.content}, Proxied: ${updatedDnsRecord.proxied}`);
sendWebhookRequest({status: 'success', message: `DNS record for ${DNS_URL} has been updated. IP Address: ${updatedDnsRecord.content}, Proxied: ${updatedDnsRecord.proxied}`});
console.log(`DNS record for ${DNS_URL} has been updated. IP Address: ${updatedDnsRecord.result.content}, Proxied: ${updatedDnsRecord.result.proxied}`);
sendWebhookRequest({status: 'success', message: `DNS record for ${DNS_URL} has been updated. IP Address: ${updatedDnsRecord.result.content}, Proxied: ${updatedDnsRecord.result.proxied}`});
}
} else {
console.log(`DNS record does not exist for ${DNS_URL}, creating it...`);
Expand All @@ -87,8 +93,8 @@ const start = async () => {
proxied: PROXIED,
});

console.log(`DNS record for ${DNS_URL} has been created. IP Address: ${newRecord.content}`);
sendWebhookRequest({status: 'progress', message: `DNS record for ${DNS_URL} has been created. IP Address: ${newRecord.content}`});
console.log(`DNS record for ${DNS_URL} has been created. IP Address: ${newRecord.result.content}`);
sendWebhookRequest({status: 'progress', message: `DNS record for ${DNS_URL} has been created. IP Address: ${newRecord.result.content}`});
}

console.log('Completed, waiting for next scheduled run...\n');
Expand All @@ -104,6 +110,7 @@ const verifyStartup = () => {
console.log('Configuration:');
console.log(`CLOUDFLARE_API_KEY: ${CLOUDFLARE_API_KEY}`);
console.log(`DNS_URL: ${DNS_URL}`);
console.log('PARSED_TLD: ', PARSED_TLD);
console.log(`CRON_SCHEDULE: ${CRON_SCHEDULE}`);
console.log(`TIMEZONE: ${TIMEZONE}`);
if (PROXIED) console.log(`PROXIED: ${PROXIED}`);
Expand All @@ -122,8 +129,12 @@ const verifyStartup = () => {
return console.log('CRON_SCHEDULE is required.');
}

if (!PARSED_TLD) {
return console.log('Unable to parse TLD from DNS_URL.');
}

if (!PROXIED) {
return console.log('PROXIED value is not supplied so defaulting to - false.');
console.log('PROXIED value is not supplied so defaulting to - false.');
}

if (WEBHOOK_URL || WEBHOOK_METHOD) {
Expand Down

0 comments on commit 99c20e0

Please sign in to comment.