adding experimental udp6 support
All checks were successful
the build was successful

This commit is contained in:
Fabian Stamm 2018-05-17 13:54:18 +02:00
parent 25131a2d4b
commit 43f4cf358d
4 changed files with 13 additions and 11 deletions

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, host?: string);
constructor(type: "udp4" | "udp6" | "tcp", onRequest: (request: Request) => any, host?: string);
close(): void;
}

View File

@ -6,8 +6,9 @@ const request_1 = require("./request");
class Listener {
constructor(type, onRequest, host = "0.0.0.0") {
switch (type) {
case "udp":
this.udp = dgram.createSocket("udp4");
case "udp6":
case "udp4":
this.udp = dgram.createSocket(type);
this.udp.on("listening", () => {
console.log(`UDP Server Listening on 53`);
});
@ -21,7 +22,7 @@ class Listener {
this.udp.bind(53, host);
break;
case "tcp":
console.log("Not correct implemented");
console.log("Using TCP is experimantal");
this.tcp = net.createServer((socket) => {
let length;
let got = 0;
@ -30,7 +31,7 @@ class Listener {
let offset = 0;
if (!message) {
length = data.readUInt16BE(0);
if (length > 2048)
if (length > 4096)
return socket.destroy(); //Requests with more that 2k are ignored
message = Buffer.alloc(length);
offset = 2;

View File

@ -1 +1 @@
{"version":3,"file":"listener.js","sourceRoot":"","sources":["../src/listener.ts"],"names":[],"mappings":";;AAAA,2BAA0B;AAC1B,+BAA+B;AAC/B,uCAAoC;AAGpC;IAGG,YAAY,IAAmB,EAAE,SAAoC,EAAE,OAAe,SAAS;QAC5F,QAAQ,IAAI,EAAE;YACX,KAAK,KAAK;gBACP,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,CAAA;gBACrC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,WAAW,EAAE,GAAG,EAAE;oBAC3B,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAA;gBAC5C,CAAC,CAAC,CAAA;gBAEF,IAAI,CAAC,GAAG,CAAC,EAAE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@@ import { PassThrough } from "stream";
export default class Listener {
private udp: dgram.Socket
private tcp: net.Server
constructor(type: "udp" | "tcp", onRequest: (request: Request) => any, host: string = "0.0.0.0") {
constructor(type: "udp4" | "udp6" | "tcp", onRequest: (request: Request) => any, host: string = "0.0.0.0") {
switch (type) {
case "udp":
this.udp = dgram.createSocket("udp4")
case "udp6":
case "udp4":
this.udp = dgram.createSocket(type)
this.udp.on("listening", () => {
console.log(`UDP Server Listening on 53`)
})
@ -25,7 +26,7 @@ export default class Listener {
this.udp.bind(53, host)
break;
case "tcp":
console.log("Not correct implemented")
console.log("Using TCP is experimantal")
this.tcp = net.createServer((socket) => {
let length: number;
let got: number = 0;
@ -34,7 +35,7 @@ export default class Listener {
let offset = 0;
if (!message) {
length = data.readUInt16BE(0);
if (length > 2048) return socket.destroy(); //Requests with more that 2k are ignored
if (length > 4096) return socket.destroy(); //Requests with more that 2k are ignored
message = Buffer.alloc(length);
offset = 2;
}