async-ify, handle vanity ns, bugfix some ns logic

This commit is contained in:
AJ ONeal 2017-10-09 15:54:18 -06:00
parent 717fb1fe3e
commit 691f5a4a15
2 changed files with 118 additions and 102 deletions

View File

@ -9,9 +9,8 @@ module.exports.ask = function (query, cb) {
var NOERROR = 0;
var NXDOMAIN = 3;
var REFUSED = 5;
var primaryNameservers = [ 'localhost' ];
function getRecords(db, qname) {
function getRecords(db, qname, cb) {
var myRecords = db.records.filter(function (r) {
if ('string' !== typeof r.domain) {
return false;
@ -24,7 +23,9 @@ function getRecords(db, qname) {
}
});
return myRecords;
process.nextTick(function () {
cb(null, myRecords);
});
}
function dbToResourceRecord(r) {
@ -70,13 +71,17 @@ function getNs(db, ds, results, cb) {
var d = ds.shift();
if (!d) {
results.rcode = NXDOMAIN;
results.header.rcode = NXDOMAIN;
cb(null, results);
return;
}
var qn = d.id.toLowerCase();
getRecords(db, qn).forEach(function (r) {
return getRecords(db, qn, function (err, records) {
if (err) { cb(err); return; }
records.forEach(function (r) {
if ('NS' !== r.type) {
return;
}
@ -98,24 +103,30 @@ function getNs(db, ds, results, cb) {
results.authority.push(ns);
});
if (results.authority.length) {
if (!results.authority.length) {
return getNs(db, ds, results, cb);
}
// d.vanityNs should only be vanity nameservers (pointing to this same server)
if (d.vanityNs || results.authority.some(function (ns) {
console.log('[debug] ns', ns);
return -1 !== db.primaryNameservers.indexOf(ns.data.toLowerCase());
})) {
results.authority.length = 0;
results.authority.push(domainToSoa(db, d));
results.header.rcode = NXDOMAIN;
}
cb(null, results);
return;
});
}
results.header.rcode = NXDOMAIN;
cb(null, results);
}
function getSoa(db, domain, results, cb) {
console.log('[DEV] getSoa entered');
var nameservers = domain.nameservers || primaryNameservers;
function domainToSoa(db, domain) {
var nameservers = domain.vanityNs || db.primaryNameservers;
var index = Math.floor(Math.random() * nameservers.length) % nameservers.length;
var nameserver = nameservers[index];
results.authority.push({
return {
name: domain.id
, typeName: 'SOA'
, className: 'IN'
@ -148,7 +159,13 @@ function getSoa(db, domain, results, cb) {
// minimum -- how long to cache a non-existent domain (also the default ttl for BIND)
, minimum: domain.minimum || 5
, nx: domain.minimum || 5
});
};
}
function getSoa(db, domain, results, cb) {
console.log('[DEV] getSoa entered');
results.authority.push(domainToSoa(db, domain));
cb(null, results);
return;
@ -205,7 +222,8 @@ module.exports.query = function (input, query, cb) {
, question: [ query.question[0] ], answer: [], authority: [], additional: []
};
var myRecords = getRecords(db, qname);
return getRecords(db, qname, function (err, myRecords) {
if (err) { cb(err); return; }
if (myRecords.length) {
myRecords.forEach(function (r) {
@ -269,20 +287,17 @@ module.exports.query = function (input, query, cb) {
if (err) { cb(err, results); return; }
// has NS records
if (NXDOMAIN !== results.header.rcode) { console.log(results); cb(null, results); return; }
// has NS records (or SOA record if NS records match the server itself)
if (results.authority.length) {
console.log(results); cb(null, results); return;
}
// myDomains was sorted such that the longest was first
getSoa(db, myDomains[0], results, cb);
});
}
/*
query.question.forEach(function (q) {
module.exports.ask(q);
});
*/
};
}());

View File

@ -1,10 +1,11 @@
'use strict';
module.exports = {
"domains": [
"primaryNameservers": [ 'localhost' ] // 'ns1.redirect-www.org'
, "domains": [
{ "id": "daplie.me", "revokedAt": 0 }
, { "id": "oneal.daplie.me", "revokedAt": 0 }
, { "id": "aj.oneal.daplie.me", "revokedAt": 0 }
, { "id": "aj.oneal.daplie.me", "revokedAt": 0, "vanityNs": [ 'ns1.daplie.me', 'ns2.daplie.me' ] }
]
, "records": [
// zone daplie.me should be able to have some records on its own