Skip to content

Instantly share code, notes, and snippets.

@vikrum
Created January 14, 2013 18:56
Show Gist options
  • Save vikrum/4532349 to your computer and use it in GitHub Desktop.
Save vikrum/4532349 to your computer and use it in GitHub Desktop.
A custom DNS server in NodeJS that saves off queries to Firebase so they can be retrieved later. Accompanying blog post: http://5f5.org/ruminations/dns-debugging-over-http.html
var crypto = require('crypto');
var dns = require('native-dns');
var rest = require('restler');
var server = dns.createServer();
server.on('request', function (request, response) {
var domain = request.question[0].name;
if(domain == 'webutils.flourishworks.com') {
// Don't log this because it can't be uniquely identified and subsequently retrieved
response.answer.push(dns.CNAME( { name: domain, data: 'ghs.googlehosted.com.', ttl: 30 }));
response.send();
}
else {
var sha256 = crypto.createHash('sha256').update(domain).digest("hex");
rest.postJson('http://firedns.firebaseio.com/'+sha256+'/.json',
{ poweredBy: 'Firebase',
timestamp: new Date().getTime(),
header: request.header,
question: request.question,
address: request.address}
).on('complete', function() {
response.answer.push(dns.CNAME( { name: domain, data: 'ghs.googlehosted.com.', ttl: 30 }));
response.send();
});
}
});
server.on('error', function (err, buff, req, res) { console.log(err.stack); });
server.serve(53);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment