From 6e7a0c57c8483c03dc1427a4e962050f6e87d5df Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 15 Dec 2017 01:54:39 -0700 Subject: [PATCH] allow empty string for query --- bin/dig.js | 56 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/bin/dig.js b/bin/dig.js index 0c47e57..9524f0d 100755 --- a/bin/dig.js +++ b/bin/dig.js @@ -22,7 +22,7 @@ cli.parse({ , 'nameserver': [ false, 'the nameserver to use for DNS resolution (defaults to ' + defaultNameservers.join(',') + ')', 'string' ] //, 'serve': [ 's', 'path to json file with array of responses to issue for given queries', 'string' ] , 'type': [ 't', 'type (defaults to ANY for dns and PTR for mdns)', 'string' ] -, 'query': [ 'q', 'a superfluous explicit option to set the query as a command line flag' ] +, 'query': [ 'q', 'a superfluous explicit option to set the query as a command line flag', 'string' ] , 'norecase': [ false, 'Disable dns0x20 security checking (mixed casing). See https://dyn.com/blog/use-of-bit-0x20-in-dns-labels/' ] , 'recase': [ false, "Print the dns0x20 casing as-is rather than converting it back to lowercase. This is the default when explicitly using mixed case." ] }); @@ -30,14 +30,16 @@ cli.parse({ var common = require('../common.js'); cli.main(function (args, cli) { + cli.implicitType = cli.type; + cli.implicitQuery = cli.query; args.forEach(function (arg) { if (typeRe.test(arg) || -1 !== common.types.concat([ 'ANY' ]).indexOf(arg.toUpperCase())) { - if (cli.type) { + if (cli.implicitType) { console.error("'type' was specified more than once"); process.exit(1); return; } - cli.type = cli.t = arg.toUpperCase(); + cli.implicitType = cli.t = arg.toUpperCase(); return; } @@ -81,15 +83,41 @@ cli.main(function (args, cli) { return; } - if (cli.query) { - console.error("'query' was specified more than once or unrecognized flag: " + cli.query + ", " + arg); + if ('string' === typeof cli.implicitQuery) { + console.error("'query' was specified more than once or unrecognized flag: " + cli.implicitQuery + ", " + arg); process.exit(1); return; } - cli.query = cli.q = arg; + cli.implicitQuery = cli.q = arg; }); + + // it can happen that a TLD is created with the name of a common type + if (!cli.type && cli.implicitType && !cli.implicitQuery) { + cli.implicitQuery = cli.implicitType; + cli.implicitType = null; + } + if ('string' === typeof cli.implicitQuery) { + cli.query = cli.implicitQuery; + } + if (cli.implicitType) { + cli.type = cli.implicitType; + } + if ('string' !== typeof cli.query) { + console.error(''); + console.error('Usage:'); + console.error('dig.js [@server] [TYPE] [domain]'); + console.error(''); + console.error('Example:'); + console.error('dig.js daplie.com'); + console.error(''); + process.exit(1); + } + if (cli.query !== cli.query.toLowerCase()) { + cli.norecase = true; + } + if (cli.mdns) { if (!cli.type) { cli.type = cli.t = 'PTR'; @@ -100,7 +128,7 @@ cli.main(function (args, cli) { if (!cli.nameserver) { cli.nameserver = '224.0.0.251'; } - if (!cli.query) { + if ('string' !== typeof cli.query) { cli.query = '_services._dns-sd._udp.local'; } if (!cli.timeout) { @@ -112,10 +140,6 @@ cli.main(function (args, cli) { } } - if (cli.query !== cli.query.toLowerCase()) { - cli.norecase = true; - } - if (!cli.norecase) { cli.casedQuery = cli.query.split('').map(function (ch) { // dns0x20 takes advantage of the fact that the binary operation for toUpperCase is @@ -138,16 +162,6 @@ cli.main(function (args, cli) { if (!cli.class) { cli.class = cli.c = 'IN'; } - if (!cli.query) { - console.error(''); - console.error('Usage:'); - console.error('dig.js [@server] [TYPE] [domain]'); - console.error(''); - console.error('Example:'); - console.error('dig.js daplie.com'); - console.error(''); - process.exit(1); - } var query = { header: {