From 51daf2378d47f19605461bd06b11b2bf8ffe633b Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 15 Sep 2017 18:42:32 -0600 Subject: [PATCH] updated formatting, small refactoring --- bin/dig.js | 136 ++++++++++++++++++----------------------------------- common.js | 87 ++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 91 deletions(-) create mode 100644 common.js diff --git a/bin/dig.js b/bin/dig.js index 57ac942..1c741d9 100755 --- a/bin/dig.js +++ b/bin/dig.js @@ -15,7 +15,7 @@ cli.parse({ //, 'json': [ false, 'output results as json', 'string' ] //, 'lint': [ false, 'attack (in the metaphorical sense) a nameserver with all sorts of queries to test for correct responses', 'string', false ] , 'mdns': [ false, "Alias for setting defaults to -p 5353 @224.0.0.251 -t PTR -q _services._dns-sd._udp.local and waiting for multiple responses", 'boolean', false ] -, 'timeout': [ false, "Alias for setting defaults to -p 5353 @224.0.0.251 -t PTR -q _services._dns-sd._udp.local and waiting for multiple responses", 'boolean', false ] +, 'timeout': [ false, "How long, in milliseconds, to wait for a response. Alias of +time=", 'int', false ] , 'output': [ 'o', 'output prefix to use for writing query and response(s) to disk', 'file' ] , 'port': [ 'p', 'port (defaults to 53 for dns and 5353 for mdns)', 'int' ] //, 'serve': [ 's', 'path to json file with array of responses to issue for given queries', 'string' ] @@ -25,88 +25,9 @@ cli.parse({ var fs = require('fs'); var dgram = require('dgram'); -var commonTypes = [ 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT' ]; -var commonPrinters = { - 'ANY': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data || q.rdata || 'unknown record type'); - } - -, 'A': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.address); - } -, 'AAAA': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.address); - } -, 'CNAME': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data + '.'); - } -, 'MX': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.priority + ' ' + q.exchange + '.'); - } -, 'NS': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data); - } -, 'PTR': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data); - } -/* -, 'SOA': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data); - } -*/ -, 'SRV': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.priority + ' ' + q.weight + ' ' + q.port + ' ' + q.target); - } -, 'TXT': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, '"' + q.data.join('" "') + '"'); - } -, 'CAA': function (q) { - console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.flag + ' ' + q.tag + ' "' + q.value + '"'); - } -}; - -function writeQuery(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 binpath = opts.output + '.' + binname; - var jsonpath = opts.output + '.' + jsonname; - var json = JSON.stringify(query, null, 2); - if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) { - binpath = path.join(opts.output, binname); - jsonpath = path.join(opts.output, jsonname); - } - - fs.writeFile(binpath, Buffer.from(queryAb), null, function () { - console.log('wrote ' + queryAb.byteLength + ' bytes to ' + binpath); - }); - fs.writeFile(jsonpath, json, null, function () { - console.log('wrote ' + json.length + ' bytes to ' + jsonpath); - }); -} +var common = require('../common.js'); var count = 0; -function writeResponse(opts, query, nb, packet) { - var path = require('path'); - var binname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + count + '.bin'; - var jsonname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + count + '.json'; - var binpath = opts.output + '.' + binname; - var jsonpath = opts.output + '.' + jsonname; - var json = JSON.stringify(packet, null, 2); - if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) { - binpath = path.join(opts.output, binname); - jsonpath = path.join(opts.output, jsonname); - } - - count += 1; - - fs.writeFile(binpath, nb, null, function () { - console.log('wrote ' + nb.byteLength + ' bytes to ' + binpath); - }); - fs.writeFile(jsonpath, json, null, function () { - console.log('wrote ' + json.length + ' bytes to ' + jsonpath); - }); -} function request(query, opts) { var queryAb = dnsjs.DNSPacket.write(query); @@ -135,6 +56,7 @@ function request(query, opts) { }; handlers.onMessage = function (nb) { var packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength)); + var flags = ""; if (packet.id !== query.id) { console.log('ignoring packet for ', packet.question[0].name); @@ -150,18 +72,29 @@ function request(query, opts) { console.log(packet); } - console.log(''); - console.log('; <<>> dig.js ' + 'v0.0.0' + ' <<>> ' + query.question[0].name); console.log(';; Got answer:'); - console.log(';; ->>HEADER<<-'); - console.log(JSON.stringify(packet.header)); + // TODO opcode 0 QUERY rcode 0 NOERROR + console.log(';; ->>HEADER<<- [opcode: ' + packet.header.opcode + ', status: ' + packet.header.rcode + '], id: ' + packet.header.id); + if (packet.header.tc) { console.log("Truncated [tc] (we don't know the normal way to print a tc packet... you should record this with -o tc-packet.dig and send it to us)"); } + flags += ";; flags:"; + if (packet.header.qr) { flags += " qr"; } + if (packet.header.aa) { flags += " aa"; } + if (packet.header.rd) { flags += " rd"; } + if (packet.header.ra) { flags += " ra"; } + flags += "; QUERY: " + packet.question.length + ", ANSWER: " + packet.answer.length + ", AUTHORITY: " + packet.authority.length + ", ADDITIONAL: " + packet.additional.length; + console.log(flags); + if (packet.header.res1) { console.log("[res1] (we don't know how to print a packet with res1 yet)"); } + if (packet.header.res2) { console.log("[res2] (we don't know how to print a packet with res2 yet)"); } + if (packet.header.res3) { console.log("[res3] (we don't know how to print a packet with res2 yet)"); } + // {"id":32736,"qr":1,"opcode":0,"aa":0,"tc":0,"rd":1,"ra":0,"res1":0,"res2":0,"res3":0,"rcode":5} + //console.log(JSON.stringify(packet.header)); console.log(''); console.log(';; QUESTION SECTION:'); packet.question.forEach(function (q) { console.log(';' + q.name + '.', ' ', q.className, q.typeName); }); function print(q) { - var printer = commonPrinters[q.typeName] || commonPrinters.ANY; + var printer = common.printers[q.typeName] || common.printers.ANY; printer(q); } if (packet.answer.length) { @@ -180,13 +113,18 @@ function request(query, opts) { packet.additional.forEach(print); } console.log(''); + console.log(';; Query time: ' + (Date.now() - opts._ts) + ' msec'); + // ;; SERVER: 8.8.8.8#53(8.8.8.8) + console.log(';; SERVER: ' + opts._nameserver + '#' + opts.port + '(' + opts._nameserver + ')'); + // TODO ;; WHEN: Fri Sep 15 18:25:53 2017 + console.log(';; WHEN: ' + new Date().toString()); console.log(';; MSG SIZE rcvd: ' + nb.byteLength); console.log(''); if (opts.output) { console.log(''); - writeQuery(opts, query, queryAb); - writeResponse(opts, query, nb, packet); + common.writeQuery(opts, query, queryAb); + common.writeResponse(opts, query, nb, packet); } }; handlers.onListening = function () { @@ -220,6 +158,8 @@ function request(query, opts) { console.log("querying '" + nameserver + "':'" + opts.port + "'"); } server.send(Buffer.from(queryAb), opts.port, nameserver, function () { + opts._nameserver = nameserver; + opts._ts = Date.now(); if (opts.debug) { console.log(''); console.log('request sent'); @@ -227,7 +167,11 @@ function request(query, opts) { }); }; - + console.log(''); + if (!cli.nocmd) { + console.log('; <<>> dig.js ' + 'v0.0.0' + ' <<>> ' + process.argv.slice(2).join(' ')); + console.log(';; global options: +cmd'); + } server.on('error', handlers.onError); server.on('message', handlers.onMessage); server.on('listening', handlers.onListening); @@ -248,7 +192,7 @@ function request(query, opts) { cli.main(function (args, cli) { args.forEach(function (arg) { - if (-1 !== commonTypes.concat([ 'ANY' ]).indexOf(arg.toUpperCase())) { + if (-1 !== common.types.concat([ 'ANY' ]).indexOf(arg.toUpperCase())) { if (cli.type) { console.error("'type' was specified more than once"); process.exit(1); @@ -258,6 +202,16 @@ cli.main(function (args, cli) { return; } + if (arg === '+norecurse') { + if (cli.norecurse) { + console.error("'+norecurse' was specified more than once"); + process.exit(1); + return; + } + cli.norecurse = true; + return; + } + if (/^\+time=/.test(arg)) { if (cli.timeout) { console.error("'+time=' was specified more than once"); @@ -332,7 +286,7 @@ cli.main(function (args, cli) { , opcode: 0 , aa: 0 // NA , tc: 0 // NA - , rd: 1 + , rd: cli.norecurse ? 0 : 1 , ra: 0 // NA , rcode: 0 // NA } diff --git a/common.js b/common.js new file mode 100644 index 0000000..2d663ec --- /dev/null +++ b/common.js @@ -0,0 +1,87 @@ +'use strict'; + +var fs = require('fs'); + +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'); + } + + , 'A': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.address); + } + , 'AAAA': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.address); + } + , 'CNAME': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data + '.'); + } + , 'MX': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.priority + ' ' + q.exchange + '.'); + } + , 'NS': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data); + } + , 'PTR': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data); + } + /* + , 'SOA': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.data); + } + */ + , 'SRV': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.priority + ' ' + q.weight + ' ' + q.port + ' ' + q.target); + } + , 'TXT': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, '"' + q.data.join('" "') + '"'); + } + , 'CAA': function (q) { + console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.flag + ' ' + q.tag + ' "' + q.value + '"'); + } + } +, 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 binpath = opts.output + '.' + binname; + var jsonpath = opts.output + '.' + jsonname; + var json = JSON.stringify(query, null, 2); + if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) { + binpath = path.join(opts.output, binname); + jsonpath = path.join(opts.output, jsonname); + } + + fs.writeFile(binpath, Buffer.from(queryAb), null, function () { + console.log('wrote ' + queryAb.byteLength + ' bytes to ' + binpath); + }); + fs.writeFile(jsonpath, json, null, function () { + console.log('wrote ' + json.length + ' bytes to ' + jsonpath); + }); + } +, writeResponse: function (opts, query, nb, packet) { + 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 binpath = opts.output + '.' + binname; + var jsonpath = opts.output + '.' + jsonname; + var json = JSON.stringify(packet, null, 2); + if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) { + binpath = path.join(opts.output, binname); + jsonpath = path.join(opts.output, jsonname); + } + + me._count += 1; + + fs.writeFile(binpath, nb, null, function () { + console.log('wrote ' + nb.byteLength + ' bytes to ' + binpath); + }); + fs.writeFile(jsonpath, json, null, function () { + console.log('wrote ' + json.length + ' bytes to ' + jsonpath); + }); + } +};