Last active
January 4, 2018 13:13
-
-
Save Dviejopomata/88fbc2aa5d76ac45377d22b45be4cf31 to your computer and use it in GitHub Desktop.
Dns server
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 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