Files
cloudron-box/src/dns/wildcard.js
T

102 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-09-06 20:26:24 -07:00
'use strict';
exports = module.exports = {
removePrivateFields: removePrivateFields,
injectPrivateFields: injectPrivateFields,
2018-09-06 20:26:24 -07:00
upsert: upsert,
get: get,
del: del,
2019-01-04 18:44:54 -08:00
wait: wait,
2018-09-06 20:26:24 -07:00
verifyDnsConfig: verifyDnsConfig
};
var assert = require('assert'),
2019-10-23 10:02:04 -07:00
BoxError = require('../boxerror.js'),
2018-09-06 20:26:24 -07:00
debug = require('debug')('box:dns/manual'),
dns = require('../native-dns.js'),
2019-01-04 18:44:54 -08:00
domains = require('../domains.js'),
2018-09-06 20:26:24 -07:00
sysinfo = require('../sysinfo.js'),
2019-01-04 18:44:54 -08:00
util = require('util'),
waitForDns = require('./waitfordns.js');
2018-09-06 20:26:24 -07:00
function removePrivateFields(domainObject) {
return domainObject;
}
2019-10-23 10:02:04 -07:00
// eslint-disable-next-line no-unused-vars
function injectPrivateFields(newConfig, currentConfig) {
}
2019-01-04 18:44:54 -08:00
function upsert(domainObject, location, type, values, callback) {
assert.strictEqual(typeof domainObject, 'object');
assert.strictEqual(typeof location, 'string');
2018-09-06 20:26:24 -07:00
assert.strictEqual(typeof type, 'string');
assert(util.isArray(values));
assert.strictEqual(typeof callback, 'function');
2019-01-04 18:44:54 -08:00
debug('upsert: %s for zone %s of type %s with values %j', location, domainObject.zoneName, type, values);
2018-09-06 20:26:24 -07:00
return callback(null);
}
2019-01-04 18:44:54 -08:00
function get(domainObject, location, type, callback) {
assert.strictEqual(typeof domainObject, 'object');
assert.strictEqual(typeof location, 'string');
2018-09-06 20:26:24 -07:00
assert.strictEqual(typeof type, 'string');
assert.strictEqual(typeof callback, 'function');
callback(null, [ ]); // returning ip confuses apptask into thinking the entry already exists
}
2019-01-04 18:44:54 -08:00
function del(domainObject, location, type, values, callback) {
assert.strictEqual(typeof domainObject, 'object');
assert.strictEqual(typeof location, 'string');
2018-09-06 20:26:24 -07:00
assert.strictEqual(typeof type, 'string');
assert(util.isArray(values));
assert.strictEqual(typeof callback, 'function');
return callback();
}
2019-01-04 18:44:54 -08:00
function wait(domainObject, location, type, value, options, callback) {
assert.strictEqual(typeof domainObject, 'object');
assert.strictEqual(typeof location, 'string');
assert.strictEqual(typeof type, 'string');
assert.strictEqual(typeof value, 'string');
assert(options && typeof options === 'object'); // { interval: 5000, times: 50000 }
assert.strictEqual(typeof callback, 'function');
const fqdn = domains.fqdn(location, domainObject);
waitForDns(fqdn, domainObject.zoneName, type, value, options, callback);
}
function verifyDnsConfig(domainObject, callback) {
assert.strictEqual(typeof domainObject, 'object');
2018-09-06 20:26:24 -07:00
assert.strictEqual(typeof callback, 'function');
2019-01-04 18:44:54 -08:00
const zoneName = domainObject.zoneName;
2018-09-06 20:26:24 -07:00
// Very basic check if the nameservers can be fetched
dns.resolve(zoneName, 'NS', { timeout: 5000 }, function (error, nameservers) {
2019-10-23 10:02:04 -07:00
if (error && error.code === 'ENOTFOUND') return callback(new BoxError(BoxError.BAD_FIELD, 'Unable to resolve nameservers for this domain', { field: 'nameservers' }));
if (error || !nameservers) return callback(new BoxError(BoxError.BAD_FIELD, error ? error.message : 'Unable to get nameservers', { field: 'nameservers' }));
2018-09-06 20:26:24 -07:00
2019-01-04 18:44:54 -08:00
const location = 'cloudrontestdns';
const fqdn = domains.fqdn(location, domainObject);
2018-09-06 20:26:24 -07:00
dns.resolve(fqdn, 'A', { server: '127.0.0.1', timeout: 5000 }, function (error, result) {
2019-10-23 10:02:04 -07:00
if (error && error.code === 'ENOTFOUND') return callback(new BoxError(BoxError.BAD_FIELD, `Unable to resolve ${fqdn}`, { field: 'nameservers' }));
if (error || !result) return callback(new BoxError(BoxError.BAD_FIELD, error ? error.message : `Unable to resolve ${fqdn}`, { field: 'nameservers' }));
2018-09-06 20:26:24 -07:00
sysinfo.getPublicIp(function (error, ip) {
2019-10-23 10:02:04 -07:00
if (error) return callback(new BoxError(BoxError.EXTERNAL_ERROR, `Failed to detect IP of this server: ${error.message}`));
2018-09-06 20:26:24 -07:00
2019-10-23 10:02:04 -07:00
if (result.length !== 1 || ip !== result[0]) return callback(new BoxError(BoxError.EXTERNAL_ERROR, `Domain resolves to ${JSON.stringify(result)} instead of ${ip}`));
2018-09-06 20:26:24 -07:00
2018-09-11 21:24:04 -07:00
callback(null, {});
2018-09-06 20:26:24 -07:00
});
});
});
}