2018-01-25 14:03:42 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2023-07-08 19:48:12 +05:30
|
|
|
refreshDns,
|
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'),
|
2023-08-11 19:41:05 +05:30
|
|
|
dashboard = require('./dashboard.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'),
|
2023-08-03 13:38:42 +05:30
|
|
|
network = require('./network.js'),
|
2019-01-12 09:58:11 -08:00
|
|
|
paths = require('./paths.js'),
|
|
|
|
|
safe = require('safetydance'),
|
2023-07-08 19:48:12 +05:30
|
|
|
tasks = require('./tasks.js');
|
2018-01-25 14:03:42 -08:00
|
|
|
|
2023-03-24 08:55:21 +01:00
|
|
|
// FIXME: this races with apptask. can result in a conflict if apptask is doing some dns operation and this code changes entries
|
2023-07-08 19:48:12 +05:30
|
|
|
async function refreshDns(auditSource) {
|
2019-03-25 14:53:12 -07:00
|
|
|
assert.strictEqual(typeof auditSource, 'object');
|
2018-01-25 14:03:42 -08:00
|
|
|
|
2023-08-03 13:38:42 +05:30
|
|
|
const ipv4 = await network.getIPv4();
|
|
|
|
|
const ipv6 = await network.getIPv6();
|
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 };
|
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) {
|
2023-07-08 19:48:12 +05:30
|
|
|
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
|
|
|
|
2023-07-08 19:48:12 +05:30
|
|
|
debug(`refreshDns: updating IP from ${info.ipv4} to ipv4: ${ipv4} (changed: ${ipv4Changed}) ipv6: ${ipv6} (changed: ${ipv6Changed})`);
|
|
|
|
|
|
|
|
|
|
// update dns in a separate task so we can have trackable logs
|
|
|
|
|
const taskId = await tasks.add(tasks.TASK_SYNC_DYNDNS, [ ipv4Changed ? ipv4 : null, ipv6Changed ? ipv6 : null, auditSource ]);
|
|
|
|
|
tasks.startTask(taskId, {}, async function (error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
await eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, auditSource, { taskId, fromIpv4: info.ipv4, fromIpv6: info.ipv6, toIpv4: ipv4, toIpv6: ipv6, errorMessage: error.message });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
safe.fs.writeFileSync(paths.DYNDNS_INFO_FILE, JSON.stringify(info), 'utf8');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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 = 10;
|
2023-08-11 19:41:05 +05:30
|
|
|
const { domain:dashboardDomain, fqdn:dashboardFqdn, subdomain:dashboardSubdomain } = await dashboard.getLocation();
|
|
|
|
|
progressCallback({ percent, message: `Updating dashboard domain ${dashboardFqdn}`});
|
|
|
|
|
if (ipv4) await safe(dns.upsertDnsRecords(dashboardSubdomain, dashboardDomain, 'A', [ ipv4 ]), { debug });
|
|
|
|
|
if (ipv6) await safe(dns.upsertDnsRecords(dashboardSubdomain, dashboardDomain, 'AAAA', [ ipv6 ]), { debug });
|
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) {
|
2023-07-08 19:48:12 +05:30
|
|
|
percent += Math.round(90/result.length);
|
|
|
|
|
progressCallback({ percent, message: `Updating app ${app.fqdn}`});
|
|
|
|
|
|
2023-02-08 23:07:53 +01:00
|
|
|
const locations = [{ domain: app.domain, subdomain: app.subdomain }]
|
2023-02-08 23:16:48 +01:00
|
|
|
.concat(app.secondaryDomains)
|
|
|
|
|
.concat(app.redirectDomains)
|
|
|
|
|
.concat(app.aliasDomains);
|
2023-02-08 23:07:53 +01:00
|
|
|
|
|
|
|
|
for (const location of locations) {
|
2023-07-09 08:09:36 +05:30
|
|
|
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 }));
|
2023-02-08 23:07:53 +01:00
|
|
|
}
|
2021-08-27 09:52:24 -07:00
|
|
|
}
|
2018-01-25 14:03:42 -08:00
|
|
|
|
2023-07-08 19:48:12 +05:30
|
|
|
progressCallback({ percent: 100, message: 'refreshDNS: updated apps' });
|
2018-01-25 14:03:42 -08:00
|
|
|
}
|