dns-suite.js/bin/dns-pack.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-09-26 00:20:32 +00:00
'use strict';
// EXAMPLE:
// node bin/dns-parse.js samples/a-0.mdns.bin
2017-09-26 21:58:38 +00:00
var path = require('path');
2017-09-26 00:20:32 +00:00
// pass a terminal arg
var filename = process.argv[2];
2017-09-26 21:58:38 +00:00
var outname = process.argv[3];
2017-09-26 00:20:32 +00:00
if (!filename) {
2017-09-26 21:58:38 +00:00
console.error("Usage: node bin/dns-pack.js <path/to/sample.json> <path/to/output.bin>");
console.error("Example: node bin/dns-pack.js ./samples/services-0.mdns.json ./services-0.mdns.bin");
2017-09-26 00:20:32 +00:00
process.exit(1);
2017-09-26 21:58:38 +00:00
return;
}
if (!outname) {
console.warn('');
console.warn(
"Usage: node bin/dns-pack.js '" + filename + "' './" + path.basename(filename).replace(path.extname(filename), '') + ".bin'"
);
console.warn('');
2017-09-26 00:20:32 +00:00
}
var PromiseA = require('bluebird');
var fs = PromiseA.promisifyAll(require('fs'));
var dnsjs = require('../').DNSPacket;
fs.readFileAsync(filename, null).then(function (nb) {
//
// current reference impl
//
//console.log(require('native-dns-packet').parse(nb));
//
// other reference impl
//
//console.log(require('dns-js').DNSPacket.parse(nb));
// nb is a Uint8Array (ArrayBufferView) for nb.buffer
// nb.buffer is the actual ArrayBuffer
//var ab = nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength);
2017-09-26 21:58:38 +00:00
var packet = dnsjs.write(JSON.parse(nb.toString('ascii')));
2017-09-26 00:20:32 +00:00
//console.log('[packet]', nb.byteLength, 'bytes:');
//console.log(JSON.stringify(packet, null, 2));
//TODO hexdump packet
2017-09-26 21:58:38 +00:00
var hexdump = require('hexdump.js').hexdump;
var str = hexdump(packet);
console.log(str);
if (outname) {
fs.writeFileSync(outname, packet, null);
console.log('');
console.log("wrote '" + outname + "'");
}
2017-09-26 00:20:32 +00:00
});