diff --git a/bin/dig.js b/bin/dig.js index 1633611..bd14057 100755 --- a/bin/dig.js +++ b/bin/dig.js @@ -23,6 +23,41 @@ var PromiseA = require('bluebird'); var fs = PromiseA.promisifyAll(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('" "') + '"'); + } +}; function hexdump(ab) { var ui8 = new Uint8Array(ab); @@ -89,14 +124,48 @@ function request(query, opts) { server.close(); } + if (opts.debug) { + console.log(''); + console.log('DNS Response:'); + 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)); + 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; + printer(q); + } + if (packet.answer.length) { + console.log(''); + console.log(';; ANSWER SECTION:'); + packet.answer.forEach(print); + } + if (packet.authority.length) { + console.log(''); + console.log(';; AUTHORITY SECTION:'); + packet.authority.forEach(print); + } + if (packet.additional.length) { + console.log(''); + console.log(';; ADDITIONAL SECTION:'); + packet.additional.forEach(print); + } + console.log(''); + console.log(';; MSG SIZE rcvd: ' + nb.byteLength); console.log(''); - console.log('DNS Response:'); - console.log(packet); if (opts.output) { - var path = require('path'); var filename = packet.question[0].typeName + '-' + count + '.bin'; - var fullpath = path.join('samples', filename); + var fullpath = opts.output + filename; count += 1;