From a41b3f8f3f771c7721043cd4a6db2a306e5df270 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 30 Mar 2017 23:19:52 -0600 Subject: [PATCH] small refactor --- bin/dig.js | 34 ++++------------------------------ hexdump.js | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 hexdump.js diff --git a/bin/dig.js b/bin/dig.js index 874866a..03e2dcd 100755 --- a/bin/dig.js +++ b/bin/dig.js @@ -3,6 +3,8 @@ var dnsjs = require('dns-suite'); var cli = require('cli'); +var hexdump = require('../hexdump'); +var crypto = require('crypto'); cli.parse({ // 'b': [ false, 'set source IP address (defaults to 0.0.0.0)', 'string' ] 'class': [ 'c', 'class (defaults to IN)', 'string', 'IN' ] @@ -60,34 +62,6 @@ var commonPrinters = { } }; -function hexdump(ab) { - var ui8 = new Uint8Array(ab); - var bytecount = 0; - var head = ' 0 1 2 3 4 5 6 7 8 9 A B C D E F'; - var trail; - var str = [].slice.call(ui8).map(function (i) { - var h = i.toString(16); - if (h.length < 2) { - h = '0' + h; - } - return h; - }).join('').match(/.{1,2}/g).join(' ').match(/.{1,48}/g).map(function (str) { - var lead = bytecount.toString(16); - bytecount += 16; - - while (lead.length < 7) { - lead = '0' + lead; - } - - return lead + ' ' + str; - }).join('\n'); - trail = ab.byteLength.toString(16); - while (trail.length < 7) { - trail = '0' + trail; - } - return head + '\n' + str + '\n' + trail; -} - function writeQuery(opts, query, queryAb) { var path = require('path'); var binname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.query.bin'; @@ -221,7 +195,7 @@ function request(query, opts) { if (!nameserver) { nameservers = require('dns').getServers(); - index = (Math.round(Math.random() * 7777)) % nameservers.length; + index = crypto.randomBytes(2).readUInt16BE(0) % nameservers.length; nameserver = nameservers[index]; if (opts.debug) { console.log(index, nameservers); @@ -259,7 +233,7 @@ function request(query, opts) { // 53 dns server // 5353 mdns if (opts.mdns) { - server.bind(5353); + server.bind(opts.port /*5353*/); setTimeout(function () { server.close(); }, opts.timeout || (5 * 1000)); diff --git a/hexdump.js b/hexdump.js new file mode 100644 index 0000000..0d057fc --- /dev/null +++ b/hexdump.js @@ -0,0 +1,27 @@ +module.exports = function hexdump(ab) { + var ui8 = new Uint8Array(ab); + var bytecount = 0; + var head = ' 0 1 2 3 4 5 6 7 8 9 A B C D E F'; + var trail; + var str = [].slice.call(ui8).map(function (i) { + var h = i.toString(16); + if (h.length < 2) { + h = '0' + h; + } + return h; + }).join('').match(/.{1,2}/g).join(' ').match(/.{1,48}/g).map(function (str) { + var lead = bytecount.toString(16); + bytecount += 16; + + while (lead.length < 7) { + lead = '0' + lead; + } + + return lead + ' ' + str; + }).join('\n'); + trail = ab.byteLength.toString(16); + while (trail.length < 7) { + trail = '0' + trail; + } + return head + '\n' + str + '\n' + trail; +};