UNPKG

cloudflare-ddns-sync

Version:

A simple module to update DNS records on Cloudflare whenever you want

59 lines (58 loc) 2.08 kB
import CloudflareClient from './lib/cloudflare-client.js'; import Cron from './lib/cron.js'; import ipUtils from './lib/ip-utils.js'; export default class CloudflareDDNSSync { cloudflareClient; constructor(auth) { this.cloudflareClient = new CloudflareClient(auth); } getIp() { return ipUtils.getIpv4(); } getIpv6() { return ipUtils.getIpv6(); } getRecordDataForDomain(domain) { return this.cloudflareClient.getRecordDataForDomain(domain); } getRecordDataForDomains(domains) { return this.cloudflareClient.getRecordDataForDomains(domains); } getRecordDataForRecord(record) { return this.cloudflareClient.getRecordDataForRecord(record); } getRecordDataForRecords(records) { return this.cloudflareClient.getRecordDataForRecords(records); } removeRecord(recordName, recordType) { return this.cloudflareClient.removeRecordByNameAndType(recordName, recordType); } stopSyncOnIpChange(changeListenerId) { ipUtils.removeIpChangeListener(changeListenerId); } syncByCronTime(cronExpression, records, callback, ip) { return Cron.createCronJob(cronExpression, async () => { const result = await this.syncRecords(records, ip); callback(result); }); } async syncOnIpChange(records, callback) { const changeListenerId = await ipUtils.addIpChangeListener(async (ip) => { const result = await this.syncRecords(records, ip); callback(result); }); // Sync records to make sure the current ip is already set. const currentIp = await ipUtils.getIpv4(); this.syncRecords(records, currentIp).then((syncedRecords) => { callback(syncedRecords); }); return changeListenerId; } syncRecord(record, ip) { return this.cloudflareClient.syncRecord(record, ip); } syncRecords(records, ip) { return this.cloudflareClient.syncRecords(records, ip); } } export * from './types/index.js';