Created
July 2, 2013 07:25
-
-
Save madbence/5907360 to your computer and use it in GitHub Desktop.
Simple DNS server in nodejs for testing purposes. Sometimes it works!
This file contains hidden or 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 udp = require('dgram'); | |
var server = udp.createSocket('udp4'); | |
var client = null; | |
server.on('message', function(msg, rinfo) { | |
if(rinfo.address = '127.0.0.1') { | |
client = rinfo; | |
} else { | |
server.send(msg, 0, msg.length, client.port, client.address); | |
} | |
var offset = 12, | |
state = 0, | |
read = 0, | |
remaining = 0, | |
url = ''; | |
transaction1 = msg.readUInt8(0), | |
transaction2 = msg.readUInt8(1), | |
query = []; | |
for(;;offset++) { | |
if(state == 0) { | |
remaining = msg.readUInt8(offset); | |
state = 1; | |
query.push(remaining); | |
if(!remaining) break; | |
if(url != '') { url += '.'; } | |
} else { | |
read = msg.readUInt8(offset); | |
query.push(read); | |
url += String.fromCharCode(read); | |
remaining--; | |
if(!remaining) state = 0; | |
} | |
} | |
console.log(url, rinfo); | |
if(!url.match(/localhost$/)) { | |
return server.send(msg,0,msg.length,53,'8.8.8.8'); | |
} | |
var ans = [transaction1, transaction2, 0x81, 0x80, 0, 1, 0, 1, 0, 0, 0, 0]; | |
ans = ans.concat(query, [0, 1, 0, 1, 0xc0, 0x0c, 0, 1, 0, 1, 0, 0, 1, 0x2c, 0, 4, 127, 0, 0, 1]); | |
ans = new Buffer(ans); | |
server.send(ans,0,ans.length,rinfo.port,rinfo.address,function(error){ | |
console.log('sent', ans); | |
}); | |
}); | |
server.bind(53); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment