diff --git a/CHANGES b/CHANGES index 6459b42aa..a813fa95e 100644 --- a/CHANGES +++ b/CHANGES @@ -1516,4 +1516,5 @@ * Log events in the mail container * Fix issue where SpamAssassin and SPF checks were run for outbound email * Improve various eventlog messages +* Track dyndns change events diff --git a/src/dyndns.js b/src/dyndns.js index 33bc68062..16358158b 100644 --- a/src/dyndns.js +++ b/src/dyndns.js @@ -10,6 +10,9 @@ var appdb = require('./appdb.js'), config = require('./config.js'), debug = require('debug')('box:dyndns'), domains = require('./domains.js'), + eventlog = require('./eventlog.js'), + paths = require('./paths.js'), + safe = require('safetydance'), sysinfo = require('./sysinfo.js'); var NOOP_CALLBACK = function (error) { if (error) debug(error); }; @@ -21,12 +24,18 @@ function sync(callback) { sysinfo.getPublicIp(function (error, ip) { if (error) return callback(error); - debug('refreshDNS: current ip %s', ip); + 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}`); domains.upsertDnsRecords(config.adminLocation(), config.adminDomain(), 'A', [ ip ], function (error) { if (error) return callback(error); - debug('refreshDNS: done for admin location'); + debug('refreshDNS: updated admin location'); apps.getAll(function (error, result) { if (error) return callback(error); @@ -39,7 +48,11 @@ function sync(callback) { }, function (error) { if (error) return callback(error); - debug('refreshDNS: done for apps'); + debug('refreshDNS: updated apps'); + + eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, { userId: null, username: 'cron' }, { fromIp: info.ip, toIp: ip }); + info.ip = ip; + safe.fs.writeFileSync(paths.DYNDNS_INFO_FILE, JSON.stringify(info), 'utf8'); callback(); }); diff --git a/src/eventlog.js b/src/eventlog.js index a974543b2..abd5e686f 100644 --- a/src/eventlog.js +++ b/src/eventlog.js @@ -48,6 +48,8 @@ exports = module.exports = { ACTION_USER_REMOVE: 'user.remove', ACTION_USER_UPDATE: 'user.update', ACTION_USER_TRANSFER: 'user.transfer', + + ACTION_DYNDNS_UPDATE: 'dyndns.update' }; var assert = require('assert'), diff --git a/src/paths.js b/src/paths.js index 3db6fcfdd..9dfa4fd0f 100644 --- a/src/paths.js +++ b/src/paths.js @@ -23,6 +23,7 @@ exports = module.exports = { BACKUP_INFO_DIR: path.join(config.baseDir(), 'platformdata/backup'), UPDATE_DIR: path.join(config.baseDir(), 'platformdata/update'), SNAPSHOT_INFO_FILE: path.join(config.baseDir(), 'platformdata/backup/snapshot-info.json'), + DYNDNS_INFO_FILE: path.join(config.baseDir(), 'platformdata/dyndns-info.json'), // this is not part of appdata because an icon may be set before install APP_ICONS_DIR: path.join(config.baseDir(), 'boxdata/appicons'),