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

69 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2017-09-27 23:29:27 +00:00
#!/usr/bin/env node
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) {
2019-10-20 05:52:59 +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'
);
process.exit(1);
return;
2017-09-26 21:58:38 +00:00
}
if (!outname) {
2019-10-20 05:52:59 +00:00
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;
2019-10-20 05:52:59 +00:00
fs.readFileAsync(filename, null).then(function(nb) {
//
// current reference impl
//
//console.log(require('native-dns-packet').parse(nb));
2017-09-26 00:20:32 +00:00
2019-10-20 05:52:59 +00:00
//
// other reference impl
//
//console.log(require('dns-js').DNSPacket.parse(nb));
2017-09-26 00:20:32 +00:00
2019-10-20 05:52:59 +00:00
// nb is a Uint8Array (ArrayBufferView) for nb.buffer
// nb.buffer is the actual ArrayBuffer
2017-09-26 00:20:32 +00:00
2019-10-20 05:52:59 +00:00
//var ab = nb.buffer.slice(nb.byteOffset, nb.byteOffset + nb.byteLength);
var packet = dnsjs.write(JSON.parse(nb.toString('ascii')));
2017-09-26 00:20:32 +00:00
2019-10-20 05:52:59 +00:00
//console.log('[packet]', nb.byteLength, 'bytes:');
//console.log(JSON.stringify(packet, null, 2));
2017-09-26 00:20:32 +00:00
2019-10-20 05:52:59 +00:00
//TODO hexdump packet
var hexdump = require('@root/hexdump');
var str = hexdump(packet);
2017-09-26 21:58:38 +00:00
2019-10-20 05:52:59 +00:00
console.log(str);
2017-09-26 21:58:38 +00:00
2019-10-20 05:52:59 +00:00
if (outname) {
fs.writeFileSync(outname, packet, null);
console.log('');
console.log("wrote '" + outname + "'");
}
2017-09-26 00:20:32 +00:00
});