Add dynamic DNS: keep A records pointed at the current public IP
For deployments on WAN DHCP, operators can declare A records in the DNS section
that the app updates to this box's current public IP every 4 hours (and
immediately on create).
- utils/public_ip.js: getPublicIp() queries external echo services (ipify +
fallbacks, configurable) with pure isIPv4/extractIp helpers.
- utils/dns_records.js: pure planARecordUpdate() reconciliation decision.
- models/dns_provider.js: Domain.upsertARecord(name, ip) — provider-agnostic
upsert via getRecords + deleteRecordById + createRecord (createRecord alone is
not a reliable cross-provider upsert). Apex ('@') handling added to each
provider (CloudFlare uses the domain name, Porkbun an empty name, DigitalOcean
'@') via a new DnsApi.apexName().
- models/dynamic_record.js: DynamicRecord model (deterministic id per host,
apply()/refreshAll()), registered + ModelPs-wrapped for live UI updates.
- services/dynamic_dns.js + conf: 4h scheduler mirroring host_scheduler.
- routes/dns.js: /dynamic CRUD + /dynamic/ip, gated to domain managers/admins.
- views/dns.ejs: "Dynamic A Records (WAN IP)" card with add form + list.
- test/unit/dynamic_record.test.js: public-IP parsing + reconciliation logic.
Verified end-to-end against a live Porkbun domain (create, idempotent, IP-change,
cleanup) plus unit suite (111 pass).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const conf = require('@simpleworkjs/conf');
|
||||
const {DynamicRecord} = require('../models').models;
|
||||
|
||||
function dynamicDnsService(){
|
||||
/**
|
||||
* Dynamic DNS Service
|
||||
*
|
||||
* Keeps every declared DynamicRecord pointed at this deployment's current
|
||||
* public (WAN) IP — for sites on WAN DHCP where the IP changes.
|
||||
*
|
||||
* Schedule (conf.service.dynamicDns):
|
||||
* - Initial refresh shortly after start
|
||||
* - Recurring refresh every 4 hours (14400000ms)
|
||||
*
|
||||
* refreshAll resolves the public IP once, then reconciles each record's A
|
||||
* record via the domain's provider (create/replace only when it drifted).
|
||||
*/
|
||||
setTimeout(DynamicRecord.refreshAll.bind(DynamicRecord), conf.service.dynamicDns.initial);
|
||||
setInterval(DynamicRecord.refreshAll.bind(DynamicRecord), conf.service.dynamicDns.interval);
|
||||
|
||||
console.log('Dynamic DNS service initialized');
|
||||
console.log(`- Public IP refresh: ${conf.service.dynamicDns.initial / 1000}s after start, then every ${conf.service.dynamicDns.interval / 3600000}h`);
|
||||
}
|
||||
|
||||
if(conf.service.dynamicDns && conf.service.dynamicDns.enabled !== false) dynamicDnsService();
|
||||
|
||||
module.exports = {};
|
||||
Reference in New Issue
Block a user