Skip to content

Commit

Permalink
dns: root server plugin should run before cache
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Feb 26, 2021
1 parent 32be720 commit 0bb5f4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
54 changes: 24 additions & 30 deletions lib/dns/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,36 +323,10 @@ class RootServer extends DNSServer {
return res;
}

// Plugins can insert middleware here and hijack the
// lookup for special TLDs before checking Urkel tree.
// We also pass the entire question in case a plugin
// is able to return an authoritative (non-referral) answer.
let data = null;
if (typeof this.middle === 'function') {
try {
data = await this.middle(tld, req);
} catch (e) {
this.logger.error(
'Root server middleware resolution failed for name %s: %s',
name,
e.message
);
}

// Middleware function did an entire lookup on its own.
// Despite the variable name this must be a `Message` object,
// but since a plugin could be using an external instance of
// bns/wire/Message we can't assert instanceof.
if (data)
return data;
}

if (!data) {
// Ask the urkel tree for the name data.
data = !this.blacklist.has(tld)
? (await this.lookupName(tld))
: null;
}
// Ask the urkel tree for the name data.
const data = !this.blacklist.has(tld)
? (await this.lookupName(tld))
: null;

// Non-existent domain.
if (!data) {
Expand Down Expand Up @@ -422,6 +396,26 @@ class RootServer extends DNSServer {
const {name, type} = qs;
const tld = util.from(name, -1);

// Plugins can insert middleware here and hijack the
// lookup for special TLDs before checking Urkel tree.
// We also pass the entire question in case a plugin
// is able to return an authoritative (non-referral) answer.
if (typeof this.middle === 'function') {
let res;
try {
res = await this.middle(tld, req);
} catch (e) {
this.logger.error(
'Root server middleware resolution failed for name %s: %s',
name,
e.message
);
}

if (res)
return res;
}

// Hit the cache first.
const cache = this.cache.get(name, type);

Expand Down
2 changes: 1 addition & 1 deletion test/ns-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('RootServer Plugins', function() {
const name = qs.name.toLowerCase();
const type = qs.type;

if (tld === 'bit') {
if (tld === 'bit.') {
// This plugin runs an imaginary Namecoin full node.
// It looks up records and returns an authoritative answer.
// This makes it look like the complete record including
Expand Down

0 comments on commit 0bb5f4b

Please sign in to comment.