updated formatting, small refactoring

This commit is contained in:
AJ ONeal 2017-09-15 18:42:32 -06:00
parent 0cabe30b88
commit 51daf2378d
2 changed files with 132 additions and 91 deletions

View File

@ -15,7 +15,7 @@ cli.parse({
//, 'json': [ false, 'output results as json', 'string' ] //, 'json': [ false, 'output results as json', 'string' ]
//, 'lint': [ false, 'attack (in the metaphorical sense) a nameserver with all sorts of queries to test for correct responses', 'string', false ] //, 'lint': [ false, 'attack (in the metaphorical sense) a nameserver with all sorts of queries to test for correct responses', 'string', false ]
, 'mdns': [ false, "Alias for setting defaults to -p 5353 @224.0.0.251 -t PTR -q _services._dns-sd._udp.local and waiting for multiple responses", 'boolean', false ] , 'mdns': [ false, "Alias for setting defaults to -p 5353 @224.0.0.251 -t PTR -q _services._dns-sd._udp.local and waiting for multiple responses", 'boolean', false ]
, 'timeout': [ false, "Alias for setting defaults to -p 5353 @224.0.0.251 -t PTR -q _services._dns-sd._udp.local and waiting for multiple responses", 'boolean', false ] , 'timeout': [ false, "How long, in milliseconds, to wait for a response. Alias of +time=", 'int', false ]
, 'output': [ 'o', 'output prefix to use for writing query and response(s) to disk', 'file' ] , 'output': [ 'o', 'output prefix to use for writing query and response(s) to disk', 'file' ]
, 'port': [ 'p', 'port (defaults to 53 for dns and 5353 for mdns)', 'int' ] , 'port': [ 'p', 'port (defaults to 53 for dns and 5353 for mdns)', 'int' ]
//, 'serve': [ 's', 'path to json file with array of responses to issue for given queries', 'string' ] //, 'serve': [ 's', 'path to json file with array of responses to issue for given queries', 'string' ]
@ -25,88 +25,9 @@ cli.parse({
var fs = require('fs'); var fs = require('fs');
var dgram = require('dgram'); var dgram = require('dgram');
var commonTypes = [ 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT' ]; var common = require('../common.js');
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('" "') + '"');
}
, 'CAA': function (q) {
console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.flag + ' ' + q.tag + ' "' + q.value + '"');
}
};
function writeQuery(opts, query, queryAb) {
var path = require('path');
var binname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.query.bin';
var jsonname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.query.json';
var binpath = opts.output + '.' + binname;
var jsonpath = opts.output + '.' + jsonname;
var json = JSON.stringify(query, null, 2);
if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) {
binpath = path.join(opts.output, binname);
jsonpath = path.join(opts.output, jsonname);
}
fs.writeFile(binpath, Buffer.from(queryAb), null, function () {
console.log('wrote ' + queryAb.byteLength + ' bytes to ' + binpath);
});
fs.writeFile(jsonpath, json, null, function () {
console.log('wrote ' + json.length + ' bytes to ' + jsonpath);
});
}
var count = 0; var count = 0;
function writeResponse(opts, query, nb, packet) {
var path = require('path');
var binname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + count + '.bin';
var jsonname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + count + '.json';
var binpath = opts.output + '.' + binname;
var jsonpath = opts.output + '.' + jsonname;
var json = JSON.stringify(packet, null, 2);
if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) {
binpath = path.join(opts.output, binname);
jsonpath = path.join(opts.output, jsonname);
}
count += 1;
fs.writeFile(binpath, nb, null, function () {
console.log('wrote ' + nb.byteLength + ' bytes to ' + binpath);
});
fs.writeFile(jsonpath, json, null, function () {
console.log('wrote ' + json.length + ' bytes to ' + jsonpath);
});
}
function request(query, opts) { function request(query, opts) {
var queryAb = dnsjs.DNSPacket.write(query); var queryAb = dnsjs.DNSPacket.write(query);
@ -135,6 +56,7 @@ function request(query, opts) {
}; };
handlers.onMessage = function (nb) { handlers.onMessage = function (nb) {
var packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength)); var packet = dnsjs.DNSPacket.parse(nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength));
var flags = "";
if (packet.id !== query.id) { if (packet.id !== query.id) {
console.log('ignoring packet for ', packet.question[0].name); console.log('ignoring packet for ', packet.question[0].name);
@ -150,18 +72,29 @@ function request(query, opts) {
console.log(packet); console.log(packet);
} }
console.log('');
console.log('; <<>> dig.js ' + 'v0.0.0' + ' <<>> ' + query.question[0].name);
console.log(';; Got answer:'); console.log(';; Got answer:');
console.log(';; ->>HEADER<<-'); // TODO opcode 0 QUERY rcode 0 NOERROR
console.log(JSON.stringify(packet.header)); console.log(';; ->>HEADER<<- [opcode: ' + packet.header.opcode + ', status: ' + packet.header.rcode + '], id: ' + packet.header.id);
if (packet.header.tc) { console.log("Truncated [tc] (we don't know the normal way to print a tc packet... you should record this with -o tc-packet.dig and send it to us)"); }
flags += ";; flags:";
if (packet.header.qr) { flags += " qr"; }
if (packet.header.aa) { flags += " aa"; }
if (packet.header.rd) { flags += " rd"; }
if (packet.header.ra) { flags += " ra"; }
flags += "; QUERY: " + packet.question.length + ", ANSWER: " + packet.answer.length + ", AUTHORITY: " + packet.authority.length + ", ADDITIONAL: " + packet.additional.length;
console.log(flags);
if (packet.header.res1) { console.log("[res1] (we don't know how to print a packet with res1 yet)"); }
if (packet.header.res2) { console.log("[res2] (we don't know how to print a packet with res2 yet)"); }
if (packet.header.res3) { console.log("[res3] (we don't know how to print a packet with res2 yet)"); }
// {"id":32736,"qr":1,"opcode":0,"aa":0,"tc":0,"rd":1,"ra":0,"res1":0,"res2":0,"res3":0,"rcode":5}
//console.log(JSON.stringify(packet.header));
console.log(''); console.log('');
console.log(';; QUESTION SECTION:'); console.log(';; QUESTION SECTION:');
packet.question.forEach(function (q) { packet.question.forEach(function (q) {
console.log(';' + q.name + '.', ' ', q.className, q.typeName); console.log(';' + q.name + '.', ' ', q.className, q.typeName);
}); });
function print(q) { function print(q) {
var printer = commonPrinters[q.typeName] || commonPrinters.ANY; var printer = common.printers[q.typeName] || common.printers.ANY;
printer(q); printer(q);
} }
if (packet.answer.length) { if (packet.answer.length) {
@ -180,13 +113,18 @@ function request(query, opts) {
packet.additional.forEach(print); packet.additional.forEach(print);
} }
console.log(''); console.log('');
console.log(';; Query time: ' + (Date.now() - opts._ts) + ' msec');
// ;; SERVER: 8.8.8.8#53(8.8.8.8)
console.log(';; SERVER: ' + opts._nameserver + '#' + opts.port + '(' + opts._nameserver + ')');
// TODO ;; WHEN: Fri Sep 15 18:25:53 2017
console.log(';; WHEN: ' + new Date().toString());
console.log(';; MSG SIZE rcvd: ' + nb.byteLength); console.log(';; MSG SIZE rcvd: ' + nb.byteLength);
console.log(''); console.log('');
if (opts.output) { if (opts.output) {
console.log(''); console.log('');
writeQuery(opts, query, queryAb); common.writeQuery(opts, query, queryAb);
writeResponse(opts, query, nb, packet); common.writeResponse(opts, query, nb, packet);
} }
}; };
handlers.onListening = function () { handlers.onListening = function () {
@ -220,6 +158,8 @@ function request(query, opts) {
console.log("querying '" + nameserver + "':'" + opts.port + "'"); console.log("querying '" + nameserver + "':'" + opts.port + "'");
} }
server.send(Buffer.from(queryAb), opts.port, nameserver, function () { server.send(Buffer.from(queryAb), opts.port, nameserver, function () {
opts._nameserver = nameserver;
opts._ts = Date.now();
if (opts.debug) { if (opts.debug) {
console.log(''); console.log('');
console.log('request sent'); console.log('request sent');
@ -227,7 +167,11 @@ function request(query, opts) {
}); });
}; };
console.log('');
if (!cli.nocmd) {
console.log('; <<>> dig.js ' + 'v0.0.0' + ' <<>> ' + process.argv.slice(2).join(' '));
console.log(';; global options: +cmd');
}
server.on('error', handlers.onError); server.on('error', handlers.onError);
server.on('message', handlers.onMessage); server.on('message', handlers.onMessage);
server.on('listening', handlers.onListening); server.on('listening', handlers.onListening);
@ -248,7 +192,7 @@ function request(query, opts) {
cli.main(function (args, cli) { cli.main(function (args, cli) {
args.forEach(function (arg) { args.forEach(function (arg) {
if (-1 !== commonTypes.concat([ 'ANY' ]).indexOf(arg.toUpperCase())) { if (-1 !== common.types.concat([ 'ANY' ]).indexOf(arg.toUpperCase())) {
if (cli.type) { if (cli.type) {
console.error("'type' was specified more than once"); console.error("'type' was specified more than once");
process.exit(1); process.exit(1);
@ -258,6 +202,16 @@ cli.main(function (args, cli) {
return; return;
} }
if (arg === '+norecurse') {
if (cli.norecurse) {
console.error("'+norecurse' was specified more than once");
process.exit(1);
return;
}
cli.norecurse = true;
return;
}
if (/^\+time=/.test(arg)) { if (/^\+time=/.test(arg)) {
if (cli.timeout) { if (cli.timeout) {
console.error("'+time=' was specified more than once"); console.error("'+time=' was specified more than once");
@ -332,7 +286,7 @@ cli.main(function (args, cli) {
, opcode: 0 , opcode: 0
, aa: 0 // NA , aa: 0 // NA
, tc: 0 // NA , tc: 0 // NA
, rd: 1 , rd: cli.norecurse ? 0 : 1
, ra: 0 // NA , ra: 0 // NA
, rcode: 0 // NA , rcode: 0 // NA
} }

87
common.js Normal file
View File

@ -0,0 +1,87 @@
'use strict';
var fs = require('fs');
module.exports = {
types: [ 'A', 'AAAA', 'CNAME', 'MX', 'NS', 'PTR', 'SOA', 'SRV', 'TXT' ]
, printers: {
'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('" "') + '"');
}
, 'CAA': function (q) {
console.log(';' + q.name + '.', q.ttl, q.className, q.typeName, q.flag + ' ' + q.tag + ' "' + q.value + '"');
}
}
, writeQuery: function (opts, query, queryAb) {
var path = require('path');
var binname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.query.bin';
var jsonname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.query.json';
var binpath = opts.output + '.' + binname;
var jsonpath = opts.output + '.' + jsonname;
var json = JSON.stringify(query, null, 2);
if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) {
binpath = path.join(opts.output, binname);
jsonpath = path.join(opts.output, jsonname);
}
fs.writeFile(binpath, Buffer.from(queryAb), null, function () {
console.log('wrote ' + queryAb.byteLength + ' bytes to ' + binpath);
});
fs.writeFile(jsonpath, json, null, function () {
console.log('wrote ' + json.length + ' bytes to ' + jsonpath);
});
}
, writeResponse: function (opts, query, nb, packet) {
var me = this;
me._count = me._count || 0;
var path = require('path');
var binname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + me._count + '.bin';
var jsonname = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + me._count + '.json';
var binpath = opts.output + '.' + binname;
var jsonpath = opts.output + '.' + jsonname;
var json = JSON.stringify(packet, null, 2);
if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) {
binpath = path.join(opts.output, binname);
jsonpath = path.join(opts.output, jsonname);
}
me._count += 1;
fs.writeFile(binpath, nb, null, function () {
console.log('wrote ' + nb.byteLength + ' bytes to ' + binpath);
});
fs.writeFile(jsonpath, json, null, function () {
console.log('wrote ' + json.length + ' bytes to ' + jsonpath);
});
}
};