Last active
December 18, 2016 11:48
-
-
Save vavrusa/acf6841b0e1bcf8daf85 to your computer and use it in GitHub Desktop.
BIND11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var dgram = require('dgram'); | |
function ntohs(arr) { | |
return ((arr[0] & 0xFF) << 8) | (arr[1] & 0xFF); | |
} | |
function parse_name(data, pos) | |
{ | |
for (base = pos; pos < data.length;) { | |
if (data[pos] == 0) { | |
return data.slice(base, pos + 1) | |
} | |
if (data[pos] > 64) { | |
throw "hackz label"; // SAFETY FIRST | |
} | |
pos += data[pos] + 1; | |
} | |
} | |
function parse_query(data, pos) | |
{ | |
qst = {} | |
qst.qname = parse_name(data, pos) | |
pos += qst.qname.length | |
qst.qtype = ntohs(data.slice(pos, pos + 2)) | |
pos += 2 | |
qst.qclass = ntohs(data.slice(pos, pos + 2)) | |
return qst; | |
} | |
function resolve(data, len) | |
{ | |
data[2] = 0x81 + 0x04; | |
data[3] = 0x05; | |
try { | |
// parse header + query | |
qst = parse_query(data, 12) | |
// fuck ANY | |
if (qst.qtype == 0xFF) { | |
data[3] = 0x04; | |
} | |
// TODO: parse /etc/hosts | |
} catch (err) { | |
console.trace(err) | |
data[3] = 0x02; // SERVFAIL | |
} | |
return len; | |
} | |
var tcp = net.createServer(function (socket) { | |
socket.on('data', function (data) { | |
var len = ntohs(data.slice(0, 2)); | |
resolve(data.slice(2), len); | |
socket.write(data); | |
}); | |
}); | |
var udp = dgram.createSocket('udp6'); | |
udp.on('message', function (message, remote) { | |
var len = resolve(message, message.length); | |
udp.send(message, 0, len, remote.port, remote.address); | |
}); | |
tcp.listen(5355, '::'); | |
udp.bind(5355, '::'); |
What about an UDP version, now? :-)
I like the variety of possible rcodes...
I aim to please. UDP version now + "Happy Eyeballs". Performance on par with BIND9 on my localhost.
Wow, most paying software do not implement so quickly what I ask for!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love this! ;-)