diff --git a/lib/digd.js b/lib/digd.js index 1ff359b..cffbb43 100644 --- a/lib/digd.js +++ b/lib/digd.js @@ -16,7 +16,7 @@ function getRecords(engine, qname, cb) { // SECURITY XXX TODO var dig = require('dig.js/dns-request'); var count; - return engine.getRecords({ name: qname }, function (err, myRecords) { + return engine.records.get({ name: qname }, function (err, myRecords) { if (err) { cb(err); return; } function checkCount() { @@ -188,7 +188,7 @@ function getNs(engine, ds, results, cb) { return -1 !== engine.primaryNameservers.indexOf(ns.data.toLowerCase()); })) { results.authority.length = 0; - results.authority.push(domainToSoa(engine.primaryNameservers, d)); + results.authority.push(engine.zoneToSoa(d)); results.header.rcode = NXDOMAIN; } cb(null, results); @@ -196,54 +196,13 @@ function getNs(engine, ds, results, cb) { }); } -function domainToSoa(primaryNameservers, domain) { - 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) { +function getSoa(engine, domain, results, cb, answerSoa) { console.log('[DEV] getSoa entered'); if (!answerSoa) { - results.authority.push(domainToSoa(primaryNameservers, domain)); + results.authority.push(engine.zoneToSoa(domain)); } else { - results.answer.push(domainToSoa(primaryNameservers, domain)); + results.answer.push(engine.zoneToSoa(domain)); } cb(null, results); @@ -326,7 +285,8 @@ module.exports.query = function (engine, query, cb) { console.log('[DEV] 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); if (err) { cb(err); return; } @@ -350,7 +310,7 @@ module.exports.query = function (engine, query, cb) { //console.log('sorted domains', myDomains); 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) { @@ -364,7 +324,7 @@ module.exports.query = function (engine, query, cb) { } // myDomains was sorted such that the longest was first - return getSoa(engine.primaryNameservers, myDomains[0], results, cb); + return getSoa(engine, myDomains[0], results, cb); }); }); diff --git a/lib/httpd.js b/lib/httpd.js index d48212c..feb6136 100644 --- a/lib/httpd.js +++ b/lib/httpd.js @@ -194,10 +194,18 @@ module.exports.create = function (cli, engine/*, dnsd*/) { }; } app.get('/api/zones/:zone/records', function (req, res) { - engine.records.all(function (err, records) { - res.send({ records: records.filter(function (r) { - return r.zone === req.params.zone; - }).map(mapRecord) }); + var zonename = req.params.zone; + engine.zones.get({ names: [ zonename ] }, function (err, zones) { + var zone = engine.zoneToSoa(zones[0]); + 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) { diff --git a/lib/store.json.js b/lib/store.json.js index cb859d6..3d4b74e 100644 --- a/lib/store.json.js +++ b/lib/store.json.js @@ -7,6 +7,46 @@ module.exports.create = function (opts) { var db = require(opts.filepath); 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 = { all: function (cb) { process.nextTick(function () { @@ -55,8 +95,6 @@ module.exports.create = function (opts) { }); } }; - engine.getSoas = engine.zones.get; - engine.getRecords = engine.records.get; return engine; };