From 717fb1fe3e3971ddcd1bee903a5f3c7e802cddc5 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 9 Oct 2017 14:07:45 -0600 Subject: [PATCH] minor fixes --- bin/digd.js | 11 +++++++++-- lib/dns-store.js | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/bin/digd.js b/bin/digd.js index e15964d..727102c 100755 --- a/bin/digd.js +++ b/bin/digd.js @@ -107,6 +107,7 @@ cli.main(function (args, cli) { } catch(e) { // TODO log bad queries (?) console.error("Could not parse DNS query, ignoring."); + console.error(e); try { hexdump = require('hexdump.js').hexdump; console.error(hexdump(queryAb)); @@ -211,7 +212,8 @@ cli.main(function (args, cli) { try { newAb = dnsjs.DNSPacket.write(emptyResp); } catch(e) { - console.error("Could not write DNS response"); + console.error("Could not write empty DNS response"); + console.error(e); console.error(emptyResp); return; } @@ -227,7 +229,8 @@ cli.main(function (args, cli) { try { newAb = dnsjs.DNSPacket.write(newPacket); } catch(e) { - console.error("Could not write DNS response"); + console.error("Could not write DNS response from local"); + console.error(e); console.error(newPacket); return; } @@ -365,10 +368,14 @@ cli.main(function (args, cli) { // TODO get local answer first, if available var path = require('path'); require('../lib/dns-store').query(path.resolve(cli.input), query, function (err, resp) { + if (err) { console.log('[DEV] answer not found in local db, recursing'); console.error(err); recurse(); return; } if (SERVFAIL === resp.header.rcode) { console.log('[DEV] local cache miss, recursing'); recurse(); return; } + // TODO double check does it matter whether rd is set when responding with ra? + if (!cli.norecurse && query.header.rd) { resp.header.ra = 1; } + sendResponse(resp); }); diff --git a/lib/dns-store.js b/lib/dns-store.js index 8781679..a869d43 100644 --- a/lib/dns-store.js +++ b/lib/dns-store.js @@ -9,6 +9,7 @@ module.exports.ask = function (query, cb) { var NOERROR = 0; var NXDOMAIN = 3; var REFUSED = 5; +var primaryNameservers = [ 'localhost' ]; function getRecords(db, qname) { var myRecords = db.records.filter(function (r) { @@ -64,6 +65,8 @@ function dbToResourceRecord(r) { } function getNs(db, ds, results, cb) { + console.log('[DEV] getNs entered'); + var d = ds.shift(); if (!d) { @@ -78,24 +81,39 @@ function getNs(db, ds, results, cb) { return; } - results.authority.push({ + var ns = { name: r.domain , typeName: r.type // NS , className: 'IN' , ttl: r.ttl || 300 - , data: r.value - }); + , data: r.address || r.value || r.data + }; + + console.log('got NS record:'); + console.log(r); + console.log(ns); + + // TODO what if this NS is one of the NS? + // return SOA record instead + results.authority.push(ns); }); if (results.authority.length) { cb(null, results); return; } + + results.header.rcode = NXDOMAIN; + cb(null, results); } function getSoa(db, domain, results, cb) { - var index = Math.floor(Math.random() * domain.nameservers.length) % domain.nameservers.length; - var nameserver = domain.nameservers[index]; + console.log('[DEV] getSoa entered'); + + var nameservers = domain.nameservers || primaryNameservers; + + var index = Math.floor(Math.random() * nameservers.length) % nameservers.length; + var nameserver = nameservers[index]; results.authority.push({ name: domain.id @@ -108,12 +126,12 @@ function getSoa(db, domain, results, cb) { , name_server: nameserver // admin -- email address or domain for admin - , admin: domain.admin - , email_addr: domain.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) / 1000) - , sn: domain.serial || Math.round((domain.updatedAt || domain.createdAt) / 1000) + , 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 @@ -184,7 +202,7 @@ module.exports.query = function (input, query, cb) { , ra: 0 // will be changed by cli.norecurse , rcode: NOERROR // 0 NOERROR, 3 NXDOMAIN, 5 REFUSED } - , question: [], answer: [], authority: [], additional: [] + , question: [ query.question[0] ], answer: [], authority: [], additional: [] }; var myRecords = getRecords(db, qname); @@ -194,6 +212,7 @@ module.exports.query = function (input, query, cb) { results.answer.push(dbToResourceRecord(r)); }); results.header.rcode = NOERROR; + console.log('[DEV] results', results); cb(null, results); return; } @@ -246,10 +265,12 @@ module.exports.query = function (input, query, cb) { console.log('sorted domains', myDomains); return getNs(db, myDomains.slice(0), results, function (err, results) { + console.log('[DEV] getNs complete'); + if (err) { cb(err, results); return; } // has NS records - if (NXDOMAIN !== results.header.rcode) { cb(null, results); return; } + if (NXDOMAIN !== results.header.rcode) { console.log(results); cb(null, results); return; } // myDomains was sorted such that the longest was first getSoa(db, myDomains[0], results, cb);