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

107 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-12-14 13:19:12 -08:00
'use strict';
exports = module.exports = {
upsert: upsert,
get: get,
del: del,
2017-01-10 11:12:38 +01:00
waitForDns: require('./waitfordns.js'),
verifyDnsConfig: verifyDnsConfig
2016-12-14 13:19:12 -08:00
};
var assert = require('assert'),
2017-01-10 11:12:38 +01:00
async = require('async'),
2017-05-24 15:40:16 -07:00
constants = require('../constants.js'),
2017-02-19 20:30:35 -08:00
debug = require('debug')('box:dns/manual'),
2017-05-26 15:15:01 -07:00
dig = require('../dig.js'),
dns = require('dns'),
2016-12-14 13:19:12 -08:00
SubdomainError = require('../subdomains.js').SubdomainError,
util = require('util');
function upsert(dnsConfig, zoneName, subdomain, type, values, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
assert(util.isArray(values));
assert.strictEqual(typeof callback, 'function');
debug('upsert: %s for zone %s of type %s with values %j', subdomain, zoneName, type, values);
return callback(null, 'noop-record-id');
}
function get(dnsConfig, zoneName, subdomain, type, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
assert.strictEqual(typeof callback, 'function');
callback(null, [ ]); // returning ip confuses apptask into thinking the entry already exists
}
function del(dnsConfig, zoneName, subdomain, type, values, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof subdomain, 'string');
assert.strictEqual(typeof type, 'string');
assert(util.isArray(values));
assert.strictEqual(typeof callback, 'function');
return callback();
}
2017-01-10 11:12:38 +01:00
function verifyDnsConfig(dnsConfig, domain, ip, callback) {
assert.strictEqual(typeof dnsConfig, 'object');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof ip, 'string');
assert.strictEqual(typeof callback, 'function');
2017-05-24 15:40:16 -07:00
var adminDomain = constants.ADMIN_LOCATION + '.' + domain;
2017-01-10 12:26:14 +01:00
2017-01-10 11:12:38 +01:00
dns.resolveNs(domain, function (error, nameservers) {
2017-01-10 11:49:23 +01:00
if (error || !nameservers) return callback(new SubdomainError(SubdomainError.BAD_FIELD, 'Unable to get nameservers'));
2017-02-16 20:11:09 -08:00
async.every(nameservers, function (nameserver, everyNsCallback) {
2017-01-10 11:12:38 +01:00
// ns records cannot have cname
dns.resolve4(nameserver, function (error, nsIps) {
2017-01-10 11:49:23 +01:00
if (error || !nsIps || nsIps.length === 0) {
2017-02-16 20:11:09 -08:00
return everyNsCallback(new SubdomainError(SubdomainError.BAD_FIELD, 'Unable to resolve nameservers for this domain'));
2017-01-10 11:49:23 +01:00
}
2017-01-10 11:12:38 +01:00
2017-02-16 20:11:09 -08:00
async.every(nsIps, function (nsIp, everyIpCallback) {
2017-05-26 15:15:01 -07:00
dig.resolve(adminDomain, 'A', { server: nsIp, timeout: 5000 }, function (error, answer) {
if (error && error.code === 'ETIMEDOUT') {
debug('nameserver %s (%s) timed out when trying to resolve %s', nameserver, nsIp, adminDomain);
return everyIpCallback(null, true); // should be ok if dns server is down
}
2017-01-10 11:12:38 +01:00
if (error) {
2017-01-10 12:26:14 +01:00
debug('nameserver %s (%s) returned error trying to resolve %s: %s', nameserver, nsIp, adminDomain, error);
2017-02-16 20:11:09 -08:00
return everyIpCallback(null, false);
2017-01-10 11:12:38 +01:00
}
if (!answer || answer.length === 0) {
2017-05-26 15:15:01 -07:00
debug('bad answer from nameserver %s (%s) resolving %s (%s): %j', nameserver, nsIp, adminDomain, 'A', answer);
2017-02-16 20:11:09 -08:00
return everyIpCallback(null, false);
2017-01-10 11:12:38 +01:00
}
2017-01-10 12:26:14 +01:00
debug('verifyDnsConfig: ns: %s (%s), name:%s Actual:%j Expecting:%s', nameserver, nsIp, adminDomain, answer, ip);
2017-01-10 11:12:38 +01:00
2017-05-26 15:15:01 -07:00
var match = answer.some(function (a) { return a === ip; });
2017-01-10 11:12:38 +01:00
2017-02-16 20:11:09 -08:00
if (match) return everyIpCallback(null, true); // done!
2017-01-10 11:12:38 +01:00
2017-02-16 20:11:09 -08:00
everyIpCallback(null, false);
2017-01-10 11:12:38 +01:00
});
2017-02-16 20:11:09 -08:00
}, everyNsCallback);
2017-01-10 11:12:38 +01:00
});
2017-02-16 20:11:09 -08:00
}, function (error, success) {
if (error) return callback(error);
2017-01-12 11:48:03 -08:00
if (!success) return callback(new SubdomainError(SubdomainError.BAD_FIELD, 'The domain ' + adminDomain + ' does not resolve to the server\'s IP ' + ip));
2017-01-10 11:49:23 +01:00
callback(null, { provider: dnsConfig.provider, wildcard: !!dnsConfig.wildcard });
2017-01-10 11:12:38 +01:00
});
});
}