Skip to content

Instantly share code, notes, and snippets.

@Dviejopomata Dviejopomata/server.js
Last active Jan 4, 2018

Embed
What would you like to do?
Dns server
var dns = require('native-dns');
var server = dns.createServer();
const secondaryDns = { address: "8.8.8.8", port: 53, type: 'udp' };
const hosts = [
{ name: "host1.nextagilesoftdev.com", address: "192.168.1.41", ttl: 6000 },
{ name: "host3.nextagilesoftdev.com", address: "192.168.1.8", ttl: 6000 },
{ name: "host4.nextagilesoftdev.com", address: "192.168.1.8", ttl: 6000 },
]
server.serve(53);
/**
* Search Forward to other dns
* @param {any} question Question to be
* @param {Function} callback Callback function
* @returns {any} Nothing
*/
const searchForward = function (question, callback) {
const nativeQuestion1 = dns.Question(question);
const nativeReq = dns.Request({
question: nativeQuestion1,
server: secondaryDns
});
nativeReq.on('message', (err, answer) => {
if (err) {
return callback(err, null)
}
return callback(null, answer.answer)
});
nativeReq.send();
}
server.on('request', function (request, response) {
const question = request.question[0]
const name = question.name;
const r = hosts.find(a => a.name === name)
if (r) {
response.answer.push(dns.A(r))
response.send();
} else {
searchForward(question, (err, answers) => {
if (err) {
return;
}
answers.forEach(answer => response.answer.push(answer))
response.send();
});
}
});
server.on('error', function (err, buff, req, res) {
console.log(err.stack);
throw err
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.