Files
cloudron-box/src/dyndns.js
T
Girish Ramakrishnan 96dc79cfe6 Migrate codebase from CommonJS to ES Modules
- Convert all require()/module.exports to import/export across 260+ files
- Add "type": "module" to package.json to enable ESM by default
- Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible
- Convert eslint.config.js to ESM with sourceType: "module"
- Replace __dirname/__filename with import.meta.dirname/import.meta.filename
- Replace require.main === module with process.argv[1] === import.meta.filename
- Remove 'use strict' directives (implicit in ESM)
- Convert dynamic require() in switch statements to static import lookup maps
  (dns.js, domains.js, backupformats.js, backupsites.js, network.js)
- Extract self-referencing exports.CONSTANT patterns into standalone const
  declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.)
- Lazify SERVICES object in services.js to avoid circular dependency TDZ issues
- Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests
- Add _setMockApp() to ldapserver.js for ESM-safe test mocking
- Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests
- Convert backupupload.js to use dynamic imports so --check exits before
  loading the module graph (which requires BOX_ENV)
- Update check-install to use ESM import for infra_version.js
- Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations)
- All 1315 tests passing

Migration stats (AI-assisted using Cursor with Claude):
- Wall clock time: ~3-4 hours
- Assistant completions: ~80-100
- Estimated token usage: ~1-2M tokens

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-14 15:11:45 +01:00

93 lines
4.0 KiB
JavaScript

import apps from './apps.js';
import assert from 'node:assert';
import * as dashboard from './dashboard.js';
import debugModule from 'debug';
import * as dns from './dns.js';
import eventlog from './eventlog.js';
import fs from 'node:fs';
import * as mailServer from './mailserver.js';
import * as network from './network.js';
import paths from './paths.js';
import safe from 'safetydance';
import tasks from './tasks.js';
const debug = debugModule('box:dyndns');
export {
refreshDns,
sync
};
// FIXME: this races with apptask. can result in a conflict if apptask is doing some dns operation and this code changes entries
async function refreshDns(auditSource) {
assert.strictEqual(typeof auditSource, 'object');
const ipv4 = await network.getIPv4();
const ipv6 = await network.getIPv6();
const info = safe.JSON.parse(safe.fs.readFileSync(paths.DYNDNS_INFO_FILE, 'utf8')) || { ipv4: null, ipv6: null };
const ipv4Changed = info.ipv4 !== ipv4;
const ipv6Changed = ipv6 && info.ipv6 !== ipv6; // both should be RFC 5952 format
if (!ipv4Changed && !ipv6Changed) {
debug(`refreshDns: no change in IP. ipv4: ${ipv4} ipv6: ${ipv6}`);
return;
}
debug(`refreshDns: updating IP from ${info.ipv4} to ipv4: ${ipv4} (changed: ${ipv4Changed}) ipv6: ${ipv6} (changed: ${ipv6Changed})`);
const taskId = await tasks.add(tasks.TASK_SYNC_DYNDNS, [ ipv4Changed ? ipv4 : null, ipv6Changed ? ipv6 : null, auditSource ]);
// background
tasks.startTask(taskId, {})
.then(async () => {
await eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, auditSource, { taskId, fromIpv4: info.ipv4, fromIpv6: info.ipv6, toIpv4: ipv4, toIpv6: ipv6 });
info.ipv4 = ipv4;
info.ipv6 = ipv6;
await fs.promises.writeFile(paths.DYNDNS_INFO_FILE, JSON.stringify(info), 'utf8');
})
.catch(async (error) => {
await eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, auditSource, { taskId, fromIpv4: info.ipv4, fromIpv6: info.ipv6, toIpv4: ipv4, toIpv6: ipv6, errorMessage: error.message });
});
return taskId;
}
async function sync(ipv4, ipv6, auditSource, progressCallback) {
assert(ipv4 === null || typeof ipv4 === 'string');
assert(ipv6 === null || typeof ipv6 === 'string');
assert.strictEqual(typeof auditSource, 'object');
assert.strictEqual(typeof progressCallback, 'function');
let percent = 5;
const { domain:dashboardDomain, fqdn:dashboardFqdn, subdomain:dashboardSubdomain } = await dashboard.getLocation();
progressCallback({ percent, message: `Updating dashboard location ${dashboardFqdn}`});
if (ipv4) await safe(dns.upsertDnsRecords(dashboardSubdomain, dashboardDomain, 'A', [ ipv4 ]), { debug });
if (ipv6) await safe(dns.upsertDnsRecords(dashboardSubdomain, dashboardDomain, 'AAAA', [ ipv6 ]), { debug });
const { domain:mailDomain, fqdn:mailFqdn, subdomain:mailSubdomain } = await mailServer.getLocation();
percent += 10;
progressCallback({ percent, message: `Updating mail location ${mailFqdn}`});
if (dashboardFqdn !== mailFqdn) {
if (ipv4) await safe(dns.upsertDnsRecords(mailSubdomain, mailDomain, 'A', [ ipv4 ]), { debug });
if (ipv6) await safe(dns.upsertDnsRecords(mailSubdomain, mailDomain, 'AAAA', [ ipv6 ]), { debug });
}
const result = await apps.list();
for (const app of result) {
percent += Math.round(90/result.length);
progressCallback({ percent, message: `Updating app ${app.fqdn}`});
const locations = [{ domain: app.domain, subdomain: app.subdomain }]
.concat(app.secondaryDomains)
.concat(app.redirectDomains)
.concat(app.aliasDomains);
for (const location of locations) {
if (ipv4) await safe(dns.upsertDnsRecords(location.subdomain, location.domain, 'A', [ ipv4 ]), { debug });
if (ipv6) await safe(dns.upsertDnsRecords(location.subdomain, location.domain, 'AAAA', [ ipv6 ], { debug }));
}
}
progressCallback({ percent: 100, message: 'refreshDNS: updated apps' });
}