small refactor

This commit is contained in:
AJ ONeal 2017-03-30 23:19:52 -06:00
parent bf4e6398c7
commit a41b3f8f3f
2 changed files with 31 additions and 30 deletions

View File

@ -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));

27
hexdump.js Normal file
View File

@ -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;
};