'use strict'; var cli = require('cli'); var dgram = require('dgram'); var dnsjs = require('dns-suite'); var hexdump = require('../hexdump'); var crypto = require('crypto'); var common = require('../common'); cli.parse({ // 'b': [ false, 'set source IP address (defaults to 0.0.0.0)', 'string' ] 'class': [ 'c', 'class (defaults to IN)', 'string', 'IN' ] , 'debug': [ false, 'more verbose output', 'boolean', false ] //, 'insecure': [ false, 'turn off RaNDOm cAPS required for securing queries'] //, 'ipv4': [ '4', 'use ipv4 exclusively (defaults to false)', 'boolean', false ] //, 'ipv6': [ '6', 'use ipv6 exclusively (defaults to false)', 'boolean', false ] //, '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 ] , '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, "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' ] , 'address': [ false, 'ip address(es) to listen on (defaults to 0.0.0.0,::0)', 'string' ] , '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' ] , 'type': [ 't', 'type (defaults to ANY for dns and PTR for mdns)', 'string' ] , 'query': [ 'q', 'a superfluous explicit option to set the query as a command line flag' ] }); cli.main(function (args, cli) { args.forEach(function (arg) { if (arg === '+norecurse') { if (cli.norecurse) { console.error("'+norecurse' was specified more than once"); process.exit(1); return; } cli.norecurse = true; return; } }); if (cli.mdns) { if (!cli.type) { cli.type = cli.t = 'PTR'; } if (!cli.port) { cli.port = cli.p = 5353; } if (!cli.nameserver) { cli.nameserver = '224.0.0.251'; } if (!cli.query) { cli.query = '_services._dns-sd._udp.local'; } if (!('timeout' in cli)) { cli.timeout = 3000; } } else { if (!cli.port) { cli.port = cli.p = 53; } } var handlers = {}; var server = dgram.createSocket({ type: cli.udp6 ? 'udp6' : 'udp4' //, reuseAddr: true }); server.bind({ port: cli.port , address: cli.address }); handlers.onError = function (err) { console.error("error:", err.stack); server.close(); }; handlers.onMessage = function (nb) { var queryAb = nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength); var query = dnsjs.DNSPacket.parse(queryAb); if (cli.debug) { console.log(''); console.log('DNS Question:'); console.log(''); console.log(query); console.log(''); console.log(hexdump(queryAb)); console.log(''); } console.log(';; Got question:'); console.log(';; ->>HEADER<<-'); console.log(JSON.stringify(query.header)); console.log(''); console.log(';; QUESTION SECTION:'); query.question.forEach(function (q) { console.log(';' + q.name + '.', ' ', q.className, q.typeName); }); function print(q) { var printer = common.printers[q.typeName] || common.printers.ANY; printer(q); } if (query.answer.length) { console.error('[ERROR] Query contains an answer section:'); console.log(';; ANSWER SECTION:'); query.answer.forEach(print); } if (query.authority.length) { console.log(''); console.error('[ERROR] Query contains an authority section:'); console.log(';; AUTHORITY SECTION:'); query.authority.forEach(print); } if (query.additional.length) { console.log(''); console.error('[ERROR] Query contains an additional section:'); console.log(';; ADDITIONAL SECTION:'); query.additional.forEach(print); } console.log(''); console.log(';; MSG SIZE rcvd: ' + nb.byteLength); console.log(''); if (cli.output) { console.log(''); common.writeQuery(cli, query, queryAb); //common.writeResponse(opts, query, nb, packet); } }; handlers.onListening = function () { /*jshint validthis:true*/ var server = this; var nameserver = cli.nameserver; var nameservers; var index; if (!nameserver) { nameservers = require('dns').getServers(); index = crypto.randomBytes(2).readUInt16BE(0) % nameservers.length; nameserver = nameservers[index]; if (cli.debug) { console.log(index, nameservers); } } if (cli.mdns || '224.0.0.251' === cli.nameserver) { server.setBroadcast(true); server.addMembership(cli.nameserver); } console.log(''); console.log('Bound and Listening:'); console.log(server.address().address + '#' + server.address().port); }; console.log(''); if (!cli.nocmd) { console.log('; <<>> dig.js ' + 'v0.0.0' + ' <<>> ' + process.argv.slice(2)); console.log(';; global options: +cmd'); } server.on('error', handlers.onError); server.on('message', handlers.onMessage); server.on('listening', handlers.onListening); });