From 7b09fefd1799be082e0c140e446a6ee260719333 Mon Sep 17 00:00:00 2001 From: Varun Gupta Date: Sat, 2 Dec 2023 22:47:22 -0500 Subject: [PATCH] Update configs for active hosts only on ddns update Other changes: - Fixed null property read error on clients (when switching to public access) - Use separate `resolvedAddress` field for resolved IP instead of overwriting address - Reduced ddns log verbosity --- backend/internal/nginx.js | 9 +++++---- backend/lib/ddns_resolver/ddns_resolver.js | 4 +--- backend/lib/ddns_resolver/ddns_updater.js | 6 +++++- backend/lib/utils.js | 3 +++ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/backend/internal/nginx.js b/backend/internal/nginx.js index 3708808e..84cec0d5 100644 --- a/backend/internal/nginx.js +++ b/backend/internal/nginx.js @@ -139,12 +139,13 @@ const internalNginx = { */ resolveDDNSAddresses: (host) => { const promises = []; - if (typeof host.access_list !== 'undefined' && typeof host.access_list.clients !== 'undefined') { + if (typeof host.access_list !== 'undefined' && host.access_list && typeof host.access_list.clients !== 'undefined' && host.access_list.clients) { for (const client of host.access_list.clients) { - if (ddnsResolver.requiresResolution(client.address)) { - const p = ddnsResolver.resolveAddress(client.address) + const address = client.address; + if (ddnsResolver.requiresResolution(address)) { + const p = ddnsResolver.resolveAddress(address) .then((resolvedIP) => { - client.address = `${resolvedIP}; # ${client.address}`; + Object.defineProperty(client, 'resolvedAddress', {value: resolvedIP}); return Promise.resolve(); }); promises.push(p); diff --git a/backend/lib/ddns_resolver/ddns_resolver.js b/backend/lib/ddns_resolver/ddns_resolver.js index 0fa45e0e..38b6168b 100644 --- a/backend/lib/ddns_resolver/ddns_resolver.js +++ b/backend/lib/ddns_resolver/ddns_resolver.js @@ -64,15 +64,13 @@ const ddnsResolver = { * @returns {Promise} */ _queryHost: (host) => { - logger.info('Looking up IP for ', host); return utils.execSafe('getent', ['hosts', host]) .then((result) => { if (result.length < 8) { - logger.error('IP lookup returned invalid output: ', result); + logger.error(`IP lookup for ${host} returned invalid output: ${result}`); throw error.ValidationError('Invalid output from getent hosts'); } const out = result.split(/\s+/); - logger.info(`Resolved ${host} to ${out[0]}`); return out[0]; }, (error) => { diff --git a/backend/lib/ddns_resolver/ddns_updater.js b/backend/lib/ddns_resolver/ddns_updater.js index f00e588d..f67ed1a8 100644 --- a/backend/lib/ddns_resolver/ddns_updater.js +++ b/backend/lib/ddns_resolver/ddns_updater.js @@ -113,7 +113,11 @@ const ddnsUpdater = { for (const row of rows) { if (!updatedRows.has(row.id)) { updatedRows.set(row.id, 1); - proxy_hosts.push(...row.proxy_hosts); + for (const host of row.proxy_hosts) { + if (host.enabled) { + proxy_hosts.push(host); + } + } } } } diff --git a/backend/lib/utils.js b/backend/lib/utils.js index 94ed74ce..26a0ffdc 100644 --- a/backend/lib/utils.js +++ b/backend/lib/utils.js @@ -128,6 +128,9 @@ module.exports = { */ renderEngine.registerFilter('nginxAccessRule', (v) => { if (typeof v.directive !== 'undefined' && typeof v.address !== 'undefined' && v.directive && v.address) { + if (typeof v.resolvedAddress !== 'undefined' && v.resolvedAddress) { + return `${v.directive} ${v.resolvedAddress}; # ${v.address}`; + } return `${v.directive} ${v.address};`; } return '';