add type<num> support, fix recasing bug

This commit is contained in:
AJ ONeal 2017-10-09 14:45:07 -06:00
parent b3d7408db4
commit b6bc592e56
4 changed files with 21 additions and 11 deletions

View File

@ -73,7 +73,7 @@ Options
--mdns Use mDNS port and nameserver address, and listen for multiple packets
-t <type> (superfluous) default ANY (mdns default: PTR)
-t <type> (superfluous) A, CNAME, MX, etc. Also supports -t type<decimal> for "unsupported" types. default ANY (mdns default: PTR)
-c <class> default IN
-p <port> default 53 (mdns default: 5353) (listener is random for DNS and 5353 for mDNS)
-q <query> (superfluous) required (ex: daplie.com)

View File

@ -4,6 +4,7 @@
var dig = require('../dns-request');
var cli = require('cli');
var defaultNameservers = require('dns').getServers();
var typeRe = /^type\d+$/i;
cli.parse({
// 'b': [ false, 'set source IP address (defaults to 0.0.0.0)', 'string' ]
@ -30,7 +31,7 @@ var common = require('../common.js');
cli.main(function (args, cli) {
args.forEach(function (arg) {
if (-1 !== common.types.concat([ 'ANY' ]).indexOf(arg.toUpperCase())) {
if (typeRe.test(arg) || -1 !== common.types.concat([ 'ANY' ]).indexOf(arg.toUpperCase())) {
if (cli.type) {
console.error("'type' was specified more than once");
process.exit(1);
@ -128,6 +129,9 @@ cli.main(function (args, cli) {
if (!cli.type) {
cli.type = cli.t = 'ANY';
}
if (typeRe.test(cli.type)) {
cli.rawType = parseInt(cli.type.replace('type', ''), 10);
}
if (!cli.port) {
cli.port = cli.p = 53;
}
@ -158,7 +162,8 @@ cli.main(function (args, cli) {
}
, question: [
{ name: cli.casedQuery
, typeName: cli.type
, type: cli.rawType
, typeName: cli.rawType ? undefined : cli.type
, className: cli.class
}
]
@ -220,7 +225,7 @@ cli.main(function (args, cli) {
if (-1 !== i) {
// "EXamPLE.cOm".replace("wWw.EXamPLE.cOm".substr(4), "www.example.com".substr(4))
a.name = a.name.replace(cli.casedQuery.substr(i), cli.query.substr(i));
} else {
} else if (-1 !== j) {
// "www.example.com".replace("EXamPLE.cOm", "example.com")
a.name = a.name.substr(0, j) + a.name.substr(j).replace(cli.casedQuery, cli.query);
}
@ -229,7 +234,8 @@ cli.main(function (args, cli) {
// it does not handle the case of a record for example.com.uk being returned in response to a query for www.example.com correctly
// (but I don't think it should need to)
if (a.name.length !== an.length) {
console.error("[ERROR] '" + an + "' != '" + a.length + "'");
console.error("[ERROR] question / answer mismatch: '" + an + "' != '" + a.length + "'");
console.error(a);
}
});
});

View File

@ -6,7 +6,7 @@ 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');
console.log(';' + q.name + '.', q.ttl, (q.className || q.class), (q.typeName || ('type' + q.type)), q.data || q.rdata || 'unknown record type');
}
, 'A': function (q) {
@ -43,8 +43,10 @@ module.exports = {
}
, 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 basename = query.question[0].name + '.'
+ (query.question[0].typeName||query.question[0].type.toString()).toLowerCase();
var binname = basename + '.query.bin';
var jsonname = basename + '.query.json';
var binpath = opts.output + '.' + binname;
var jsonpath = opts.output + '.' + jsonname;
var json = JSON.stringify(query, null, 2);
@ -64,8 +66,10 @@ module.exports = {
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 basename = query.question[0].name + '.'
+ (query.question[0].typeName||query.question[0].type.toString()).toLowerCase();
var binname = basename + '.' + me._count + '.bin';
var jsonname = basename + '.' + me._count + '.json';
var binpath = opts.output + '.' + binname;
var jsonpath = opts.output + '.' + jsonname;
var json = JSON.stringify(packet, null, 2);

View File

@ -25,7 +25,7 @@ function logQuestion(packet) {
console.info('');
console.info(';; QUESTION SECTION:');
packet.question.forEach(function (q) {
console.info(';' + q.name + '.', ' ', q.className, q.typeName);
console.info(';' + q.name + '.', ' ', q.className, q.typeName || ('type' + q.type));
});
}