small refactor

This commit is contained in:
AJ ONeal 2018-01-17 01:03:05 -07:00
parent 8d329b93b2
commit fa45842f9f
3 changed files with 61 additions and 55 deletions

View File

@ -16,7 +16,7 @@ function getRecords(engine, qname, cb) {
// SECURITY XXX TODO var dig = require('dig.js/dns-request'); // SECURITY XXX TODO var dig = require('dig.js/dns-request');
var count; var count;
return engine.getRecords({ name: qname }, function (err, myRecords) { return engine.records.get({ name: qname }, function (err, myRecords) {
if (err) { cb(err); return; } if (err) { cb(err); return; }
function checkCount() { function checkCount() {
@ -188,7 +188,7 @@ function getNs(engine, ds, results, cb) {
return -1 !== engine.primaryNameservers.indexOf(ns.data.toLowerCase()); return -1 !== engine.primaryNameservers.indexOf(ns.data.toLowerCase());
})) { })) {
results.authority.length = 0; results.authority.length = 0;
results.authority.push(domainToSoa(engine.primaryNameservers, d)); results.authority.push(engine.zoneToSoa(d));
results.header.rcode = NXDOMAIN; results.header.rcode = NXDOMAIN;
} }
cb(null, results); cb(null, results);
@ -196,54 +196,13 @@ function getNs(engine, ds, results, cb) {
}); });
} }
function domainToSoa(primaryNameservers, domain) { function getSoa(engine, domain, results, cb, answerSoa) {
var nameservers = domain.vanityNs || primaryNameservers;
var index = Math.floor(Math.random() * nameservers.length) % nameservers.length;
var nameserver = nameservers[index];
return {
name: domain.id
, typeName: 'SOA'
, className: 'IN'
, ttl: domain.ttl || 60
// nameserver -- select an NS at random if they're all in sync
, primary: nameserver
, name_server: nameserver
// admin -- email address or domain for admin
, admin: domain.admin || ('admin.' + domain.id)
, email_addr: domain.admin || ('admin.' + domain.id)
// serial -- the version, for cache-busting of secondary nameservers. suggested format: YYYYMMDDnn
, serial: domain.serial || Math.round((domain.updatedAt || domain.createdAt || 0) / 1000)
, sn: domain.serial || Math.round((domain.updatedAt || domain.createdAt || 0) / 1000)
// refresh -- only used when nameservers following the DNS NOTIFY spec talk
, refresh: domain.refresh || 1800
, ref: domain.refresh || 1800
// retry -- only used when nameservers following the DNS NOTIFY spec talk
, retry: domain.retry || 600
, ret: domain.retry || 600
// expiration -- how long other nameservers should continue when the primary goes down
, expiration: domain.expiration || 2419200
, ex: domain.expiration || 2419200
// 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(primaryNameservers, domain, results, cb, answerSoa) {
console.log('[DEV] getSoa entered'); console.log('[DEV] getSoa entered');
if (!answerSoa) { if (!answerSoa) {
results.authority.push(domainToSoa(primaryNameservers, domain)); results.authority.push(engine.zoneToSoa(domain));
} else { } else {
results.answer.push(domainToSoa(primaryNameservers, domain)); results.answer.push(engine.zoneToSoa(domain));
} }
cb(null, results); cb(null, results);
@ -326,7 +285,8 @@ module.exports.query = function (engine, query, cb) {
console.log('[DEV] qnames'); console.log('[DEV] qnames');
console.log(qnames); console.log(qnames);
return engine.getSoas({ names: qnames}, function (err, myDomains) { // getSoas
return engine.zones.get({ names: qnames }, function (err, myDomains) {
console.log('[SOA] looking for', qnames, 'and proudly serving', err, myDomains); console.log('[SOA] looking for', qnames, 'and proudly serving', err, myDomains);
if (err) { cb(err); return; } if (err) { cb(err); return; }
@ -350,7 +310,7 @@ module.exports.query = function (engine, query, cb) {
//console.log('sorted domains', myDomains); //console.log('sorted domains', myDomains);
if (!getNsAlso) { if (!getNsAlso) {
return getSoa(engine.primaryNameservers, myDomains[0], results, cb, answerSoa); return getSoa(engine, myDomains[0], results, cb, answerSoa);
} }
return getNs(engine, /*myDomains.slice(0)*/qnames.map(function (qn) { return { id: qn }; }), results, function (err, results) { return getNs(engine, /*myDomains.slice(0)*/qnames.map(function (qn) { return { id: qn }; }), results, function (err, results) {
@ -364,7 +324,7 @@ module.exports.query = function (engine, query, cb) {
} }
// myDomains was sorted such that the longest was first // myDomains was sorted such that the longest was first
return getSoa(engine.primaryNameservers, myDomains[0], results, cb); return getSoa(engine, myDomains[0], results, cb);
}); });
}); });

View File

@ -194,10 +194,18 @@ module.exports.create = function (cli, engine/*, dnsd*/) {
}; };
} }
app.get('/api/zones/:zone/records', function (req, res) { app.get('/api/zones/:zone/records', function (req, res) {
engine.records.all(function (err, records) { var zonename = req.params.zone;
res.send({ records: records.filter(function (r) { engine.zones.get({ names: [ zonename ] }, function (err, zones) {
return r.zone === req.params.zone; var zone = engine.zoneToSoa(zones[0]);
}).map(mapRecord) }); zone.class = zone.className;
zone.type = zone.typeName;
engine.records.all(function (err, records) {
records = records.filter(function (r) {
return r.zone === zonename;
}).map(mapRecord);
records.unshift(zone);
res.send({ records: records });
});
}); });
}); });
app.get('/api/records', function (req, res) { app.get('/api/records', function (req, res) {

View File

@ -7,6 +7,46 @@ module.exports.create = function (opts) {
var db = require(opts.filepath); var db = require(opts.filepath);
engine.primaryNameservers = db.primaryNameservers; engine.primaryNameservers = db.primaryNameservers;
engine.zoneToSoa = function (domain) {
var nameservers = domain.vanityNs || engine.primaryNameservers;
var index = Math.floor(Math.random() * nameservers.length) % nameservers.length;
var nameserver = nameservers[index];
return {
name: domain.id
, typeName: 'SOA'
, className: 'IN'
, ttl: domain.ttl || 60
// nameserver -- select an NS at random if they're all in sync
, primary: nameserver
, name_server: nameserver
// admin -- email address or domain for admin
, admin: domain.admin || ('admin.' + domain.id)
, email_addr: domain.admin || ('admin.' + domain.id)
// serial -- the version, for cache-busting of secondary nameservers. suggested format: YYYYMMDDnn
, serial: domain.serial || Math.round((domain.updatedAt || domain.createdAt || 0) / 1000)
, sn: domain.serial || Math.round((domain.updatedAt || domain.createdAt || 0) / 1000)
// refresh -- only used when nameservers following the DNS NOTIFY spec talk
, refresh: domain.refresh || 1800
, ref: domain.refresh || 1800
// retry -- only used when nameservers following the DNS NOTIFY spec talk
, retry: domain.retry || 600
, ret: domain.retry || 600
// expiration -- how long other nameservers should continue when the primary goes down
, expiration: domain.expiration || 2419200
, ex: domain.expiration || 2419200
// minimum -- how long to cache a non-existent domain (also the default ttl for BIND)
, minimum: domain.minimum || 5
, nx: domain.minimum || 5
};
};
engine.peers = { engine.peers = {
all: function (cb) { all: function (cb) {
process.nextTick(function () { process.nextTick(function () {
@ -55,8 +95,6 @@ module.exports.create = function (opts) {
}); });
} }
}; };
engine.getSoas = engine.zones.get;
engine.getRecords = engine.records.get;
return engine; return engine;
}; };