diff --git a/lib/listener.d.ts b/lib/listener.d.ts deleted file mode 100644 index 2ba4a0c..0000000 --- a/lib/listener.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -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; -} diff --git a/lib/listener.js b/lib/listener.js deleted file mode 100644 index cfb3c56..0000000 --- a/lib/listener.js +++ /dev/null @@ -1,71 +0,0 @@ -"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 \ No newline at end of file diff --git a/lib/listener.js.map b/lib/listener.js.map deleted file mode 100644 index 9827db2..0000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BACzC,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"} \ No newline at end of file diff --git a/src/listener.ts b/src/listener.ts deleted file mode 100644 index 5c26195..0000000 --- a/src/listener.ts +++ /dev/null @@ -1,73 +0,0 @@ -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(); - } - } -} \ No newline at end of file