Refactor and integrate ddns resolution with nginx module

Refactored ddns resolver so that no patching is done. nginx.js will automatically resolve ddns addresses if needed.

Added dedicated logger scope for ddns resovler.
This commit is contained in:
Varun Gupta
2023-12-02 19:13:47 -05:00
committed by Varun Gupta
parent 5586709d03
commit ec9eb0dd60
5 changed files with 80 additions and 82 deletions

View File

@ -4,6 +4,7 @@ const logger = require('../logger').nginx;
const config = require('../lib/config');
const utils = require('../lib/utils');
const error = require('../lib/error');
const ddnsResolver = require('../lib/ddns_resolver');
const internalNginx = {
@ -131,6 +132,31 @@ const internalNginx = {
return '/data/nginx/' + internalNginx.getFileFriendlyHostType(host_type) + '/' + host_id + '.conf';
},
/**
* Resolves any ddns addresses that need to be resolved for clients in the host's access list.
* @param {Object} host
* @returns {Promise}
*/
resolveDDNSAddresses: (host) => {
const promises = [];
if (typeof host.access_list !== 'undefined' && typeof host.access_list.clients !== 'undefined') {
for (const client of host.access_list.clients) {
if (ddnsResolver.requiresResolution(client.address)) {
const p = ddnsResolver.resolveAddress(client.address)
.then((resolvedIP) => {
client.address = `${resolvedIP}; # ${client.address}`;
return Promise.resolve();
});
promises.push(p);
}
}
}
if (promises.length) {
return Promise.all(promises);
}
return Promise.resolve();
},
/**
* Generates custom locations
* @param {Object} host
@ -201,6 +227,12 @@ const internalNginx = {
return;
}
// Resolve ddns addresses if needed
let resolverPromise = Promise.resolve();
if (host_type === 'proxy_host') {
resolverPromise = internalNginx.resolveDDNSAddresses(host);
}
let locationsPromise;
let origLocations;
@ -215,8 +247,10 @@ const internalNginx = {
if (host.locations) {
//logger.info ('host.locations = ' + JSON.stringify(host.locations, null, 2));
origLocations = [].concat(host.locations);
locationsPromise = internalNginx.renderLocations(host).then((renderedLocations) => {
host.locations = renderedLocations;
locationsPromise = resolverPromise.then(() => {
return internalNginx.renderLocations(host).then((renderedLocations) => {
host.locations = renderedLocations;
});
});
// Allow someone who is using / custom location path to use it, and skip the default / location
@ -227,7 +261,7 @@ const internalNginx = {
});
} else {
locationsPromise = Promise.resolve();
locationsPromise = resolverPromise;
}
// Set the IPv6 setting for the host