format like DiG

This commit is contained in:
AJ ONeal 2017-02-17 20:00:42 -07:00
parent 26816ee0b1
commit e23046f736
1 changed files with 73 additions and 4 deletions

View File

@ -23,6 +23,41 @@ var PromiseA = require('bluebird');
var fs = PromiseA.promisifyAll(require('fs')); var fs = PromiseA.promisifyAll(require('fs'));
var dgram = require('dgram'); var dgram = require('dgram');
var commonTypes = [ 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT' ]; 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) { function hexdump(ab) {
var ui8 = new Uint8Array(ab); var ui8 = new Uint8Array(ab);
@ -89,14 +124,48 @@ function request(query, opts) {
server.close(); 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('');
console.log('DNS Response:');
console.log(packet);
if (opts.output) { if (opts.output) {
var path = require('path');
var filename = packet.question[0].typeName + '-' + count + '.bin'; var filename = packet.question[0].typeName + '-' + count + '.bin';
var fullpath = path.join('samples', filename); var fullpath = opts.output + filename;
count += 1; count += 1;