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

View File

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