Skip to content

Commit 6033b30

Browse files
committed
Merge remote-tracking branch 'origin-ddns/access-list-client-ddns-support' into develop
2 parents 79d28f0 + e317900 commit 6033b30

File tree

7 files changed

+338
-10
lines changed

7 files changed

+338
-10
lines changed

backend/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ async function appStart () {
99
const app = require('./app');
1010
const internalCertificate = require('./internal/certificate');
1111
const internalIpRanges = require('./internal/ip_ranges');
12+
const ddnsUpdater = require('./lib/ddns_resolver/ddns_updater');
1213

1314
return migrate.latest()
1415
.then(setup)
@@ -17,6 +18,7 @@ async function appStart () {
1718
.then(() => {
1819
internalCertificate.initTimer();
1920
internalIpRanges.initTimer();
21+
ddnsUpdater.initTimer();
2022

2123
const server = app.listen(3000, () => {
2224
logger.info('Backend PID ' + process.pid + ' listening on port 3000 ...');

backend/internal/nginx.js

+45-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const _ = require('lodash');
2-
const fs = require('fs');
3-
const logger = require('../logger').nginx;
4-
const config = require('../lib/config');
5-
const utils = require('../lib/utils');
6-
const error = require('../lib/error');
1+
const _ = require('lodash');
2+
const fs = require('fs');
3+
const logger = require('../logger').nginx;
4+
const config = require('../lib/config');
5+
const utils = require('../lib/utils');
6+
const error = require('../lib/error');
7+
const ddnsResolver = require('../lib/ddns_resolver/ddns_resolver');
78

89
const internalNginx = {
910

@@ -131,6 +132,33 @@ const internalNginx = {
131132
return '/data/nginx/' + internalNginx.getFileFriendlyHostType(host_type) + '/' + host_id + '.conf';
132133
},
133134

135+
/**
136+
* Resolves any ddns addresses that need to be resolved for clients in the host's access list.
137+
* Defines a new property 'resolvedAddress' on each client in `host.access_list.clients` that uses a ddns address.
138+
* @param {Object} host
139+
* @returns {Promise}
140+
*/
141+
resolveDDNSAddresses: (host) => {
142+
const promises = [];
143+
if (typeof host.access_list !== 'undefined' && host.access_list && typeof host.access_list.clients !== 'undefined' && host.access_list.clients) {
144+
for (const client of host.access_list.clients) {
145+
const address = client.address;
146+
if (ddnsResolver.requiresResolution(address)) {
147+
const p = ddnsResolver.resolveAddress(address)
148+
.then((resolvedIP) => {
149+
Object.defineProperty(client, 'resolvedAddress', {value: resolvedIP});
150+
return Promise.resolve();
151+
});
152+
promises.push(p);
153+
}
154+
}
155+
}
156+
if (promises.length) {
157+
return Promise.all(promises);
158+
}
159+
return Promise.resolve();
160+
},
161+
134162
/**
135163
* Generates custom locations
136164
* @param {Object} host
@@ -203,6 +231,12 @@ const internalNginx = {
203231
return;
204232
}
205233

234+
// Resolve ddns addresses if needed
235+
let resolverPromise = Promise.resolve();
236+
if (host_type === 'proxy_host') {
237+
resolverPromise = internalNginx.resolveDDNSAddresses(host);
238+
}
239+
206240
let locationsPromise;
207241
let origLocations;
208242

@@ -217,8 +251,10 @@ const internalNginx = {
217251
if (host.locations) {
218252
//logger.info ('host.locations = ' + JSON.stringify(host.locations, null, 2));
219253
origLocations = [].concat(host.locations);
220-
locationsPromise = internalNginx.renderLocations(host).then((renderedLocations) => {
221-
host.locations = renderedLocations;
254+
locationsPromise = resolverPromise.then(() => {
255+
return internalNginx.renderLocations(host).then((renderedLocations) => {
256+
host.locations = renderedLocations;
257+
});
222258
});
223259

224260
// Allow someone who is using / custom location path to use it, and skip the default / location
@@ -229,7 +265,7 @@ const internalNginx = {
229265
});
230266

231267
} else {
232-
locationsPromise = Promise.resolve();
268+
locationsPromise = resolverPromise;
233269
}
234270

235271
// Set the IPv6 setting for the host
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const error = require('../error');
2+
const logger = require('../../logger').ddns;
3+
const utils = require('../utils');
4+
5+
const ddnsResolver = {
6+
/**
7+
* Checks whether the address requires resolution (i.e. starts with ddns:)
8+
* @param {String} address
9+
* @returns {boolean}
10+
*/
11+
requiresResolution: (address) => {
12+
if (typeof address !== 'undefined' && address && address.toLowerCase().startsWith('ddns:')) {
13+
return true;
14+
}
15+
return false;
16+
},
17+
18+
/**
19+
* Resolves the given address to its IP
20+
* @param {String} address
21+
* @param {boolean} forceUpdate: whether to force resolution instead of using the cached value
22+
*/
23+
resolveAddress: (address, forceUpdate=false) => {
24+
if (!forceUpdate && ddnsResolver._cache.has(address)) {
25+
// Check if it is still valid
26+
const value = ddnsResolver._cache.get(address);
27+
const ip = value[0];
28+
const lastUpdated = value[1];
29+
const nowSeconds = Date.now();
30+
const delta = nowSeconds - lastUpdated;
31+
if (delta < ddnsResolver._updateIntervalMs) {
32+
return Promise.resolve(ip);
33+
}
34+
}
35+
ddnsResolver._cache.delete(address);
36+
// Reach here only if cache value doesn't exist or needs to be updated
37+
let host = address.toLowerCase();
38+
if (host.startsWith('ddns:')) {
39+
host = host.substring(5);
40+
}
41+
return ddnsResolver._queryHost(host)
42+
.then((resolvedIP) => {
43+
ddnsResolver._cache.set(address, [resolvedIP, Date.now()]);
44+
return resolvedIP;
45+
})
46+
.catch((/*error*/) => {
47+
// return input address in case of failure
48+
return address;
49+
});
50+
},
51+
52+
53+
/** Private **/
54+
// Properties
55+
/**
56+
* cache mapping host to (ip address, last updated time)
57+
*/
58+
_cache: new Map(),
59+
60+
// Methods
61+
/**
62+
*
63+
* @param {String} host
64+
* @returns {Promise}
65+
*/
66+
_queryHost: (host) => {
67+
return utils.execSafe('getent', ['hosts', host])
68+
.then((result) => {
69+
if (result.length < 8) {
70+
logger.error(`IP lookup for ${host} returned invalid output: ${result}`);
71+
throw error.ValidationError('Invalid output from getent hosts');
72+
}
73+
const out = result.split(/\s+/);
74+
return out[0];
75+
},
76+
(error) => {
77+
logger.error('Error looking up IP for ' + host + ': ', error);
78+
throw error;
79+
});
80+
},
81+
};
82+
83+
module.exports = ddnsResolver;
+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
const internalNginx = require('../../internal/nginx');
2+
const logger = require('../../logger').ddns;
3+
const internalAccessList = require('../../internal/access-list');
4+
const ddnsResolver = require('./ddns_resolver');
5+
6+
const ddnsUpdater = {
7+
/**
8+
* Starts a timer to periodically check for ddns updates
9+
*/
10+
initTimer: () => {
11+
ddnsUpdater._initialize();
12+
ddnsUpdater._interval = setInterval(ddnsUpdater._checkForDDNSUpdates, ddnsUpdater._updateIntervalMs);
13+
logger.info(`DDNS Update Timer initialized (interval: ${Math.floor(ddnsUpdater._updateIntervalMs / 1000)}s)`);
14+
// Trigger a run so that initial cache is populated and hosts can be updated - delay by 10s to give server time to boot up
15+
setTimeout(ddnsUpdater._checkForDDNSUpdates, 10 * 1000);
16+
},
17+
18+
/** Private **/
19+
// Properties
20+
_initialized: false,
21+
_updateIntervalMs: 60 * 60 * 1000, // 1 hr default (overriden with $DDNS_UPDATE_INTERVAL env var)
22+
_interval: null, // reference to created interval id
23+
_processingDDNSUpdate: false,
24+
25+
// Methods
26+
27+
_initialize: () => {
28+
if (ddnsUpdater._initialized) {
29+
return;
30+
}
31+
// Init the resolver
32+
// Read and set custom update interval from env if needed
33+
if (typeof process.env.DDNS_UPDATE_INTERVAL !== 'undefined') {
34+
const interval = Number(process.env.DDNS_UPDATE_INTERVAL.toLowerCase());
35+
if (!isNaN(interval)) {
36+
// Interval value from env is in seconds. Set min to 60s.
37+
ddnsUpdater._updateIntervalMs = Math.max(interval * 1000, 60 * 1000);
38+
} else {
39+
logger.warn(`[DDNS] invalid value for update interval: '${process.env.DDNS_UPDATE_INTERVAL}'`);
40+
}
41+
}
42+
ddnsUpdater._initialized = true;
43+
},
44+
45+
/**
46+
* Triggered by a timer, will check for and update ddns hosts in access list clients
47+
*/
48+
_checkForDDNSUpdates: () => {
49+
logger.info('Checking for DDNS updates...');
50+
if (!ddnsUpdater._processingDDNSUpdate) {
51+
ddnsUpdater._processingDDNSUpdate = true;
52+
53+
const updatedAddresses = new Map();
54+
55+
// Get all ddns hostnames in use
56+
return ddnsUpdater._getAccessLists()
57+
.then((rows) => {
58+
// Build map of used addresses that require resolution
59+
const usedAddresses = new Map();
60+
for (const row of rows) {
61+
if (!row.proxy_host_count) {
62+
// Ignore rows (access lists) that are not associated to any hosts
63+
continue;
64+
}
65+
for (const client of row.clients) {
66+
if (!ddnsResolver.requiresResolution(client.address)) {
67+
continue;
68+
}
69+
if (!usedAddresses.has(client.address)) {
70+
usedAddresses.set(client.address, [row]);
71+
} else {
72+
usedAddresses.get(client.address).push(row);
73+
}
74+
}
75+
}
76+
logger.info(`Found ${usedAddresses.size} address(es) in use.`);
77+
// Remove unused addresses
78+
const addressesToRemove = [];
79+
for (const address of ddnsResolver._cache.keys()) {
80+
if (!usedAddresses.has(address)) {
81+
addressesToRemove.push(address);
82+
}
83+
}
84+
addressesToRemove.forEach((address) => { ddnsResolver._cache.delete(address); });
85+
86+
const promises = [];
87+
88+
for (const [address, rows] of usedAddresses) {
89+
let oldIP = '';
90+
if (ddnsResolver._cache.has(address)) {
91+
oldIP = ddnsResolver._cache.get(address)[0];
92+
}
93+
const p = ddnsResolver.resolveAddress(address, true)
94+
.then((resolvedIP) => {
95+
if (resolvedIP !== address && resolvedIP !== oldIP) {
96+
// Mark this as an updated address
97+
updatedAddresses.set(address, rows);
98+
}
99+
});
100+
promises.push(p);
101+
}
102+
103+
if (promises.length) {
104+
return Promise.all(promises);
105+
}
106+
return Promise.resolve();
107+
})
108+
.then(() => {
109+
logger.info(`${updatedAddresses.size} DDNS IP(s) updated.`);
110+
const updatedRows = new Map();
111+
const proxy_hosts = [];
112+
for (const rows of updatedAddresses.values()) {
113+
for (const row of rows) {
114+
if (!updatedRows.has(row.id)) {
115+
updatedRows.set(row.id, 1);
116+
for (const host of row.proxy_hosts) {
117+
if (host.enabled) {
118+
proxy_hosts.push(host);
119+
}
120+
}
121+
}
122+
}
123+
}
124+
if (proxy_hosts.length) {
125+
logger.info(`Updating ${proxy_hosts.length} proxy host(s) affected by DDNS changes`);
126+
return internalNginx.bulkGenerateConfigs('proxy_host', proxy_hosts)
127+
.then(internalNginx.reload);
128+
}
129+
return Promise.resolve();
130+
})
131+
.then(() => {
132+
logger.info('Finished checking for DDNS updates');
133+
ddnsUpdater._processingDDNSUpdate = false;
134+
});
135+
} else {
136+
logger.info('Skipping since previous DDNS update check is in progress');
137+
}
138+
},
139+
140+
_getAccessLists: () => {
141+
const fakeAccess = {
142+
can: (/*role*/) => {
143+
return Promise.resolve({
144+
permission_visibility: 'all'
145+
});
146+
}
147+
};
148+
149+
return internalAccessList.getAll(fakeAccess)
150+
.then((rows) => {
151+
const promises = [];
152+
for (const row of rows) {
153+
const p = internalAccessList.get(fakeAccess, {
154+
id: row.id,
155+
expand: ['owner', 'items', 'clients', 'proxy_hosts.[certificate,access_list.[clients,items]]']
156+
}, true /* <- skip masking */);
157+
promises.push(p);
158+
}
159+
if (promises.length) {
160+
return Promise.all(promises);
161+
}
162+
return Promise.resolve([]);
163+
});
164+
}
165+
};
166+
167+
module.exports = ddnsUpdater;

0 commit comments

Comments
 (0)