Files
cloudron-box/src/dyndns.js
T
2022-01-05 18:08:15 -08:00

49 lines
1.6 KiB
JavaScript

'use strict';
exports = module.exports = {
sync
};
const apps = require('./apps.js'),
assert = require('assert'),
constants = require('./constants.js'),
debug = require('debug')('box:dyndns'),
dns = require('./dns.js'),
eventlog = require('./eventlog.js'),
paths = require('./paths.js'),
safe = require('safetydance'),
settings = require('./settings.js'),
sysinfo = require('./sysinfo.js');
// called for dynamic dns setups where we have to update the IP
async function sync(auditSource) {
assert.strictEqual(typeof auditSource, 'object');
const ip = await sysinfo.getServerIPv4();
let info = safe.JSON.parse(safe.fs.readFileSync(paths.DYNDNS_INFO_FILE, 'utf8')) || { ip: null };
if (info.ip === ip) {
debug(`refreshDNS: no change in IP ${ip}`);
return;
}
debug(`refreshDNS: updating ip from ${info.ip} to ${ip}`);
await dns.upsertDnsRecords(constants.DASHBOARD_LOCATION, settings.dashboardDomain(), 'A', [ ip ]);
debug('refreshDNS: updated admin location');
const result = await apps.list();
for (const app of result) {
// do not change state of installing apps since apptask will error if dns record already exists
if (app.installationState !== apps.ISTATE_INSTALLED) continue;
await dns.upsertDnsRecords(app.location, app.domain, 'A', [ ip ]);
}
debug('refreshDNS: updated apps');
await eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, auditSource, { fromIp: info.ip, toIp: ip });
info.ip = ip;
safe.fs.writeFileSync(paths.DYNDNS_INFO_FILE, JSON.stringify(info), 'utf8');
}