Files
cloudron-box/src/dyndns.js

63 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-01-25 14:03:42 -08:00
'use strict';
exports = module.exports = {
sync: sync
};
2019-08-30 13:12:49 -07:00
let apps = require('./apps.js'),
2019-03-25 14:53:12 -07:00
assert = require('assert'),
2018-01-25 14:03:42 -08:00
async = require('async'),
constants = require('./constants.js'),
2018-01-25 14:03:42 -08:00
debug = require('debug')('box:dyndns'),
domains = require('./domains.js'),
2019-01-12 09:58:11 -08:00
eventlog = require('./eventlog.js'),
paths = require('./paths.js'),
safe = require('safetydance'),
settings = require('./settings.js'),
2018-01-25 14:03:42 -08:00
sysinfo = require('./sysinfo.js');
// called for dynamic dns setups where we have to update the IP
2019-03-25 14:53:12 -07:00
function sync(auditSource, callback) {
assert.strictEqual(typeof auditSource, 'object');
assert.strictEqual(typeof callback, 'function');
2018-01-25 14:03:42 -08:00
2019-10-29 15:46:33 -07:00
sysinfo.getServerIp(function (error, ip) {
2018-01-25 14:03:42 -08:00
if (error) return callback(error);
2019-01-12 09:58:11 -08:00
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 callback();
}
debug(`refreshDNS: updating ip from ${info.ip} to ${ip}`);
2018-01-25 14:03:42 -08:00
domains.upsertDnsRecords(constants.ADMIN_LOCATION, settings.adminDomain(), 'A', [ ip ], function (error) {
2018-01-25 14:03:42 -08:00
if (error) return callback(error);
2019-01-12 09:58:11 -08:00
debug('refreshDNS: updated admin location');
2018-01-25 14:03:42 -08:00
apps.getAll(function (error, result) {
if (error) return callback(error);
async.each(result, function (app, callback) {
// do not change state of installing apps since apptask will error if dns record already exists
2019-08-30 13:12:49 -07:00
if (app.installationState !== apps.ISTATE_INSTALLED) return callback();
2018-01-25 14:03:42 -08:00
2018-02-08 12:05:29 -08:00
domains.upsertDnsRecords(app.location, app.domain, 'A', [ ip ], callback);
2018-01-25 14:03:42 -08:00
}, function (error) {
if (error) return callback(error);
2019-01-12 09:58:11 -08:00
debug('refreshDNS: updated apps');
2019-03-25 14:53:12 -07:00
eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, auditSource, { fromIp: info.ip, toIp: ip });
2019-01-12 09:58:11 -08:00
info.ip = ip;
safe.fs.writeFileSync(paths.DYNDNS_INFO_FILE, JSON.stringify(info), 'utf8');
2018-01-25 14:03:42 -08:00
callback();
});
});
});
});
}