write json files too

This commit is contained in:
AJ ONeal 2017-02-23 16:55:24 -07:00
parent 578f57bf10
commit 50cfa7d8a6
3 changed files with 43 additions and 16 deletions

View File

@ -4,14 +4,24 @@ dig.js
Create and capture DNS and mDNS query and response packets to disk as binary and/or JSON.
Options are similar to the Unix `dig` command.
Install
Install with git
-------
```bash
# Install the latest of v1.x
npm install -g 'git+https://git@git.daplie.com/Daplie/dig.js.git#v1'
```
If you don't have `git` installed you can also try the npm repo:
```bash
# Install exactly v1.0.0
npm install -g 'git+https://git@git.daplie.com/Daplie/dig.js.git#v1.0.0'
```
Install without git
-------
Don't have git? Well, you can also bow down to the gods of the centralized, monopolized, concentrated, *dictator*net
(as we like to call it here at Daplie Labs), if that's how you roll:
```bash
npm install -g dig.js

View File

@ -1,6 +1,6 @@
'use strict';
var dnsjs = require('../../dns-lint');
var dnsjs = require('dns-suite');
var cli = require('cli');
cli.parse({
// 'b': [ false, 'set source IP address (defaults to 0.0.0.0)', 'string' ]
@ -89,30 +89,44 @@ function hexdump(ab) {
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;
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])) {
fullpath = path.join(opts.output, filename);
binpath = path.join(opts.output, binname);
jsonpath = path.join(opts.output, jsonname);
}
fs.writeFileAsync(fullpath, Buffer.from(queryAb)).then(function () {
console.log('wrote ' + queryAb.byteLength + ' bytes to ' + fullpath);
fs.writeFileAsync(binpath, Buffer.from(queryAb)).then(function () {
console.log('wrote ' + queryAb.byteLength + ' bytes to ' + binpath);
});
fs.writeFileAsync(jsonpath, json).then(function () {
console.log('wrote ' + json.length + ' bytes to ' + jsonpath);
});
}
var count = 0;
function writeResponse(opts, query, nb) {
function writeResponse(opts, query, nb, packet) {
var path = require('path');
var filename = query.question[0].name + '.' + query.question[0].typeName.toLowerCase() + '.' + count + '.bin';
var fullpath = opts.output + '.' + filename;
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])) {
fullpath = path.join(opts.output, filename);
binpath = path.join(opts.output, binname);
jsonpath = path.join(opts.output, jsonname);
}
count += 1;
fs.writeFileAsync(fullpath, nb).then(function () {
console.log('wrote ' + nb.length + ' bytes to ' + fullpath);
fs.writeFileAsync(binpath, nb).then(function () {
console.log('wrote ' + nb.byteLength + ' bytes to ' + binpath);
});
fs.writeFileAsync(jsonpath, json).then(function () {
console.log('wrote ' + json.length + ' bytes to ' + jsonpath);
});
}
@ -194,7 +208,7 @@ function request(query, opts) {
if (opts.output) {
console.log('');
writeQuery(opts, query, queryAb);
writeResponse(opts, query, nb);
writeResponse(opts, query, nb, packet);
}
};
handlers.onListening = function () {

View File

@ -22,5 +22,8 @@
"json"
],
"author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
"license": "(MIT OR Apache-2.0)"
"license": "(MIT OR Apache-2.0)",
"dependencies": {
"dns-suite": "git+https://git@git.daplie.com:Daplie/dns-suite#v1"
}
}