Skip to content

Commit

Permalink
Option to use multiple sub domains
Browse files Browse the repository at this point in the history
  • Loading branch information
realshaunoneill committed Mar 19, 2024
1 parent f84218c commit fab70c4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ The .env file is used to store your Cloudflare API key and other configuration o
You can modify the following configuration settings in the .env file:

- CLOUDFLARE_API_KEY: Your Cloudflare API key or access token.
- DNS_URL: The subdomain to update with the current IP address (can be a wildcard).
- DNS_URL: The subdomain to update with the current IP address (can be a wildcard, multiples can be separated with commas).
- PROXIED: Whether or not to proxy the DNS record through Cloudflare.
- CRON_SCHEDULE: The interval at which to check for IP address changes (using cron syntax).
- WEBHOOK_URL: The URL to send a webhook to when the IP address changes.
Expand Down
93 changes: 55 additions & 38 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,12 @@ const DEBUG = process.env.DEBUG || false;

let job = null;

const start = async () => {
try {
// Verify that the cloudflare API key is valid
const isCloudflareTokenValid = await verifyCloudflareToken();
if (!isCloudflareTokenValid) {
console.log('Cloudflare API key is invalid. Exiting. Sent API key: ', CLOUDFLARE_API_KEY);
job.stop();
return;
}

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

// Get the public IP address of the machine
const publicIpAddress = IP_OVERRIDE ? IP_OVERRIDE : await getPublicIpAddress();
if (IP_OVERRIDE) {
console.log(`IP Override: ${IP_OVERRIDE}, not checking public IP address.`);
} else {
if (!publicIpAddress) {
console.log('Unable to get public IP address.', publicIpAddress);
}
console.log(`Public IP Address: ${publicIpAddress}`);
}
const getParsedTLD = (dnsUrl) => {
return dnsUrl.split('.').slice(-2).join('.');
}

const updateDomain = async (domain) => {
try {
// 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 === PARSED_TLD);
Expand All @@ -64,23 +45,23 @@ const start = async () => {
console.log(`Unable to get DNS records for ${PARSED_TLD}.`);
}

// Check if the DNS_URL is already in the zone
const dnsRecord = zoneRecords.result.find((record) => record.name === DNS_URL);
// Check if the domain is already in the zone
const dnsRecord = zoneRecords.result.find((record) => record.name === domain);
if (dnsRecord) {
console.log(`DNS record already exists for ${DNS_URL}, checking if it needs to be updated...`);
console.log(`DNS record already exists for ${domain}, checking if it needs to be updated...`);

// Check if the DNS record needs to be updated
if (dnsRecord.content === publicIpAddress && dnsRecord.proxied === PROXIED) {
console.log(`DNS record for ${DNS_URL} is already up to date. No action required.`);
sendWebhookRequest({ status: 'success', message: `DNS record for ${DNS_URL} is already up to date. No action required.` });
console.log(`DNS record for ${domain} is already up to date. No action required.`);
sendWebhookRequest({ status: 'success', message: `DNS record for ${domain} is already up to date. No action required.` });
} else {
if (dnsRecord.content !== publicIpAddress) {
console.log(`DNS record for ${DNS_URL} needs to be updated. IP Address: ${dnsRecord.content} -> ${publicIpAddress}`);
console.log(`DNS record for ${domain} needs to be updated. IP Address: ${dnsRecord.content} -> ${publicIpAddress}`);
}
if (dnsRecord.proxied !== PROXIED) {
console.log(`DNS record for ${DNS_URL} needs to be updated. Proxied: ${dnsRecord.proxied} -> ${PROXIED}`);
console.log(`DNS record for ${domain} 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}, Proxied: ${dnsRecord.proxied} -> ${PROXIED}` });
sendWebhookRequest({ status: 'progress', message: `DNS record for ${domain} needs to be updated. IP Address: ${dnsRecord.content} -> ${publicIpAddress}, Proxied: ${dnsRecord.proxied} -> ${PROXIED}` });

// Update the DNS record
const updatedDnsRecord = await updateCloudflareZoneRecord(zone.id, dnsRecord.id, {
Expand All @@ -90,20 +71,56 @@ const start = async () => {
proxied: 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}` });
console.log(`DNS record for ${domain} has been updated. IP Address: ${updatedDnsRecord.result.content}, Proxied: ${updatedDnsRecord.result.proxied}`);
sendWebhookRequest({ status: 'success', message: `DNS record for ${domain} 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...`);
console.log(`DNS record does not exist for ${domain}, creating it...`);
const newRecord = await createCloudflareZoneRecord(zone.id, {
type: 'A',
name: DNS_URL,
name: domain,
content: publicIpAddress,
proxied: PROXIED,
});

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(`DNS record for ${domain} has been created. IP Address: ${newRecord.result.content}`);
sendWebhookRequest({ status: 'progress', message: `DNS record for ${domain} has been created. IP Address: ${newRecord.result.content}` });
}
} catch (error) {
console.error('An error occurred while updating the domain', error);
}
};

const start = async () => {
try {
// Verify that the cloudflare API key is valid
const isCloudflareTokenValid = await verifyCloudflareToken();
if (!isCloudflareTokenValid) {
console.log('Cloudflare API key is invalid. Exiting. Sent API key: ', CLOUDFLARE_API_KEY);
job.stop();
return;
}

// Find out the TLD of the DNS_URL, check for multiple subdomains

const PARSED_TLDS = DNS_URL.split(',').map((dnsUrl) => getParsedTLD(dnsUrl.trim()));
console.log(`Parsed TLDs: ${PARSED_TLDS}`);

// Get the public IP address of the machine
const publicIpAddress = IP_OVERRIDE ? IP_OVERRIDE : await getPublicIpAddress();
if (IP_OVERRIDE) {
console.log(`IP Override: ${IP_OVERRIDE}, not checking public IP address.`);
} else {
if (!publicIpAddress) {
console.log('Unable to get public IP address.', publicIpAddress);
}
console.log(`Public IP Address: ${publicIpAddress}`);
}

// If the DNS_URL is a single domain, update the domain
for (const PARSED_TLD of PARSED_TLDS) {
console.log(`Updating domain: ${PARSED_TLD}`);
updateDomain(PARSED_TLD);
}

console.log('Completed, waiting for next scheduled run...\n');
Expand Down

0 comments on commit fab70c4

Please sign in to comment.