Compare commits

...

4 Commits

Author SHA1 Message Date
Fabian Stamm 6dad6c5d56 Adding tests 2018-05-17 10:02:17 +02:00
Fabian Stamm 4e3cf05d29 Refactoring and adding Question class 2018-05-17 10:01:56 +02:00
Fabian Stamm a34cd4bac2 Removing port choice on listener
Listening ports are now fixed to 53 on udp and tcp
2018-05-17 09:57:51 +02:00
Fabian Stamm c57260e11b Adding .editorconfig 2018-05-17 09:31:48 +02:00
18 changed files with 1336 additions and 523 deletions

9
.editorconfig Normal file
View File

@ -0,0 +1,9 @@
root = true
[*]
indent_style = space
indent_size = 3
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

2
lib/listener.d.ts vendored
View File

@ -2,6 +2,6 @@ import { Request } from "./request";
export default class Listener {
private udp;
private tcp;
constructor(type: "udp" | "tcp", onRequest: (request: Request) => any, port: number, host?: string);
constructor(type: "udp" | "tcp", onRequest: (request: Request) => any, host?: string);
close(): void;
}

View File

@ -4,12 +4,12 @@ const net = require("net");
const dgram = require("dgram");
const request_1 = require("./request");
class Listener {
constructor(type, onRequest, port, host = "0.0.0.0") {
constructor(type, onRequest, host = "0.0.0.0") {
switch (type) {
case "udp":
this.udp = dgram.createSocket("udp4");
this.udp.on("listening", () => {
console.log(`UDP Server Listening on ${port}`);
console.log(`UDP Server Listening on 53`);
});
this.udp.on("message", (message, remote) => {
let request = new request_1.Request(message, (data) => {
@ -18,7 +18,7 @@ class Listener {
});
onRequest(request);
});
this.udp.bind(port, host);
this.udp.bind(53, host);
break;
case "tcp":
console.log("Not correct implemented");
@ -50,7 +50,8 @@ class Listener {
}
});
});
this.tcp.listen(port, host);
this.tcp.listen(53, host);
console.log(`TCP Server Listening on 53`);
break;
default:
throw new Error("Unknown socket type");

View File

@ -1 +1 @@
{"version":3,"file":"listener.js","sourceRoot":"","sources":["../src/listener.ts"],"names":[],"mappings":";;AAAA,2BAA0B;AAC1B,+BAA+B;AAC/B,uCAAoC;AAEpC;IAGG,YAAY,IAAmB,EAAE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AAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;SAC3C;IACJ,CAAC;IAED,KAAK;QACF,IAAI,IAAI,CAAC,GAAG,EAAE;YACX,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;SACnB;aAAM;YACJ,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;SACnB;IACJ,CAAC;CACH;AAlED,2BAkEC"}

137
lib/request.d.ts vendored
View File

@ -1,113 +1,49 @@
/// <reference types="node" />
import { Message, MessageHeader, MessageQuestion, MessageRecourceRecord, ErrorCodes } from "./types";
export declare enum QueryTypes {
/**
* IPv4 address
*/
A = 1,
/**
* Nameserver
*/
NS = 2,
/**
* Obsolete
*/
MD = 3,
/**
* Obsolete
*/
MF = 4,
/**
* Alias
*/
CNAME = 5,
/**
* Start of authority
*/
SOA = 6,
/**
* Experimental
*/
MB = 7,
/**
* Experimental
*/
MG = 8,
/**
* Experimental
*/
MR = 9,
/**
* Experimental
*/
NULL = 10,
/**
* Service description
*/
WKS = 11,
/**
* Reverse entry (inaddr.arpa)
*/
PTR = 12,
/**
* Host information
*/
HINFO = 13,
/**
* Mailbox / Mail-list information
*/
MINFO = 14,
/**
* Mail exchange
*/
MX = 15,
/**
* Text strings
*/
TXT = 16,
/**
* IPv6 address
*/
AAAA = 28,
/**
* SRV records
*/
SRV = 33,
/**
* Request to transfer entire zone
*/
AXFR = 252,
/**
* Request for mailbox related records
*/
MAILA = 254,
/**
* Request for mail agend RRs
*/
MAILB = 253,
/**
* Any class
*/
ANY = 255,
}
export declare function SerializeName(name: string): Buffer;
export declare class Request implements Message {
import { IMessage, IMessageHeader, IMessageQuestion, MessageRecourceRecord, ErrorCodes } from "./types";
export declare function parseHeader(data: Buffer): IMessageHeader;
export declare function parseQuestions(count: number, packet: Buffer): IMessageQuestion[];
export declare function serializeName(name: string): Buffer;
export declare class Request implements IMessage {
private sendCallback;
private max_size;
private _header;
readonly header: MessageHeader;
readonly header: Header;
private _questions;
readonly questions: MessageQuestion[];
readonly questions: Question[];
answers: RecourceRecord[];
authorities: RecourceRecord[];
additionals: RecourceRecord[];
constructor(packet: Buffer, sendCallback: (packet: Buffer) => any);
constructor(packet: Buffer, sendCallback: (packet: Buffer) => any, max_size?: number);
error(error: ErrorCodes): void;
noRecursion(): void;
send(): void;
serialize(truncate?: boolean, rcode?: 0 | 1 | 2 | 3 | 4 | 5): Buffer;
private serializeHeader();
private serializeQuestion(question);
private serializeResourceRecord(record);
serialize(): Buffer;
}
export declare class Header implements IMessageHeader {
ID: number;
QR: 0 | 1;
OPCODE: number;
AA: 0 | 1;
TC: 0 | 1;
RD: 0 | 1;
RA: 0 | 1;
Z: 0 | 1;
AD: 0 | 1;
CD: 0 | 1;
RCODE: ErrorCodes;
QDCOUNT: number;
ANCOUNT: number;
NSCOUNT: number;
ARCOUNT: number;
constructor(header: IMessageHeader);
serialize(): Buffer;
}
export declare class Question implements IMessageQuestion {
QNAME: string;
QTYPE: number;
QCLASS: number;
constructor(question: IMessageQuestion);
serialize(): Buffer;
}
export declare class RecourceRecord implements MessageRecourceRecord {
/**
@ -123,4 +59,5 @@ export declare class RecourceRecord implements MessageRecourceRecord {
TTL: number;
RDATA: Buffer;
readonly RDLENGTH: number;
serialize(): Buffer;
}

View File

@ -2,98 +2,6 @@
Object.defineProperty(exports, "__esModule", { value: true });
const binary_parser_1 = require("binary-parser");
const types_1 = require("./types");
const MAX_LABEL_SIZE = 63;
var QueryTypes;
(function (QueryTypes) {
/**
* IPv4 address
*/
QueryTypes[QueryTypes["A"] = 1] = "A";
/**
* Nameserver
*/
QueryTypes[QueryTypes["NS"] = 2] = "NS";
/**
* Obsolete
*/
QueryTypes[QueryTypes["MD"] = 3] = "MD";
/**
* Obsolete
*/
QueryTypes[QueryTypes["MF"] = 4] = "MF";
/**
* Alias
*/
QueryTypes[QueryTypes["CNAME"] = 5] = "CNAME";
/**
* Start of authority
*/
QueryTypes[QueryTypes["SOA"] = 6] = "SOA";
/**
* Experimental
*/
QueryTypes[QueryTypes["MB"] = 7] = "MB";
/**
* Experimental
*/
QueryTypes[QueryTypes["MG"] = 8] = "MG";
/**
* Experimental
*/
QueryTypes[QueryTypes["MR"] = 9] = "MR";
/**
* Experimental
*/
QueryTypes[QueryTypes["NULL"] = 10] = "NULL";
/**
* Service description
*/
QueryTypes[QueryTypes["WKS"] = 11] = "WKS";
/**
* Reverse entry (inaddr.arpa)
*/
QueryTypes[QueryTypes["PTR"] = 12] = "PTR";
/**
* Host information
*/
QueryTypes[QueryTypes["HINFO"] = 13] = "HINFO";
/**
* Mailbox / Mail-list information
*/
QueryTypes[QueryTypes["MINFO"] = 14] = "MINFO";
/**
* Mail exchange
*/
QueryTypes[QueryTypes["MX"] = 15] = "MX";
/**
* Text strings
*/
QueryTypes[QueryTypes["TXT"] = 16] = "TXT";
/**
* IPv6 address
*/
QueryTypes[QueryTypes["AAAA"] = 28] = "AAAA";
/**
* SRV records
*/
QueryTypes[QueryTypes["SRV"] = 33] = "SRV";
/**
* Request to transfer entire zone
*/
QueryTypes[QueryTypes["AXFR"] = 252] = "AXFR";
/**
* Request for mailbox related records
*/
QueryTypes[QueryTypes["MAILA"] = 254] = "MAILA";
/**
* Request for mail agend RRs
*/
QueryTypes[QueryTypes["MAILB"] = 253] = "MAILB";
/**
* Any class
*/
QueryTypes[QueryTypes["ANY"] = 255] = "ANY";
})(QueryTypes = exports.QueryTypes || (exports.QueryTypes = {}));
const headerParser = new binary_parser_1.Parser()
.endianess("big")
.uint16("ID")
@ -111,6 +19,15 @@ const headerParser = new binary_parser_1.Parser()
.uint16("ANCOUNT")
.uint16("NSCOUNT")
.uint16("ARCOUNT");
function parseHeader(data) {
try {
return headerParser.parse(data);
}
catch (e) {
throw new Error("Header parsing failed" + e.message);
}
}
exports.parseHeader = parseHeader;
const labelParser = new binary_parser_1.Parser()
.endianess("big")
.uint8("dataLength")
@ -133,14 +50,21 @@ const questionParser = new binary_parser_1.Parser()
.uint16("QTYPE")
.uint16("QCLASS");
function parseQuestions(count, packet) {
return new binary_parser_1.Parser()
.endianess("big")
.array("questions", {
type: questionParser,
length: count
}).parse(packet).questions;
try {
return new binary_parser_1.Parser()
.endianess("big")
.array("questions", {
type: questionParser,
length: count
}).parse(packet).questions;
}
catch (e) {
throw new Error("Question parsing failed" + e.message);
}
}
function SerializeName(name) {
exports.parseQuestions = parseQuestions;
const MAX_LABEL_SIZE = 63;
function serializeName(name) {
let length = 0;
let parts = name.split(".");
parts.forEach(e => {
@ -161,10 +85,11 @@ function SerializeName(name) {
data.writeUInt8(0, offset);
return data;
}
exports.SerializeName = SerializeName;
exports.serializeName = serializeName;
class Request {
constructor(packet, sendCallback) {
constructor(packet, sendCallback, max_size = 512) {
this.sendCallback = sendCallback;
this.max_size = max_size;
this.answers = [];
this.authorities = [];
this.additionals = [];
@ -172,17 +97,17 @@ class Request {
packet.copy(headerData, 0, 0, 12);
let bodyData = Buffer.alloc(packet.length - 12);
packet.copy(bodyData, 0, 12, packet.length);
this._header = headerParser.parse(headerData);
this._header = new Header(parseHeader(headerData));
this._header.AD = 0;
this._header.RCODE = types_1.ErrorCodes.NoError;
this._header.RA = this._header.RD;
this._questions = parseQuestions(this._header.QDCOUNT, bodyData);
this._questions = parseQuestions(this._header.QDCOUNT, bodyData).map(e => new Question(e));
}
get header() {
return Object.assign({}, this._header);
}
get questions() {
return this._questions.map(e => e);
return this._questions.map(e => Object.assign({}, e));
}
error(error) {
if (this._header.RCODE === types_1.ErrorCodes.NoError)
@ -194,37 +119,43 @@ class Request {
send() {
this.sendCallback(this.serialize());
}
serialize(truncate = false, rcode = 0) {
serialize() {
this._header.AA = 1;
this._header.ANCOUNT = this.answers.length;
this._header.ARCOUNT = this.additionals.length;
this._header.NSCOUNT = this.authorities.length;
this._header.QR = 1;
this._header.RCODE = rcode;
let questions = this.questions.map(this.serializeQuestion, this);
let answers = this.answers.map(this.serializeResourceRecord, this);
let authority = this.authorities.map(this.serializeResourceRecord, this);
let additional = this.additionals.map(this.serializeResourceRecord, this);
let questionsByteLength = 0;
questions.forEach(e => questionsByteLength += e.length);
let answersByteLength = 0;
answers.forEach(e => answersByteLength += e.length);
let authorityByteLength = 0;
authority.forEach(e => authorityByteLength += e.length);
let additionalByteLength = 0;
additional.forEach(e => additionalByteLength += e.length);
let length = 12 + questionsByteLength + answersByteLength + authorityByteLength + additionalByteLength; //Header is always 12 byte large
if (truncate && length > 512) {
let questions = this._questions.map(e => e.serialize());
let answers = this.answers.map(e => e.serialize());
let authority = this.authorities.map(e => e.serialize());
let additional = this.additionals.map(e => e.serialize());
let length = 12;
questions.forEach(e => length += e.length);
answers.forEach(e => length += e.length);
authority.forEach(e => length += e.length);
additional.forEach(e => length += e.length);
// let questionsByteLength = 0;
// questions.forEach(e => questionsByteLength += e.length);
// let answersByteLength = 0;
// answers.forEach(e => answersByteLength += e.length)
// let authorityByteLength = 0;
// authority.forEach(e => authorityByteLength += e.length)
// let additionalByteLength = 0;
// additional.forEach(e => additionalByteLength += e.length)
// let length = 12 + questionsByteLength + answersByteLength + authorityByteLength + additionalByteLength; //Header is always 12 byte large
if (length > this.max_size) {
this._header.TC = 1;
//Buffer will ignore data that exeeds the max buffer length
length = 512;
//Will ignore data, that exceeds length
length = this.max_size;
}
let header = this.serializeHeader();
let header = this._header.serialize();
let data = Buffer.alloc(length);
let offset = 0;
let append = (buffer) => {
buffer.copy(data, offset, 0, buffer.length);
offset += buffer.length;
if (offset <= length) {
buffer.copy(data, offset, 0, buffer.length);
offset += buffer.length;
}
};
append(header);
questions.forEach(append);
@ -233,57 +164,55 @@ class Request {
additional.forEach(append);
return data;
}
serializeHeader() {
let header = this.header;
}
exports.Request = Request;
class Header {
constructor(header) {
for (let k in header) {
this[k] = header[k];
}
}
serialize() {
let data = Buffer.alloc(12);
data.writeUInt16BE(header.ID, 0);
data.writeUInt16BE(this.ID, 0);
var f = 0x0000;
f = f | (header.QR << 15);
f = f | (header.OPCODE << 11);
f = f | (header.AA << 10);
f = f | (header.TC << 9);
f = f | (header.RD << 8);
f = f | (header.RA << 7);
f = f | (header.Z << 6);
f = f | (header.AD << 5);
f = f | (header.CD << 4);
f = f | header.RCODE;
f = f | (this.QR << 15);
f = f | (this.OPCODE << 11);
f = f | (this.AA << 10);
f = f | (this.TC << 9);
f = f | (this.RD << 8);
f = f | (this.RA << 7);
f = f | (this.Z << 6);
f = f | (this.AD << 5);
f = f | (this.CD << 4);
f = f | this.RCODE;
data.writeUInt16BE(f, 2);
data.writeUInt16BE(header.QDCOUNT, 4);
data.writeUInt16BE(header.ANCOUNT, 6);
data.writeUInt16BE(header.NSCOUNT, 8);
data.writeUInt16BE(header.ARCOUNT, 10);
return data;
}
serializeQuestion(question) {
let qname = SerializeName(question.QNAME);
let data = Buffer.alloc(qname.length + 4);
qname.copy(data, 0, 0, qname.length);
let offset = qname.length;
data.writeUInt16BE(question.QTYPE, offset);
offset += 2;
data.writeUInt16BE(question.QCLASS, offset);
return data;
}
serializeResourceRecord(record) {
// TODO: Implement compression
let name = SerializeName(record.NAME);
let data = Buffer.alloc(name.length + 10 + record.RDLENGTH); // For TYPE, CLASS, TTL, RLENGTH
name.copy(data, 0, 0, name.length);
let offset = name.length;
data.writeUInt16BE(record.TYPE, offset);
offset += 2;
data.writeUInt16BE(record.CLASS, offset);
offset += 2;
data.writeUInt32BE(record.TTL, offset);
offset += 4;
data.writeUInt16BE(record.RDLENGTH, offset);
offset += 2;
record.RDATA.copy(data, offset, 0, record.RDLENGTH);
data.writeUInt16BE(this.QDCOUNT, 4);
data.writeUInt16BE(this.ANCOUNT, 6);
data.writeUInt16BE(this.NSCOUNT, 8);
data.writeUInt16BE(this.ARCOUNT, 10);
return data;
}
}
exports.Request = Request;
exports.Header = Header;
class Question {
constructor(question) {
for (let k in question) {
this[k] = question[k];
}
}
serialize() {
let qname = serializeName(this.QNAME);
let data = Buffer.alloc(qname.length + 4);
qname.copy(data, 0, 0, qname.length);
let offset = qname.length;
data.writeUInt16BE(this.QTYPE, offset);
offset += 2;
data.writeUInt16BE(this.QCLASS, offset);
return data;
}
}
exports.Question = Question;
class RecourceRecord {
set TYPE(value) {
if (value < 0 || value > 65535)
@ -304,6 +233,7 @@ class RecourceRecord {
set TTL(value) {
if (value < 0 || value > 4294967295)
throw new TypeError("TTL Range: 0 - 4.294.967.295");
this._TTL = value;
}
get TTL() {
return this._TTL;
@ -311,6 +241,23 @@ class RecourceRecord {
get RDLENGTH() {
return this.RDATA.length;
}
serialize() {
// TODO: Implement compression
let name = serializeName(this.NAME);
let data = Buffer.alloc(name.length + 10 + this.RDLENGTH); // For TYPE, CLASS, TTL, RLENGTH
name.copy(data, 0, 0, name.length);
let offset = name.length;
data.writeUInt16BE(this.TYPE, offset);
offset += 2;
data.writeUInt16BE(this.CLASS, offset);
offset += 2;
data.writeUInt32BE(this._TTL, offset);
offset += 4;
data.writeUInt16BE(this.RDLENGTH, offset);
offset += 2;
this.RDATA.copy(data, offset, 0, this.RDLENGTH);
return data;
}
}
exports.RecourceRecord = RecourceRecord;
//# sourceMappingURL=request.js.map

File diff suppressed because one or more lines are too long

1
lib/test.d.ts vendored
View File

@ -0,0 +1 @@
export {};

View File

@ -1,6 +1,349 @@
// const t = `71 15 01 00 00 01 00 00 00 00 00 00 02 64 63 08 73 65 72 76 69 63 65 73 0c 76 69 73 75 61 6c 73 74 75 64 69 6f 03 63 6f 6d 00 00 01 00 01`.split(" ").join("");
// // const tp = Buffer.from(t, "hex")
// // // import Parse from "./index";
// // import * as util from "util";
// // console.log(util.inspect(Parse(tp), false, 20, true));
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const request_1 = require("./request");
const types_1 = require("./types");
function fromHex(data) {
return Buffer.from(data.replace(/\s/g, ""), "hex");
}
describe("parser", function () {
describe("header", function () {
describe("header parser", function () {
let should_templ = {
ID: 0,
QR: 0,
OPCODE: 0,
AA: 0,
TC: 0,
RD: 0,
RA: 0,
Z: 0,
AD: 0,
CD: 0,
RCODE: 0,
QDCOUNT: 0,
ANCOUNT: 0,
ARCOUNT: 0,
NSCOUNT: 0
};
let tests = [
{
name: "Testing ID field",
data: "0001 0000 0000 0000 0000 0000",
fields: {
ID: 1
}
},
{
name: "Testing ID field with max value",
data: "FFFF 0000 0000 0000 0000 0000",
fields: {
ID: 65535
}
},
{
name: "Testing QR field",
data: "0000 8000 0000 0000 0000 0000",
fields: {
QR: 1
}
},
{
name: "Testing OPCODE field value 2",
data: "0000 1000 0000 0000 0000 0000",
fields: {
OPCODE: 2
}
},
{
name: "Testing OPCODE field value 1",
data: "0000 0800 0000 0000 0000 0000",
fields: {
OPCODE: 1
}
},
{
name: "Testing AA field",
data: "0000 0400 0000 0000 0000 0000",
fields: {
AA: 1
}
},
{
name: "Testing TC field",
data: "0000 0200 0000 0000 0000 0000",
fields: {
TC: 1
}
},
{
name: "Testing RD field",
data: "0000 0100 0000 0000 0000 0000",
fields: {
RD: 1
}
},
{
name: "Testing RCODE field",
data: "0000 0002 0000 0000 0000 0000",
fields: {
RCODE: 2
}
},
{
name: "Testing QDCOUNT field max value",
data: "0000 0000 FFFF 0000 0000 0000",
fields: {
QDCOUNT: 65535
}
},
{
name: "Testing ANCOUNT field max value",
data: "0000 0000 0000 FFFF 0000 0000",
fields: {
ANCOUNT: 65535
}
},
{
name: "Testing NSCOUNT field max value",
data: "0000 0000 0000 0000 FFFF 0000",
fields: {
NSCOUNT: 65535
}
},
{
name: "Testing ARCOUNT field max value",
data: "0000 0000 0000 0000 0000 FFFF",
fields: {
ARCOUNT: 65535
}
},
{
name: "Testing all Flags and Values max",
data: "FFFF FFFF FFFF FFFF FFFF FFFF",
fields: {
ID: 65535,
QR: 1,
OPCODE: 15,
AA: 1,
TC: 1,
RD: 1,
RA: 1,
Z: 1,
AD: 1,
CD: 1,
RCODE: 15,
QDCOUNT: 65535,
ANCOUNT: 65535,
NSCOUNT: 65535,
ARCOUNT: 65535
}
}
];
tests.forEach(function (e) {
it(e.name, function () {
let testdata = fromHex(e.data);
let should = Object.assign({}, should_templ, e.fields); // Build in "clone" function
let header = request_1.parseHeader(testdata);
chai_1.assert.hasAllKeys(header, Object.keys(should), "Parsed header is missing some fields");
chai_1.assert.deepEqual(header, should, "Parsed header has not expected values!");
});
});
});
describe("header serializer", function () {
let empty_header = {
ID: 0,
QR: 0,
OPCODE: 0,
AA: 0,
TC: 0,
RD: 0,
RA: 0,
Z: 0,
AD: 0,
CD: 0,
RCODE: 0,
QDCOUNT: 0,
ANCOUNT: 0,
ARCOUNT: 0,
NSCOUNT: 0
};
let tests = [
{
name: "Fill header with 0s",
result: "0000 0000 0000 0000 0000 0000",
values: {}
},
{
name: "Set header id to 5",
result: "0005 0000 0000 0000 0000 0000",
values: {
ID: 5
}
},
{
name: "Set QR",
result: "0000 8000 0000 0000 0000 0000",
values: {
QR: 1
}
},
{
name: "Set OPCODE",
result: "0000 7800 0000 0000 0000 0000",
values: {
OPCODE: 15
}
},
{
name: "Set AA",
result: "0000 0400 0000 0000 0000 0000",
values: {
AA: 1
}
},
{
name: "Set TC",
result: "0000 0200 0000 0000 0000 0000",
values: {
TC: 1
}
},
{
name: "Set RD",
result: "0000 0100 0000 0000 0000 0000",
values: {
RD: 1
}
},
{
name: "Set RA",
result: "0000 0080 0000 0000 0000 0000",
values: {
RA: 1
}
},
{
name: "Set Z",
result: "0000 0040 0000 0000 0000 0000",
values: {
Z: 1
}
},
{
name: "Set AD",
result: "0000 0020 0000 0000 0000 0000",
values: {
AD: 1
}
},
{
name: "Set CD",
result: "0000 0010 0000 0000 0000 0000",
values: {
CD: 1
}
},
{
name: "Set RCODE",
result: "0000 000F 0000 0000 0000 0000",
values: {
RCODE: 15
}
},
{
name: "Set QDCOUNT",
result: "0000 0000 FFFF 0000 0000 0000",
values: {
QDCOUNT: 65535
}
},
{
name: "Set ANCOUNT",
result: "0000 0000 0000 FFFF 0000 0000",
values: {
ANCOUNT: 65535
}
},
{
name: "Set NSCOUNT",
result: "0000 0000 0000 0000 FFFF 0000",
values: {
NSCOUNT: 65535
}
},
{
name: "Set ARCOUNT",
result: "0000 0000 0000 0000 0000 FFFF",
values: {
ARCOUNT: 65535
}
},
];
tests.forEach(function (e) {
it(e.name, function () {
let header = Object.assign({}, empty_header, e.values);
let serialized = new request_1.Header(header).serialize();
chai_1.assert.equal(serialized.toString("hex"), e.result.replace(/\s/g, "").toLowerCase(), "Header serialization failed");
});
});
});
});
describe("question", function () {
let questionData = fromHex("0474 6573 7407 6578 616d 706c 6503 636f 6d00 0001 0001");
let questionObj = {
QNAME: "test.example.com",
QTYPE: types_1.QueryTypes.A,
QCLASS: 1
};
it("check question parser with one question", function () {
let res = request_1.parseQuestions(1, questionData);
let should = [questionObj];
chai_1.assert.deepEqual(res, should, "Question parser does not parse input correctly");
});
it("check question serialization", function () {
let res = new request_1.Question(questionObj).serialize();
chai_1.assert.equal(res.toString("hex"), questionData.toString("hex"), "Query serializer does not serialite correctly");
});
});
it("recource record serialization", function () {
let should = "07 6578616D706C65 03 636f6D 00 0001 0001 00000640 0004 0A000001";
let rr = new request_1.RecourceRecord();
rr.CLASS = 1;
rr.NAME = "example.com";
rr.TTL = 1600;
rr.TYPE = 1;
rr.RDATA = fromHex("0A 00 00 01");
let res = rr.serialize().toString("hex");
chai_1.assert.equal(res, should.replace(/\s/g, "").toLowerCase(), "Serialization not working properly");
});
it("full response serialization", function () {
let reqData = fromHex("E835 0100 0001 0000 0000 0000 07 6578616D706c65 03636F6D 00 0001 0001");
let should = "E835 8580 0001000100000000 076578616D706C6503636F6D0000010001 07 6578616D706C65 03 636F6D 00 0001 0001 0000 0640 0004 0A000001";
let request = new request_1.Request(reqData, () => null);
let rr = new request_1.RecourceRecord();
rr.CLASS = 1;
rr.NAME = "example.com";
rr.TTL = 1600;
rr.TYPE = 1;
rr.RDATA = fromHex("0A 00 00 01");
request.answers.push(rr);
let data = request.serialize();
chai_1.assert.equal(data.toString("hex"), should.replace(/\s/g, "").toLowerCase(), "Whole packet serialization failed");
});
// it("full response serialization benchmark", function () {
// let reqData = fromHex("E835 0100 0001 0000 0000 0000 07 6578616D706c65 03636F6D 00 0001 0001");
// for (let i = 0; i < 100; i++) {
// let request = new Request(reqData, () => null)
// let rr = new RecourceRecord()
// rr.CLASS = 1
// rr.NAME = "example.com"
// rr.TTL = 1600
// rr.TYPE = 1
// rr.RDATA = fromHex("0A 00 00 01")
// request.answers.push(rr)
// request.serialize()
// }
// })
});
//# sourceMappingURL=test.js.map

File diff suppressed because one or more lines are too long

102
lib/types.d.ts vendored
View File

@ -25,7 +25,97 @@ export declare enum ErrorCodes {
*/
Refused = 5,
}
export interface MessageHeader {
export declare enum QueryTypes {
/**
* IPv4 address
*/
A = 1,
/**
* Nameserver
*/
NS = 2,
/**
* Obsolete
*/
MD = 3,
/**
* Obsolete
*/
MF = 4,
/**
* Alias
*/
CNAME = 5,
/**
* Start of authority
*/
SOA = 6,
/**
* Experimental
*/
MB = 7,
/**
* Experimental
*/
MG = 8,
/**
* Experimental
*/
MR = 9,
/**
* Experimental
*/
NULL = 10,
/**
* Service description
*/
WKS = 11,
/**
* Reverse entry (inaddr.arpa)
*/
PTR = 12,
/**
* Host information
*/
HINFO = 13,
/**
* Mailbox / Mail-list information
*/
MINFO = 14,
/**
* Mail exchange
*/
MX = 15,
/**
* Text strings
*/
TXT = 16,
/**
* IPv6 address
*/
AAAA = 28,
/**
* SRV records
*/
SRV = 33,
/**
* Request to transfer entire zone
*/
AXFR = 252,
/**
* Request for mailbox related records
*/
MAILA = 254,
/**
* Request for mail agend RRs
*/
MAILB = 253,
/**
* Any class
*/
ANY = 255,
}
export interface IMessageHeader {
/**
* A 16 bit identifier assigned by the program that
* generates any kind of query. This identifier is copied
@ -73,7 +163,7 @@ export interface MessageHeader {
/**
* Reserved for future usage, must be 0 in all queries
*/
Z: 0;
Z: 0 | 1;
AD: 0 | 1;
CD: 0 | 1;
/**
@ -109,7 +199,7 @@ export interface MessageHeader {
*/
ARCOUNT: number;
}
export interface MessageQuestion {
export interface IMessageQuestion {
/**
* Domain name represented as sequence of labels
* Each label consists of a length octed followed
@ -169,9 +259,9 @@ export interface MessageRecourceRecord {
*/
RDATA: Buffer;
}
export interface Message {
header: MessageHeader;
questions: MessageQuestion[];
export interface IMessage {
header: IMessageHeader;
questions: IMessageQuestion[];
answers: MessageRecourceRecord[];
authorities: MessageRecourceRecord[];
additionals: MessageRecourceRecord[];

View File

@ -1,13 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// 0 No error condition
// * 1 Format error - unable to interpret query
// * 2 Server failure - internal problem
// * 3 Name error - Only for authorative name server, domain name of query does not exist
// * 4 Not implemented - Request not supported
// * 5 Refused - Nameserver refuses request
// * 6 - 15 Reserved for future usage
// *
var ErrorCodes;
(function (ErrorCodes) {
/**
@ -35,4 +27,95 @@ var ErrorCodes;
*/
ErrorCodes[ErrorCodes["Refused"] = 5] = "Refused";
})(ErrorCodes = exports.ErrorCodes || (exports.ErrorCodes = {}));
var QueryTypes;
(function (QueryTypes) {
/**
* IPv4 address
*/
QueryTypes[QueryTypes["A"] = 1] = "A";
/**
* Nameserver
*/
QueryTypes[QueryTypes["NS"] = 2] = "NS";
/**
* Obsolete
*/
QueryTypes[QueryTypes["MD"] = 3] = "MD";
/**
* Obsolete
*/
QueryTypes[QueryTypes["MF"] = 4] = "MF";
/**
* Alias
*/
QueryTypes[QueryTypes["CNAME"] = 5] = "CNAME";
/**
* Start of authority
*/
QueryTypes[QueryTypes["SOA"] = 6] = "SOA";
/**
* Experimental
*/
QueryTypes[QueryTypes["MB"] = 7] = "MB";
/**
* Experimental
*/
QueryTypes[QueryTypes["MG"] = 8] = "MG";
/**
* Experimental
*/
QueryTypes[QueryTypes["MR"] = 9] = "MR";
/**
* Experimental
*/
QueryTypes[QueryTypes["NULL"] = 10] = "NULL";
/**
* Service description
*/
QueryTypes[QueryTypes["WKS"] = 11] = "WKS";
/**
* Reverse entry (inaddr.arpa)
*/
QueryTypes[QueryTypes["PTR"] = 12] = "PTR";
/**
* Host information
*/
QueryTypes[QueryTypes["HINFO"] = 13] = "HINFO";
/**
* Mailbox / Mail-list information
*/
QueryTypes[QueryTypes["MINFO"] = 14] = "MINFO";
/**
* Mail exchange
*/
QueryTypes[QueryTypes["MX"] = 15] = "MX";
/**
* Text strings
*/
QueryTypes[QueryTypes["TXT"] = 16] = "TXT";
/**
* IPv6 address
*/
QueryTypes[QueryTypes["AAAA"] = 28] = "AAAA";
/**
* SRV records
*/
QueryTypes[QueryTypes["SRV"] = 33] = "SRV";
/**
* Request to transfer entire zone
*/
QueryTypes[QueryTypes["AXFR"] = 252] = "AXFR";
/**
* Request for mailbox related records
*/
QueryTypes[QueryTypes["MAILA"] = 254] = "MAILA";
/**
* Request for mail agend RRs
*/
QueryTypes[QueryTypes["MAILB"] = 253] = "MAILB";
/**
* Any class
*/
QueryTypes[QueryTypes["ANY"] = 255] = "ANY";
})(QueryTypes = exports.QueryTypes || (exports.QueryTypes = {}));
//# sourceMappingURL=types.js.map

View File

@ -1 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;AACA,uBAAuB;AACvB,kDAAkD;AAClD,8CAA8C;AAC9C,kGAAkG;AAClG,0DAA0D;AAC1D,0DAA0D;AAC1D,wCAAwC;AACxC,WAAW;AACX,IAAY,UA8BX;AA9BD,WAAY,UAAU;IACnB;;OAEG;IACH,iDAAW,CAAA;IAEX;;OAEG;IACH,yDAAW,CAAA;IAEX;;OAEG;IACH,6DAAa,CAAA;IAEb;;OAEG;IACH,qDAAS,CAAA;IAET;;OAEG;IACH,+DAAc,CAAA;IAEd;;OAEG;IACH,iDAAO,CAAA;AACV,CAAC,EA9BW,UAAU,GAAV,kBAAU,KAAV,kBAAU,QA8BrB"}
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;AAAA,IAAY,UA8BX;AA9BD,WAAY,UAAU;IACnB;;OAEG;IACH,iDAAW,CAAA;IAEX;;OAEG;IACH,yDAAW,CAAA;IAEX;;OAEG;IACH,6DAAa,CAAA;IAEb;;OAEG;IACH,qDAAS,CAAA;IAET;;OAEG;IACH,+DAAc,CAAA;IAEd;;OAEG;IACH,iDAAO,CAAA;AACV,CAAC,EA9BW,UAAU,GAAV,kBAAU,KAAV,kBAAU,QA8BrB;AAED,IAAY,UA8GX;AA9GD,WAAY,UAAU;IACnB;;OAEG;IACH,qCAAQ,CAAA;IAER;;OAEG;IACH,uCAAS,CAAA;IAET;;OAEG;IACH,uCAAS,CAAA;IAET;;OAEG;IACH,uCAAS,CAAA;IAET;;OAEG;IACH,6CAAY,CAAA;IAEZ;;OAEG;IACH,yCAAU,CAAA;IAEV;;OAEG;IACH,uCAAS,CAAA;IAET;;OAEG;IACH,uCAAS,CAAA;IAET;;OAEG;IACH,uCAAS,CAAA;IAET;;OAEG;IACH,4CAAW,CAAA;IAEX;;OAEG;IACH,0CAAU,CAAA;IAEV;;OAEG;IACH,0CAAU,CAAA;IAEV;;OAEG;IACH,8CAAY,CAAA;IAEZ;;OAEG;IACH,8CAAY,CAAA;IAEZ;;OAEG;IACH,wCAAS,CAAA;IAET;;OAEG;IACH,0CAAU,CAAA;IAEV;;OAEG;IACH,4CAAW,CAAA;IAEX;;OAEG;IACH,0CAAU,CAAA;IAEV;;OAEG;IACH,6CAAW,CAAA;IAEX;;OAEG;IACH,+CAAY,CAAA;IAEZ;;OAEG;IACH,+CAAY,CAAA;IAEZ;;OAEG;IACH,2CAAU,CAAA;AACb,CAAC,EA9GW,UAAU,GAAV,kBAAU,KAAV,kBAAU,QA8GrB"}

View File

@ -8,13 +8,19 @@
"scripts": {
"build": "tsc",
"watch": "tsc --watch",
"test": "nodemon lib/test.js"
"test": "mocha src/test.ts --require ts-node/register",
"testwatch": "mocha src/test.ts --require ts-node/register --watch --watch-extensions ts"
},
"devDependencies": {
"@types/binary-parser": "^1.3.0",
"@types/chai": "^4.1.3",
"@types/mocha": "^5.2.0",
"@types/node": "^10.0.8",
"chai": "^4.1.2",
"mocha": "^5.1.1",
"nodemon": "^1.17.4",
"typescript": "^2.8.3"
"typescript": "^2.8.3",
"ts-node": "^6.0.3"
},
"dependencies": {
"binary-parser": "^1.3.2"

View File

@ -2,15 +2,16 @@ import * as net from "net"
import * as dgram from "dgram";
import { Request } from "./request";
import { PassThrough } from "stream";
export default class Listener {
private udp: dgram.Socket
private tcp: net.Server
constructor(type: "udp" | "tcp", onRequest: (request: Request) => any, port: number, host: string = "0.0.0.0") {
constructor(type: "udp" | "tcp", onRequest: (request: Request) => any, host: string = "0.0.0.0") {
switch (type) {
case "udp":
this.udp = dgram.createSocket("udp4")
this.udp.on("listening", () => {
console.log(`UDP Server Listening on ${port}`)
console.log(`UDP Server Listening on 53`)
})
this.udp.on("message", (message, remote) => {
@ -21,7 +22,7 @@ export default class Listener {
onRequest(request);
})
this.udp.bind(port, host)
this.udp.bind(53, host)
break;
case "tcp":
console.log("Not correct implemented")
@ -53,7 +54,8 @@ export default class Listener {
}
});
});
this.tcp.listen(port, host);
this.tcp.listen(53, host);
console.log(`TCP Server Listening on 53`)
break;
default:
throw new Error("Unknown socket type")

View File

@ -1,121 +1,7 @@
import { Parser } from "binary-parser"
import { Message, MessageHeader, MessageQuestion, MessageRecourceRecord, ErrorCodes } from "./types"
import { IMessage, IMessageHeader, IMessageQuestion, MessageRecourceRecord, ErrorCodes } from "./types"
const MAX_LABEL_SIZE = 63;
export enum QueryTypes {
/**
* IPv4 address
*/
A = 0x01,
/**
* Nameserver
*/
NS = 0x02, // nameserver
/**
* Obsolete
*/
MD = 0x03, // obsolete
/**
* Obsolete
*/
MF = 0x04, // obsolete
/**
* Alias
*/
CNAME = 0x05, // alias
/**
* Start of authority
*/
SOA = 0x06, // start of authority
/**
* Experimental
*/
MB = 0x07, // experimental
/**
* Experimental
*/
MG = 0x08, // experimental
/**
* Experimental
*/
MR = 0x09, // experimental
/**
* Experimental
*/
NULL = 0x0A, // experimental null RR
/**
* Service description
*/
WKS = 0x0B, // service description
/**
* Reverse entry (inaddr.arpa)
*/
PTR = 0x0C, // reverse entry (inaddr.arpa)
/**
* Host information
*/
HINFO = 0x0D, // host information
/**
* Mailbox / Mail-list information
*/
MINFO = 0x0E, // mailbox or mail list information
/**
* Mail exchange
*/
MX = 0x0F, // mail exchange
/**
* Text strings
*/
TXT = 0x10, // text strings
/**
* IPv6 address
*/
AAAA = 0x1C, // ipv6 address
/**
* SRV records
*/
SRV = 0x21, // srv records
/**
* Request to transfer entire zone
*/
AXFR = 0xFC, // request to transfer entire zone
/**
* Request for mailbox related records
*/
MAILA = 0xFE, // request for mailbox related records
/**
* Request for mail agend RRs
*/
MAILB = 0xFD, // request for mail agent RRs
/**
* Any class
*/
ANY = 0xFF, // any class
}
const headerParser = new Parser()
const headerParser: Parser = new Parser()
.endianess("big")
.uint16("ID")
.bit1("QR")
@ -133,6 +19,14 @@ const headerParser = new Parser()
.uint16("NSCOUNT")
.uint16("ARCOUNT")
export function parseHeader(data: Buffer): IMessageHeader {
try {
return <any>headerParser.parse(data);
} catch (e) {
throw new Error("Header parsing failed" + e.message)
}
}
const labelParser = new Parser()
.endianess("big")
.uint8("dataLength")
@ -156,16 +50,22 @@ const questionParser = new Parser()
.uint16("QTYPE")
.uint16("QCLASS")
function parseQuestions(count: number, packet: Buffer): MessageQuestion[] {
return <any>new Parser()
.endianess("big")
.array("questions", {
type: questionParser,
length: count
}).parse(packet).questions;
export function parseQuestions(count: number, packet: Buffer): IMessageQuestion[] {
try {
return <any>new Parser()
.endianess("big")
.array("questions", {
type: questionParser,
length: count
}).parse(packet).questions;
} catch (e) {
throw new Error("Question parsing failed" + e.message)
}
}
export function SerializeName(name: string) {
const MAX_LABEL_SIZE = 63;
export function serializeName(name: string) {
let length = 0;
let parts = name.split(".");
parts.forEach(e => {
@ -187,33 +87,32 @@ export function SerializeName(name: string) {
return data;
}
export class Request implements Message {
private _header: MessageHeader;
export class Request implements IMessage {
private _header: Header;
get header() {
return Object.assign({}, this._header);
}
private _questions: MessageQuestion[];
private _questions: Question[];
get questions() {
return this._questions.map(e => e);
return this._questions.map(e => Object.assign({}, e));
}
answers: RecourceRecord[] = [];
authorities: RecourceRecord[] = [];
additionals: RecourceRecord[] = [];
constructor(packet: Buffer, private sendCallback: (packet: Buffer) => any) {
constructor(packet: Buffer, private sendCallback: (packet: Buffer) => any, private max_size = 512) {
let headerData = Buffer.alloc(12);
packet.copy(headerData, 0, 0, 12);
let bodyData = Buffer.alloc(packet.length - 12);
packet.copy(bodyData, 0, 12, packet.length);
this._header = <any>headerParser.parse(headerData);
this._header = new Header(parseHeader(headerData));
this._header.AD = 0;
this._header.RCODE = ErrorCodes.NoError;
this._header.RA = this._header.RD;
this._questions = parseQuestions(this._header.QDCOUNT, bodyData);
this._questions = parseQuestions(this._header.QDCOUNT, bodyData).map(e => new Question(e));
}
error(error: ErrorCodes) {
@ -229,50 +128,53 @@ export class Request implements Message {
this.sendCallback(this.serialize());
}
serialize(truncate: boolean = false, rcode: 0 | 1 | 2 | 3 | 4 | 5 = 0) {
serialize() {
this._header.AA = 1;
this._header.ANCOUNT = this.answers.length;
this._header.ARCOUNT = this.additionals.length;
this._header.NSCOUNT = this.authorities.length;
this._header.QR = 1;
this._header.RCODE = rcode;
let questions = this._questions.map(e => e.serialize())
let answers = this.answers.map(e => e.serialize())
let authority = this.authorities.map(e => e.serialize())
let additional = this.additionals.map(e => e.serialize())
let questions = this.questions.map(this.serializeQuestion, this)
let answers = this.answers.map(this.serializeResourceRecord, this)
let authority = this.authorities.map(this.serializeResourceRecord, this)
let additional = this.additionals.map(this.serializeResourceRecord, this)
let length = 12;
questions.forEach(e => length += e.length);
answers.forEach(e => length += e.length)
authority.forEach(e => length += e.length)
additional.forEach(e => length += e.length)
// let questionsByteLength = 0;
// questions.forEach(e => questionsByteLength += e.length);
let questionsByteLength = 0;
questions.forEach(e => questionsByteLength += e.length);
// let answersByteLength = 0;
// answers.forEach(e => answersByteLength += e.length)
let answersByteLength = 0;
answers.forEach(e => answersByteLength += e.length)
// let authorityByteLength = 0;
// authority.forEach(e => authorityByteLength += e.length)
let authorityByteLength = 0;
authority.forEach(e => authorityByteLength += e.length)
// let additionalByteLength = 0;
// additional.forEach(e => additionalByteLength += e.length)
let additionalByteLength = 0;
additional.forEach(e => additionalByteLength += e.length)
// let length = 12 + questionsByteLength + answersByteLength + authorityByteLength + additionalByteLength; //Header is always 12 byte large
let length = 12 + questionsByteLength + answersByteLength + authorityByteLength + additionalByteLength; //Header is always 12 byte large
if (truncate && length > 512) {
if (length > this.max_size) {
this._header.TC = 1;
//Buffer will ignore data that exeeds the max buffer length
length = 512;
//Will ignore data, that exceeds length
length = this.max_size;
}
let header = this.serializeHeader()
let header = this._header.serialize();
let data = Buffer.alloc(length)
let offset = 0;
let append = (buffer: Buffer) => {
buffer.copy(data, offset, 0, buffer.length)
offset += buffer.length;
if (offset <= length) {
buffer.copy(data, offset, 0, buffer.length)
offset += buffer.length;
}
}
append(header)
questions.forEach(append)
answers.forEach(append)
@ -280,57 +182,73 @@ export class Request implements Message {
additional.forEach(append)
return data;
}
}
private serializeHeader() {
let header = this.header;
let data = Buffer.alloc(12);
data.writeUInt16BE(header.ID, 0);
var f = 0x0000;
f = f | (header.QR << 15);
f = f | (header.OPCODE << 11);
f = f | (header.AA << 10);
f = f | (header.TC << 9);
f = f | (header.RD << 8);
f = f | (header.RA << 7);
f = f | (header.Z << 6);
f = f | (header.AD << 5);
f = f | (header.CD << 4);
f = f | header.RCODE;
data.writeUInt16BE(f, 2);
export class Header implements IMessageHeader {
ID: number;
QR: 0 | 1;
OPCODE: number
AA: 0 | 1;
TC: 0 | 1;
RD: 0 | 1;
RA: 0 | 1;
Z: 0 | 1;
AD: 0 | 1;
CD: 0 | 1;
RCODE: ErrorCodes;
QDCOUNT: number;
ANCOUNT: number;
NSCOUNT: number;
ARCOUNT: number;
data.writeUInt16BE(header.QDCOUNT, 4)
data.writeUInt16BE(header.ANCOUNT, 6)
data.writeUInt16BE(header.NSCOUNT, 8)
data.writeUInt16BE(header.ARCOUNT, 10)
return data;
constructor(header: IMessageHeader) {
for (let k in header) {
this[k] = header[k];
}
}
private serializeQuestion(question: MessageQuestion) {
let qname = SerializeName(question.QNAME);
serialize() {
let data = Buffer.alloc(12);
data.writeUInt16BE(this.ID, 0);
var f = 0x0000;
f = f | (this.QR << 15);
f = f | (this.OPCODE << 11);
f = f | (this.AA << 10);
f = f | (this.TC << 9);
f = f | (this.RD << 8);
f = f | (this.RA << 7);
f = f | (this.Z << 6);
f = f | (this.AD << 5);
f = f | (this.CD << 4);
f = f | this.RCODE;
data.writeUInt16BE(f, 2);
data.writeUInt16BE(this.QDCOUNT, 4)
data.writeUInt16BE(this.ANCOUNT, 6)
data.writeUInt16BE(this.NSCOUNT, 8)
data.writeUInt16BE(this.ARCOUNT, 10)
return data;
}
}
export class Question implements IMessageQuestion {
QNAME: string;
QTYPE: number;
QCLASS: number;
constructor(question: IMessageQuestion) {
for (let k in question) {
this[k] = question[k]
}
}
serialize() {
let qname = serializeName(this.QNAME);
let data = Buffer.alloc(qname.length + 4);
qname.copy(data, 0, 0, qname.length);
let offset = qname.length;
data.writeUInt16BE(question.QTYPE, offset);
data.writeUInt16BE(this.QTYPE, offset);
offset += 2;
data.writeUInt16BE(question.QCLASS, offset);
return data;
}
private serializeResourceRecord(record: MessageRecourceRecord) {
// TODO: Implement compression
let name = SerializeName(record.NAME);
let data = Buffer.alloc(name.length + 10 + record.RDLENGTH) // For TYPE, CLASS, TTL, RLENGTH
name.copy(data, 0, 0, name.length);
let offset = name.length;
data.writeUInt16BE(record.TYPE, offset)
offset += 2
data.writeUInt16BE(record.CLASS, offset)
offset += 2
data.writeUInt32BE(record.TTL, offset)
offset += 4
data.writeUInt16BE(record.RDLENGTH, offset)
offset += 2
record.RDATA.copy(data, offset, 0, record.RDLENGTH)
data.writeUInt16BE(this.QCLASS, offset);
return data;
}
}
@ -365,6 +283,7 @@ export class RecourceRecord implements MessageRecourceRecord {
private _TTL: number;
set TTL(value) {
if (value < 0 || value > 4294967295) throw new TypeError("TTL Range: 0 - 4.294.967.295")
this._TTL = value;
}
get TTL() {
@ -376,4 +295,22 @@ export class RecourceRecord implements MessageRecourceRecord {
get RDLENGTH() {
return this.RDATA.length;
}
public serialize() {
// TODO: Implement compression
let name = serializeName(this.NAME);
let data = Buffer.alloc(name.length + 10 + this.RDLENGTH) // For TYPE, CLASS, TTL, RLENGTH
name.copy(data, 0, 0, name.length);
let offset = name.length;
data.writeUInt16BE(this.TYPE, offset)
offset += 2
data.writeUInt16BE(this.CLASS, offset)
offset += 2
data.writeUInt32BE(this._TTL, offset)
offset += 4
data.writeUInt16BE(this.RDLENGTH, offset)
offset += 2
this.RDATA.copy(data, offset, 0, this.RDLENGTH)
return data;
}
}

View File

@ -1,5 +1,359 @@
// const t = `71 15 01 00 00 01 00 00 00 00 00 00 02 64 63 08 73 65 72 76 69 63 65 73 0c 76 69 73 75 61 6c 73 74 75 64 69 6f 03 63 6f 6d 00 00 01 00 01`.split(" ").join("");
// // const tp = Buffer.from(t, "hex")
// // // import Parse from "./index";
// // import * as util from "util";
// // console.log(util.inspect(Parse(tp), false, 20, true));
import { assert, expect } from "chai";
import { parseHeader, Header, parseQuestions, Question, RecourceRecord, Request } from "./request"
import { IMessageHeader, IMessageQuestion, QueryTypes } from "./types";
function fromHex(data: string) {
return Buffer.from(data.replace(/\s/g, ""), "hex");
}
describe("parser", function () {
describe("header", function () {
describe("header parser", function () {
let should_templ: IMessageHeader = {
ID: 0,
QR: 0,
OPCODE: 0,
AA: 0,
TC: 0,
RD: 0,
RA: 0,
Z: 0,
AD: 0,
CD: 0,
RCODE: 0,
QDCOUNT: 0,
ANCOUNT: 0,
ARCOUNT: 0,
NSCOUNT: 0
}
let tests: { name: string, data: string, fields: Partial<IMessageHeader> }[] = [
{
name: "Testing ID field",
data: "0001 0000 0000 0000 0000 0000",
fields: {
ID: 1
}
},
{
name: "Testing ID field with max value",
data: "FFFF 0000 0000 0000 0000 0000",
fields: {
ID: 65535
}
},
{
name: "Testing QR field",
data: "0000 8000 0000 0000 0000 0000",
fields: {
QR: 1
}
},
{
name: "Testing OPCODE field value 2",
data: "0000 1000 0000 0000 0000 0000",
fields: {
OPCODE: 2
}
},
{
name: "Testing OPCODE field value 1",
data: "0000 0800 0000 0000 0000 0000",
fields: {
OPCODE: 1
}
},
{
name: "Testing AA field",
data: "0000 0400 0000 0000 0000 0000",
fields: {
AA: 1
}
},
{
name: "Testing TC field",
data: "0000 0200 0000 0000 0000 0000",
fields: {
TC: 1
}
},
{
name: "Testing RD field",
data: "0000 0100 0000 0000 0000 0000",
fields: {
RD: 1
}
},
{
name: "Testing RCODE field",
data: "0000 0002 0000 0000 0000 0000",
fields: {
RCODE: 2
}
},
{
name: "Testing QDCOUNT field max value",
data: "0000 0000 FFFF 0000 0000 0000",
fields: {
QDCOUNT: 65535
}
},
{
name: "Testing ANCOUNT field max value",
data: "0000 0000 0000 FFFF 0000 0000",
fields: {
ANCOUNT: 65535
}
},
{
name: "Testing NSCOUNT field max value",
data: "0000 0000 0000 0000 FFFF 0000",
fields: {
NSCOUNT: 65535
}
},
{
name: "Testing ARCOUNT field max value",
data: "0000 0000 0000 0000 0000 FFFF",
fields: {
ARCOUNT: 65535
}
},
{
name: "Testing all Flags and Values max",
data: "FFFF FFFF FFFF FFFF FFFF FFFF",
fields: {
ID: 65535,
QR: 1,
OPCODE: 15,
AA: 1,
TC: 1,
RD: 1,
RA: 1,
Z: 1,
AD: 1,
CD: 1,
RCODE: 15,
QDCOUNT: 65535,
ANCOUNT: 65535,
NSCOUNT: 65535,
ARCOUNT: 65535
}
}
]
tests.forEach(function (e) {
it(e.name, function () {
let testdata = fromHex(e.data)
let should: IMessageHeader = Object.assign({}, should_templ, e.fields) // Build in "clone" function
let header = parseHeader(testdata);
assert.hasAllKeys(header, Object.keys(should), "Parsed header is missing some fields")
assert.deepEqual(header, should, "Parsed header has not expected values!")
})
})
})
describe("header serializer", function () {
let empty_header: IMessageHeader = {
ID: 0,
QR: 0,
OPCODE: 0,
AA: 0,
TC: 0,
RD: 0,
RA: 0,
Z: 0,
AD: 0,
CD: 0,
RCODE: 0,
QDCOUNT: 0,
ANCOUNT: 0,
ARCOUNT: 0,
NSCOUNT: 0
}
let tests: { name: string, result: string, values: Partial<IMessageHeader> }[] = [
{
name: "Fill header with 0s",
result: "0000 0000 0000 0000 0000 0000",
values: {}
},
{
name: "Set header id to 5",
result: "0005 0000 0000 0000 0000 0000",
values: {
ID: 5
}
},
{
name: "Set QR",
result: "0000 8000 0000 0000 0000 0000",
values: {
QR: 1
}
},
{
name: "Set OPCODE",
result: "0000 7800 0000 0000 0000 0000",
values: {
OPCODE: 15
}
},
{
name: "Set AA",
result: "0000 0400 0000 0000 0000 0000",
values: {
AA: 1
}
},
{
name: "Set TC",
result: "0000 0200 0000 0000 0000 0000",
values: {
TC: 1
}
},
{
name: "Set RD",
result: "0000 0100 0000 0000 0000 0000",
values: {
RD: 1
}
},
{
name: "Set RA",
result: "0000 0080 0000 0000 0000 0000",
values: {
RA: 1
}
},
{
name: "Set Z",
result: "0000 0040 0000 0000 0000 0000",
values: {
Z: 1
}
},
{
name: "Set AD",
result: "0000 0020 0000 0000 0000 0000",
values: {
AD: 1
}
},
{
name: "Set CD",
result: "0000 0010 0000 0000 0000 0000",
values: {
CD: 1
}
},
{
name: "Set RCODE",
result: "0000 000F 0000 0000 0000 0000",
values: {
RCODE: 15
}
},
{
name: "Set QDCOUNT",
result: "0000 0000 FFFF 0000 0000 0000",
values: {
QDCOUNT: 65535
}
},
{
name: "Set ANCOUNT",
result: "0000 0000 0000 FFFF 0000 0000",
values: {
ANCOUNT: 65535
}
},
{
name: "Set NSCOUNT",
result: "0000 0000 0000 0000 FFFF 0000",
values: {
NSCOUNT: 65535
}
},
{
name: "Set ARCOUNT",
result: "0000 0000 0000 0000 0000 FFFF",
values: {
ARCOUNT: 65535
}
},
]
tests.forEach(function (e) {
it(e.name, function () {
let header = Object.assign({}, empty_header, e.values)
let serialized = new Header(header).serialize();
assert.equal(serialized.toString("hex"), e.result.replace(/\s/g, "").toLowerCase(), "Header serialization failed");
})
})
})
})
describe("question", function () {
let questionData = fromHex("0474 6573 7407 6578 616d 706c 6503 636f 6d00 0001 0001")
let questionObj = {
QNAME: "test.example.com",
QTYPE: QueryTypes.A,
QCLASS: 1
}
it("check question parser with one question", function () {
let res = parseQuestions(1, questionData)
let should: IMessageQuestion[] = [questionObj]
assert.deepEqual(res, should, "Question parser does not parse input correctly")
})
it("check question serialization", function () {
let res = new Question(questionObj).serialize()
assert.equal(res.toString("hex"), questionData.toString("hex"), "Query serializer does not serialite correctly");
})
})
it("recource record serialization", function () {
let should = "07 6578616D706C65 03 636f6D 00 0001 0001 00000640 0004 0A000001"
let rr = new RecourceRecord()
rr.CLASS = 1
rr.NAME = "example.com"
rr.TTL = 1600
rr.TYPE = 1
rr.RDATA = fromHex("0A 00 00 01")
let res = rr.serialize().toString("hex")
assert.equal(res, should.replace(/\s/g, "").toLowerCase(), "Serialization not working properly")
})
it("full response serialization", function () {
let reqData = fromHex("E835 0100 0001 0000 0000 0000 07 6578616D706c65 03636F6D 00 0001 0001");
let should = "E835 8580 0001000100000000 076578616D706C6503636F6D0000010001 07 6578616D706C65 03 636F6D 00 0001 0001 0000 0640 0004 0A000001"
let request = new Request(reqData, () => null)
let rr = new RecourceRecord()
rr.CLASS = 1
rr.NAME = "example.com"
rr.TTL = 1600
rr.TYPE = 1
rr.RDATA = fromHex("0A 00 00 01")
request.answers.push(rr)
let data = request.serialize()
assert.equal(data.toString("hex"), should.replace(/\s/g, "").toLowerCase(), "Whole packet serialization failed")
})
// it("full response serialization benchmark", function () {
// let reqData = fromHex("E835 0100 0001 0000 0000 0000 07 6578616D706c65 03636F6D 00 0001 0001");
// for (let i = 0; i < 100; i++) {
// let request = new Request(reqData, () => null)
// let rr = new RecourceRecord()
// rr.CLASS = 1
// rr.NAME = "example.com"
// rr.TTL = 1600
// rr.TYPE = 1
// rr.RDATA = fromHex("0A 00 00 01")
// request.answers.push(rr)
// request.serialize()
// }
// })
})

View File

@ -1,12 +1,3 @@
// 0 No error condition
// * 1 Format error - unable to interpret query
// * 2 Server failure - internal problem
// * 3 Name error - Only for authorative name server, domain name of query does not exist
// * 4 Not implemented - Request not supported
// * 5 Refused - Nameserver refuses request
// * 6 - 15 Reserved for future usage
// *
export enum ErrorCodes {
/**
* No error
@ -39,7 +30,119 @@ export enum ErrorCodes {
Refused
}
export interface MessageHeader {
export enum QueryTypes {
/**
* IPv4 address
*/
A = 0x01,
/**
* Nameserver
*/
NS = 0x02, // nameserver
/**
* Obsolete
*/
MD = 0x03, // obsolete
/**
* Obsolete
*/
MF = 0x04, // obsolete
/**
* Alias
*/
CNAME = 0x05, // alias
/**
* Start of authority
*/
SOA = 0x06, // start of authority
/**
* Experimental
*/
MB = 0x07, // experimental
/**
* Experimental
*/
MG = 0x08, // experimental
/**
* Experimental
*/
MR = 0x09, // experimental
/**
* Experimental
*/
NULL = 0x0A, // experimental null RR
/**
* Service description
*/
WKS = 0x0B, // service description
/**
* Reverse entry (inaddr.arpa)
*/
PTR = 0x0C, // reverse entry (inaddr.arpa)
/**
* Host information
*/
HINFO = 0x0D, // host information
/**
* Mailbox / Mail-list information
*/
MINFO = 0x0E, // mailbox or mail list information
/**
* Mail exchange
*/
MX = 0x0F, // mail exchange
/**
* Text strings
*/
TXT = 0x10, // text strings
/**
* IPv6 address
*/
AAAA = 0x1C, // ipv6 address
/**
* SRV records
*/
SRV = 0x21, // srv records
/**
* Request to transfer entire zone
*/
AXFR = 0xFC, // request to transfer entire zone
/**
* Request for mailbox related records
*/
MAILA = 0xFE, // request for mailbox related records
/**
* Request for mail agend RRs
*/
MAILB = 0xFD, // request for mail agent RRs
/**
* Any class
*/
ANY = 0xFF, // any class
}
export interface IMessageHeader {
/**
* A 16 bit identifier assigned by the program that
* generates any kind of query. This identifier is copied
@ -93,7 +196,7 @@ export interface MessageHeader {
/**
* Reserved for future usage, must be 0 in all queries
*/
Z: 0;
Z: 0 | 1;
AD: 0 | 1;
@ -136,7 +239,7 @@ export interface MessageHeader {
*/
ARCOUNT: number;
}
export interface MessageQuestion {
export interface IMessageQuestion {
/**
* Domain name represented as sequence of labels
* Each label consists of a length octed followed
@ -203,9 +306,9 @@ export interface MessageRecourceRecord {
*/
RDATA: Buffer;
}
export interface Message {
header: MessageHeader;
questions: MessageQuestion[];
export interface IMessage {
header: IMessageHeader;
questions: IMessageQuestion[];
answers: MessageRecourceRecord[];
authorities: MessageRecourceRecord[];
additionals: MessageRecourceRecord[];