Files
proxy/nodejs/models/dns_provider/digitalocean.js
T
wmantly a2d194f855 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>
2026-07-10 23:55:56 -04:00

96 lines
3.0 KiB
JavaScript

'use strict';
const axios = require('axios');
const {DnsApi} = require('./common');
class DigitalOcean extends DnsApi{
static _keyMap = {
token: {isRequired: true, type: 'string', isPrivate: true, displayName: 'API Token'},
}
static displayName = 'DigitalOcean'
static displayIconHtml = `
<svg height="100%" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
<circle cx="512" cy="512" r="512" style="fill:#0080ff"/>
<path d="m273.8 669.2-.1-63.7h63.7v63.7h76v-98.8h98.8v98.5c105.1-.1 186.2-104.1 146.1-214.6-14.9-40.9-47.6-73.6-88.5-88.4-110.7-40.2-214.7 41.2-214.7 146.3H256c0-167.5 161.8-298 337.4-243.2 76.8 24 137.7 84.9 161.6 161.6C809.9 606.2 679.4 768 511.9 768v-98.8h-98.6v75.9h-75.9v-75.9h-63.6z" style="fill:#fff"/>
</svg>`
// '<i class="fa-brands fa-digital-ocean"></i>'
static displayIconUni = '&#xf391;'
constructor(token){
super()
this.token = token.token || token;
}
async axios(method, ...args){
try{
let a = axios.create({
baseURL: 'https://api.digitalocean.com/v2/',
headers: {Authorization: `Bearer ${this.token}`}
});
return await a[method](...args);
}catch(error){
if(!error.response) throw error;
if(error.response.data && error.response.data.id === 'Unauthorized'){
throw this.errors.unauthorized();
}
throw this.errors.other(error.response.status, error.response.data.message);
}
}
async listDomains(){
// DO returns {domains:[{name, ttl, zone_file}]} keyed by `name` and has no
// zone id (API paths use the domain name directly). Map name -> domain the
// way CloudFlare.listDomains does; do NOT run this through __parseRes, which
// rewrites `.name` to its subdomain and would leave no usable domain name.
let res = await this.axios('get', '/domains?per_page=200');
for(let domain of res.data.domains){
domain.domain = domain.name;
}
return res.data.domains;
}
async getRecords(domain, options){
options = this.__parseOptions(options);
let res = await this.axios('get', `/domains/${domain.domain}/records`, {params: options})
let records = this.__parseRes(res.data.domain_records);
if(!options) return records;
return records.filter((record)=>{
let matchCount = 0
for(let key in options){
if(record[key] === options[key] && ++matchCount === Object.keys(options).length){
return true;
}
}
});
}
async createRecord(domain, options){
// DigitalOcean names an apex record '@' (the base apexName default).
if(options.name === '@') options.name = this.apexName(domain.domain);
options = this.__parseOptions(options, ['type', 'name', 'data']);
let res = await this.axios('post', `/domains/${domain.domain}/records`, options);
return this.__parseRes([res.data.domain_record])[0];
}
async deleteRecordById(domain, id){
let res = await this.axios('delete', `/domains/${domain.domain}/records/${id}`);
}
async deleteRecords(domain, options){
let records = await this.getRecords(domain, options)
for(let record of records){
let res = await this.deleteRecordById(domain, record.id);
}
return true;
}
}
module.exports = DigitalOcean;