From 578f57bf10e126fb08e35aa1b21d6bdd79f23cd5 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 17 Feb 2017 20:18:19 -0700 Subject: [PATCH] now captures packets, yay! --- README.md | 9 +++++---- bin/dig.js | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 93ab931..47c4b6f 100644 --- a/README.md +++ b/README.md @@ -62,8 +62,9 @@ Options ``` --debug --mdns --t (superfluous) --c --p --q (superfluous) +--output write query and response(s) to disk with this path prefix (ex: ./samples/dns) +-t (superfluous) 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 bd14057..747e54e 100755 --- a/bin/dig.js +++ b/bin/dig.js @@ -87,6 +87,35 @@ function hexdump(ab) { return head + '\n' + str + '\n' + trail; } +function writeQuery(opts, query, queryAb) { + var path = require('path'); + var filename = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.query.bin'; + var fullpath = opts.output + '.' + filename; + if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) { + fullpath = path.join(opts.output, filename); + } + + fs.writeFileAsync(fullpath, Buffer.from(queryAb)).then(function () { + console.log('wrote ' + queryAb.byteLength + ' bytes to ' + fullpath); + }); +} + +var count = 0; +function writeResponse(opts, query, nb) { + var path = require('path'); + var filename = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + count + '.bin'; + var fullpath = opts.output + '.' + filename; + if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) { + fullpath = path.join(opts.output, filename); + } + + count += 1; + + fs.writeFileAsync(fullpath, nb).then(function () { + console.log('wrote ' + nb.length + ' bytes to ' + fullpath); + }); +} + function request(query, opts) { var queryAb = dnsjs.DNSPacket.write(query); @@ -102,7 +131,6 @@ function request(query, opts) { console.log(''); } - var count = 0; var handlers = {}; var server = dgram.createSocket({ type: 'udp4' @@ -164,14 +192,9 @@ function request(query, opts) { console.log(''); if (opts.output) { - var filename = packet.question[0].typeName + '-' + count + '.bin'; - var fullpath = opts.output + filename; - - count += 1; - - return fs.writeFileAsync(fullpath, nb).then(function () { - console.log('wrote ' + nb.length + ' bytes to ' + fullpath); - }); + console.log(''); + writeQuery(opts, query, queryAb); + writeResponse(opts, query, nb); } }; handlers.onListening = function () {