now captures packets, yay!

This commit is contained in:
AJ ONeal 2017-02-17 20:18:19 -07:00
parent e23046f736
commit 578f57bf10
2 changed files with 37 additions and 13 deletions

View File

@ -62,8 +62,9 @@ Options
```
--debug
--mdns
-t <type> (superfluous)
-c <class>
-p <port>
-q <query> (superfluous)
--output <path/to/file> write query and response(s) to disk with this path prefix (ex: ./samples/dns)
-t <type> (superfluous) 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

@ -87,6 +87,35 @@ function hexdump(ab) {
return head + '\n' + str + '\n' + trail;
}
function writeQuery(opts, query, queryAb) {
var path = require('path');
var filename = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.query.bin';
var fullpath = opts.output + '.' + filename;
if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) {
fullpath = path.join(opts.output, filename);
}
fs.writeFileAsync(fullpath, Buffer.from(queryAb)).then(function () {
console.log('wrote ' + queryAb.byteLength + ' bytes to ' + fullpath);
});
}
var count = 0;
function writeResponse(opts, query, nb) {
var path = require('path');
var filename = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + count + '.bin';
var fullpath = opts.output + '.' + filename;
if (-1 !== ['.', '/', '\\' ].indexOf(opts.output[opts.output.length -1])) {
fullpath = path.join(opts.output, filename);
}
count += 1;
fs.writeFileAsync(fullpath, nb).then(function () {
console.log('wrote ' + nb.length + ' bytes to ' + fullpath);
});
}
function request(query, opts) {
var queryAb = dnsjs.DNSPacket.write(query);
@ -102,7 +131,6 @@ function request(query, opts) {
console.log('');
}
var count = 0;
var handlers = {};
var server = dgram.createSocket({
type: 'udp4'
@ -164,14 +192,9 @@ function request(query, opts) {
console.log('');
if (opts.output) {
var filename = packet.question[0].typeName + '-' + count + '.bin';
var fullpath = opts.output + filename;
count += 1;
return fs.writeFileAsync(fullpath, nb).then(function () {
console.log('wrote ' + nb.length + ' bytes to ' + fullpath);
});
console.log('');
writeQuery(opts, query, queryAb);
writeResponse(opts, query, nb);
}
};
handlers.onListening = function () {