refactor as part of #8 compat with native-dns-packet / dns-js

This commit is contained in:
AJ ONeal 2017-01-30 16:30:08 -07:00
parent 0c2184ae00
commit d3fce04a34
5 changed files with 63 additions and 22 deletions

View File

@ -14,7 +14,7 @@ if (!filename) {
var PromiseA = require('bluebird'); var PromiseA = require('bluebird');
var fs = PromiseA.promisifyAll(require('fs')); var fs = PromiseA.promisifyAll(require('fs'));
var pdns = require('../'); var dnsjs = require('../').DNSPacket;
fs.readFileAsync(filename, null).then(function (nb) { fs.readFileAsync(filename, null).then(function (nb) {
// //
@ -30,23 +30,8 @@ fs.readFileAsync(filename, null).then(function (nb) {
// nb is a Uint8Array (ArrayBufferView) for nb.buffer // nb is a Uint8Array (ArrayBufferView) for nb.buffer
// nb.buffer is the actual ArrayBuffer // nb.buffer is the actual ArrayBuffer
var packet = pdns.unpack(nb.buffer);
function tryParseRdata(record) { var packet = dnsjs.parse(nb.buffer);
record.data = null;
record.error = null;
try {
record.data = pdns.unpackRdata(nb.buffer, packet, record);
} catch (e) {
console.error('[Error] unpackRdata: ' + e.message);
record.error = e;
}
}
packet.answers.forEach(tryParseRdata);
packet.authority.forEach(tryParseRdata);
packet.additional.forEach(tryParseRdata);
console.log('[packet]', nb.byteLength, 'bytes:'); console.log('[packet]', nb.byteLength, 'bytes:');
console.log(JSON.stringify(packet, null, 2)); console.log(JSON.stringify(packet, null, 2));

45
dns.js Normal file
View File

@ -0,0 +1,45 @@
;(function (exports) {
'use strict';
// Value: IP Address
// Meaning:Use: 16 octets represting the IP address
var Parser = (exports.DNS_PARSER || require('./dns.parser.js').DNS_PARSER);
var Packer = (exports.DNS_PACKER || require('./dns.packer.js').DNS_PACKER);
exports.DNSPacket = {
parse: function (nb) {
// backwards compat with node buffer
var ab = nb.buffer || nb;
var packet = Parser.unpack(ab);
function tryParseRdata(record) {
record.data = null;
record.error = null;
try {
record.data = Parser.unpackRdata(ab, packet, record);
} catch (e) {
console.error('[Error] unpackRdata: ' + e.message);
record.error = e;
}
}
packet.answers.forEach(tryParseRdata);
packet.authority.forEach(tryParseRdata);
packet.additional.forEach(tryParseRdata);
return packet;
}
, write: function (json) {
return Packer.pack(json);
}
};
if ('undefined' !== typeof module) {
// backwards compat node dns.js
exports.Parser = exports.DNS_PARSER;
exports.Packer = exports.DNS_PACKER;
}
}('undefined' !== typeof window ? window : exports));

10
dns.packer.js Normal file
View File

@ -0,0 +1,10 @@
(function (exports) {
'use strict';
exports.DNS_PACKER = {
pack: function () {
throw new Error("DNS Packer Not Implemented");
}
};
}('undefined' !== typeof window ? window : exports));

View File

@ -1,7 +1,7 @@
;(function (exports) { ;(function (exports) {
'use strict'; 'use strict';
var pdns = module.exports; var pdns = module.exports.DNS_PARSER = {};
var classes = exports.DNS_CLASSES || require('./dns.classes.js').DNS_CLASSES; var classes = exports.DNS_CLASSES || require('./dns.classes.js').DNS_CLASSES;
var types = exports.DNS_TYPES || require('./dns.types.js').DNS_TYPES; var types = exports.DNS_TYPES || require('./dns.types.js').DNS_TYPES;
@ -211,4 +211,5 @@ pdns.unpack = function (ab) {
return header; return header;
}; };
pdns.unpackRdata = require('./dns.rdata.parse.js').DNS_RDATA_PARSE; pdns.unpackRdata = require('./dns.rdata.parse.js').DNS_RDATA_PARSE;
}('undefined' !== typeof window ? window : exports)); }('undefined' !== typeof window ? window : exports));

View File

@ -1,15 +1,15 @@
{ {
"name": "dns-testing", "name": "dns-lint",
"version": "1.0.0", "version": "1.0.0",
"description": "testing dns", "description": "testing dns",
"main": "pure-parser.js", "main": "dns.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git@git.daplie.com:Daplie/DNS_test.git" "url": "git@git.daplie.com:Daplie/dns-lint.git"
}, },
"author": "", "author": "",
"license": "ISC" "license": "(MIT or Apache2)"
} }