Compare commits
No commits in common. "23c27c3c4f017745df177af063981b9f07709fef" and "9d3d9f8a80ddd4fbaebf02b1c7a0848c68d73d69" have entirely different histories.
23c27c3c4f
...
9d3d9f8a80
7
lib/listener.d.ts
vendored
Normal file
7
lib/listener.d.ts
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { Request } from "./request";
|
||||||
|
export default class Listener {
|
||||||
|
private udp;
|
||||||
|
private tcp;
|
||||||
|
constructor(type: "udp4" | "udp6" | "tcp", onRequest: (request: Request) => any, host?: string);
|
||||||
|
close(): void;
|
||||||
|
}
|
71
lib/listener.js
Normal file
71
lib/listener.js
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
const net = require("net");
|
||||||
|
const dgram = require("dgram");
|
||||||
|
const request_1 = require("./request");
|
||||||
|
class Listener {
|
||||||
|
constructor(type, onRequest, host = "0.0.0.0") {
|
||||||
|
switch (type) {
|
||||||
|
case "udp6":
|
||||||
|
case "udp4":
|
||||||
|
this.udp = dgram.createSocket(type);
|
||||||
|
this.udp.on("listening", () => {
|
||||||
|
console.log(`UDP Server Listening on 53`);
|
||||||
|
});
|
||||||
|
this.udp.on("message", (message, remote) => {
|
||||||
|
let request = new request_1.Request(message, (data) => {
|
||||||
|
// console.log("sending:", new Request(data, (a) => 0));
|
||||||
|
this.udp.send(data, remote.port, remote.address);
|
||||||
|
});
|
||||||
|
onRequest(request);
|
||||||
|
});
|
||||||
|
this.udp.bind(53, host);
|
||||||
|
break;
|
||||||
|
case "tcp":
|
||||||
|
console.log("Using TCP is experimantal");
|
||||||
|
this.tcp = net.createServer((socket) => {
|
||||||
|
let length;
|
||||||
|
let got = 0;
|
||||||
|
let message = undefined;
|
||||||
|
socket.on("data", (data) => {
|
||||||
|
let offset = 0;
|
||||||
|
if (!message) {
|
||||||
|
length = data.readUInt16BE(0);
|
||||||
|
if (length > 4096)
|
||||||
|
return socket.destroy(); //Requests with more that 2k are ignored
|
||||||
|
message = Buffer.alloc(length);
|
||||||
|
offset = 2;
|
||||||
|
}
|
||||||
|
let read = (data.length - offset) > (length - got) ? (length - got) : (data.length - offset);
|
||||||
|
data.copy(message, got, offset, read + offset);
|
||||||
|
got += read;
|
||||||
|
//ToDo don't ignore probably following requests
|
||||||
|
if (got >= length) {
|
||||||
|
let request = new request_1.Request(message, (data) => {
|
||||||
|
socket.write(data);
|
||||||
|
});
|
||||||
|
got = 0;
|
||||||
|
message = undefined;
|
||||||
|
length = 0;
|
||||||
|
onRequest(request);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
this.tcp.listen(53, host);
|
||||||
|
console.log(`TCP Server Listening on 53`);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error("Unknown socket type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close() {
|
||||||
|
if (this.udp) {
|
||||||
|
this.udp.close();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.tcp.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.default = Listener;
|
||||||
|
//# sourceMappingURL=listener.js.map
|
1
lib/listener.js.map
Normal file
1
lib/listener.js.map
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"version":3,"file":"listener.js","sourceRoot":"","sources":["../src/listener.ts"],"names":[],"mappings":";;AAAA,2BAA0B;AAC1B,+BAA+B;AAC/B,uCAAoC;AAGpC;IAGG,YAAY,IAA6B,EAAE,SAAoC,EAAE,OAAe,SAAS;QACtG,QAAQ,IAAI,EAAE;YACX,KAAK,MAAM,CAAC;YACZ,KAAK,MAAM;gBACR,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,CAAA;gBACnC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;oBAC3B,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;gBAC5C,CAAC,CAAC,CAAA;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M,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;wBAC7F,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,GAAG,MAAM,CAAC,CAAA;wBAC9C,GAAG,IAAI,IAAI,CAAC;wBACZ,+CAA+C;wBAC/C,IAAI,GAAG,IAAI,MAAM,EAAE;4BAChB,IAAI,OAAO,GAAG,IAAI,iBAAO,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE;gCACzC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;4BACtB,CAAC,CAAC,CAAA;4BACF,GAAG,GAAG,CAAC,CAAC;4BACR,OAAO,GAAG,SAAS,CAAC;4BACpB,MAAM,GAAG,CAAC,CAAC;4BACX,SAAS,CAAC,OAAO,CAAC,CAAC;yBACrB;oBACJ,CAAC,CAAC,CAAC;gBACN,CAAC,CAAC,CAAC;gBACH,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;gBAC1B,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;gBACzC,MAAM;YACT;gBACG,MAAM,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;AAnED,2BAmEC"}
|
@ -134,6 +134,15 @@ class Request {
|
|||||||
answers.forEach(e => length += e.length);
|
answers.forEach(e => length += e.length);
|
||||||
authority.forEach(e => length += e.length);
|
authority.forEach(e => length += e.length);
|
||||||
additional.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) {
|
if (length > this.max_size) {
|
||||||
this._header.TC = 1;
|
this._header.TC = 1;
|
||||||
//Will ignore data, that exceeds length
|
//Will ignore data, that exceeds length
|
||||||
|
File diff suppressed because one or more lines are too long
73
src/listener.ts
Normal file
73
src/listener.ts
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
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: "udp4" | "udp6" | "tcp", onRequest: (request: Request) => any, host: string = "0.0.0.0") {
|
||||||
|
switch (type) {
|
||||||
|
case "udp6":
|
||||||
|
case "udp4":
|
||||||
|
this.udp = dgram.createSocket(type)
|
||||||
|
this.udp.on("listening", () => {
|
||||||
|
console.log(`UDP Server Listening on 53`)
|
||||||
|
})
|
||||||
|
|
||||||
|
this.udp.on("message", (message, remote) => {
|
||||||
|
let request = new Request(message, (data) => {
|
||||||
|
// console.log("sending:", new Request(data, (a) => 0));
|
||||||
|
this.udp.send(data, remote.port, remote.address)
|
||||||
|
})
|
||||||
|
onRequest(request);
|
||||||
|
})
|
||||||
|
|
||||||
|
this.udp.bind(53, host)
|
||||||
|
break;
|
||||||
|
case "tcp":
|
||||||
|
console.log("Using TCP is experimantal")
|
||||||
|
this.tcp = net.createServer((socket) => {
|
||||||
|
let length: number;
|
||||||
|
let got: number = 0;
|
||||||
|
let message = undefined;
|
||||||
|
socket.on("data", (data) => {
|
||||||
|
let offset = 0;
|
||||||
|
if (!message) {
|
||||||
|
length = data.readUInt16BE(0);
|
||||||
|
if (length > 4096) return socket.destroy(); //Requests with more that 2k are ignored
|
||||||
|
message = Buffer.alloc(length);
|
||||||
|
offset = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
let read = (data.length - offset) > (length - got) ? (length - got) : (data.length - offset);
|
||||||
|
data.copy(message, got, offset, read + offset)
|
||||||
|
got += read;
|
||||||
|
//ToDo don't ignore probably following requests
|
||||||
|
if (got >= length) {
|
||||||
|
let request = new Request(message, (data) => {
|
||||||
|
socket.write(data);
|
||||||
|
})
|
||||||
|
got = 0;
|
||||||
|
message = undefined;
|
||||||
|
length = 0;
|
||||||
|
onRequest(request);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
this.tcp.listen(53, host);
|
||||||
|
console.log(`TCP Server Listening on 53`)
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error("Unknown socket type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
close() {
|
||||||
|
if (this.udp) {
|
||||||
|
this.udp.close();
|
||||||
|
} else {
|
||||||
|
this.tcp.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -144,6 +144,19 @@ export class Request implements IMessage {
|
|||||||
answers.forEach(e => length += e.length)
|
answers.forEach(e => length += e.length)
|
||||||
authority.forEach(e => length += e.length)
|
authority.forEach(e => length += e.length)
|
||||||
additional.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) {
|
if (length > this.max_size) {
|
||||||
this._header.TC = 1;
|
this._header.TC = 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user