From b6bc592e566816fe3aea412945e0dc950ad60b61 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 9 Oct 2017 14:45:07 -0600 Subject: [PATCH] add type support, fix recasing bug --- README.md | 2 +- bin/dig.js | 14 ++++++++++---- common.js | 14 +++++++++----- dns-request.js | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8ece408..6a36830 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Options --mdns Use mDNS port and nameserver address, and listen for multiple packets --t (superfluous) default ANY (mdns default: PTR) +-t (superfluous) A, CNAME, MX, etc. Also supports -t type for "unsupported" types. default ANY (mdns default: PTR) -c default IN -p default 53 (mdns default: 5353) (listener is random for DNS and 5353 for mDNS) -q (superfluous) required (ex: daplie.com) diff --git a/bin/dig.js b/bin/dig.js index 1cf337a..0c47e57 100755 --- a/bin/dig.js +++ b/bin/dig.js @@ -4,6 +4,7 @@ var dig = require('../dns-request'); var cli = require('cli'); var defaultNameservers = require('dns').getServers(); +var typeRe = /^type\d+$/i; cli.parse({ // 'b': [ false, 'set source IP address (defaults to 0.0.0.0)', 'string' ] @@ -30,7 +31,7 @@ var common = require('../common.js'); cli.main(function (args, cli) { args.forEach(function (arg) { - if (-1 !== common.types.concat([ 'ANY' ]).indexOf(arg.toUpperCase())) { + if (typeRe.test(arg) || -1 !== common.types.concat([ 'ANY' ]).indexOf(arg.toUpperCase())) { if (cli.type) { console.error("'type' was specified more than once"); process.exit(1); @@ -128,6 +129,9 @@ cli.main(function (args, cli) { if (!cli.type) { cli.type = cli.t = 'ANY'; } + if (typeRe.test(cli.type)) { + cli.rawType = parseInt(cli.type.replace('type', ''), 10); + } if (!cli.port) { cli.port = cli.p = 53; } @@ -158,7 +162,8 @@ cli.main(function (args, cli) { } , question: [ { name: cli.casedQuery - , typeName: cli.type + , type: cli.rawType + , typeName: cli.rawType ? undefined : cli.type , className: cli.class } ] @@ -220,7 +225,7 @@ cli.main(function (args, cli) { if (-1 !== i) { // "EXamPLE.cOm".replace("wWw.EXamPLE.cOm".substr(4), "www.example.com".substr(4)) a.name = a.name.replace(cli.casedQuery.substr(i), cli.query.substr(i)); - } else { + } else if (-1 !== j) { // "www.example.com".replace("EXamPLE.cOm", "example.com") a.name = a.name.substr(0, j) + a.name.substr(j).replace(cli.casedQuery, cli.query); } @@ -229,7 +234,8 @@ cli.main(function (args, cli) { // it does not handle the case of a record for example.com.uk being returned in response to a query for www.example.com correctly // (but I don't think it should need to) if (a.name.length !== an.length) { - console.error("[ERROR] '" + an + "' != '" + a.length + "'"); + console.error("[ERROR] question / answer mismatch: '" + an + "' != '" + a.length + "'"); + console.error(a); } }); }); diff --git a/common.js b/common.js index 4910f0e..47726c7 100644 --- a/common.js +++ b/common.js @@ -6,7 +6,7 @@ module.exports = { types: [ 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT' ] , printers: { 'ANY': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data || q.rdata || 'unknown record type'); + console.log(';' + q.name + '.', q.ttl, (q.className || q.class), (q.typeName || ('type' + q.type)), q.data || q.rdata || 'unknown record type'); } , 'A': function (q) { @@ -43,8 +43,10 @@ module.exports = { } , writeQuery: function (opts, query, queryAb) { var path = require('path'); - var binname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.query.bin'; - var jsonname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.query.json'; + var basename = query.question[0].name + '.' + + (query.question[0].typeName||query.question[0].type.toString()).toLowerCase(); + var binname = basename + '.query.bin'; + var jsonname = basename + '.query.json'; var binpath = opts.output + '.' + binname; var jsonpath = opts.output + '.' + jsonname; var json = JSON.stringify(query, null, 2); @@ -64,8 +66,10 @@ module.exports = { var me = this; me._count = me._count || 0; var path = require('path'); - var binname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + me._count + '.bin'; - var jsonname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + me._count + '.json'; + var basename = query.question[0].name + '.' + + (query.question[0].typeName||query.question[0].type.toString()).toLowerCase(); + var binname = basename + '.' + me._count + '.bin'; + var jsonname = basename + '.' + me._count + '.json'; var binpath = opts.output + '.' + binname; var jsonpath = opts.output + '.' + jsonname; var json = JSON.stringify(packet, null, 2); diff --git a/dns-request.js b/dns-request.js index 1714321..dca6f9c 100644 --- a/dns-request.js +++ b/dns-request.js @@ -25,7 +25,7 @@ function logQuestion(packet) { console.info(''); console.info(';; QUESTION SECTION:'); packet.question.forEach(function (q) { - console.info(';' + q.name + '.', ' ', q.className, q.typeName); + console.info(';' + q.name + '.', ' ', q.className, q.typeName || ('type' + q.type)); }); }