Skip to content

Instantly share code, notes, and snippets.

@greggman
Created March 14, 2014 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggman/9553597 to your computer and use it in GitHub Desktop.
Save greggman/9553597 to your computer and use it in GitHub Desktop.
dns server that directs all traffic to the machine its on
# requires https://github.com/tjfontaine/node-dns
var os = require('os')
var dns = require('native-dns');
var server = dns.createServer();
var port = 53;
var interfaces = os.networkInterfaces();
var addresses = [];
for (k in interfaces) {
for (k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family == 'IPv4' && !address.internal) {
addresses.push(address.address)
}
}
}
if (addresses.length < 1) {
console.error("No IP address found");
}
var address = addresses[0];
if (addresses.length > 1) {
console.log("more than 1 IP address found: " + addresses);
console.log("using: " + address);
}
server.on('request', function (request, response) {
response.answer.push(dns.A({
name: request.question[0].name,
address: address,
ttl: 1,
}));
response.send();
});
server.on('error', function (err, buff, req, res) {
console.log(err.stack);
});
try {
server.serve(port);
} catch (e) {
console.error(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment