Files
cloudron-box/src/dyndns.js
T

58 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-01-25 14:03:42 -08:00
'use strict';
exports = module.exports = {
2021-05-01 11:21:09 -07:00
sync
2018-01-25 14:03:42 -08:00
};
2021-08-20 09:19:44 -07:00
const apps = require('./apps.js'),
2019-03-25 14:53:12 -07:00
assert = require('assert'),
2019-03-09 19:51:30 -08:00
constants = require('./constants.js'),
2018-01-25 14:03:42 -08:00
debug = require('debug')('box:dyndns'),
2021-08-13 17:22:28 -07:00
dns = require('./dns.js'),
2019-01-12 09:58:11 -08:00
eventlog = require('./eventlog.js'),
paths = require('./paths.js'),
safe = require('safetydance'),
2019-07-26 10:49:29 -07:00
settings = require('./settings.js'),
2021-08-27 09:52:24 -07:00
sysinfo = require('./sysinfo.js');
2018-01-25 14:03:42 -08:00
// called for dynamic dns setups where we have to update the IP
2021-08-27 09:52:24 -07:00
async function sync(auditSource) {
2019-03-25 14:53:12 -07:00
assert.strictEqual(typeof auditSource, 'object');
2018-01-25 14:03:42 -08:00
2022-01-06 17:02:16 -08:00
const ipv4 = await sysinfo.getServerIPv4();
2022-02-15 12:31:55 -08:00
const ipv6 = await sysinfo.getServerIPv6();
2022-01-06 17:02:16 -08:00
const info = safe.JSON.parse(safe.fs.readFileSync(paths.DYNDNS_INFO_FILE, 'utf8')) || { ipv4: null, ipv6: null };
if (info.ip) { // legacy cache file
info.ipv4 = info.ip;
delete info.ip;
}
2022-03-15 09:53:35 -07:00
const ipv4Changed = info.ipv4 !== ipv4;
2022-02-15 12:31:55 -08:00
const ipv6Changed = ipv6 && info.ipv6 !== ipv6; // both should be RFC 5952 format
2018-01-25 14:03:42 -08:00
2022-01-06 17:02:16 -08:00
if (!ipv4Changed && !ipv6Changed) {
debug(`refreshDNS: no change in IP ipv4: ${ipv4} ipv6: ${ipv6}`);
2021-08-27 09:52:24 -07:00
return;
}
2019-01-12 09:58:11 -08:00
2022-03-15 09:53:35 -07:00
debug(`refreshDNS: updating IP from ${info.ipv4} to ipv4: ${ipv4} (changed: ${ipv4Changed}) ipv6: ${ipv6} (changed: ${ipv6Changed})`);
2022-01-06 17:02:16 -08:00
if (ipv4Changed) await dns.upsertDnsRecords(constants.DASHBOARD_LOCATION, settings.dashboardDomain(), 'A', [ ipv4 ]);
if (ipv6Changed) await dns.upsertDnsRecords(constants.DASHBOARD_LOCATION, settings.dashboardDomain(), 'AAAA', [ ipv6 ]);
2018-01-25 14:03:42 -08:00
2021-08-27 09:52:24 -07:00
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;
2018-01-25 14:03:42 -08:00
2022-01-16 12:32:12 -08:00
if (ipv4Changed) await dns.upsertDnsRecords(app.subdomain, app.domain, 'A', [ ipv4 ]);
if (ipv6Changed) await dns.upsertDnsRecords(app.subdomain, app.domain, 'AAAA', [ ipv6 ]);
2021-08-27 09:52:24 -07:00
}
2018-01-25 14:03:42 -08:00
2021-08-27 09:52:24 -07:00
debug('refreshDNS: updated apps');
2018-01-25 14:03:42 -08:00
2022-01-06 17:02:16 -08:00
await eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, auditSource, { fromIpv4: info.ipv4, fromIpv6: info.ipv6, toIpv4: ipv4, toIpv6: ipv6 });
info.ipv4 = ipv4;
info.ipv6 = ipv6;
2021-08-27 09:52:24 -07:00
safe.fs.writeFileSync(paths.DYNDNS_INFO_FILE, JSON.stringify(info), 'utf8');
2018-01-25 14:03:42 -08:00
}