Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- let dns = require('native-dns');
- let server = dns.createUDPServer({ dgram_type: 'udp6' });
- function secureExit() {
- try {
- server.close();
- } catch (err) {}
- process.exit(1);
- }
- function exitWithError(err) {
- console.error(err);
- secureExit();
- }
- process.once('SIGINT', () => exitWithError('Received SIGINT'));
- process.once('SIGTERM', () => exitWithError('Received SIGTERM'));
- function beReady(){
- console.log('server listening on', server.address());
- try {
- process.setgid('nogroup');
- process.setuid('nobody');
- } catch (err) {
- exitWithError('can\'t drop privileges');
- }
- }
- server.on('listening', beReady);
- server.on('close', () => console.log('server closed', server.address()));
- server.on('error', (err, buff, req, res) => exitWithError(err.stack));
- server.on('socketError', (err, socket) => exitWithError(err));
- server.serve(53, 'localhost');
- let authority = { address: '2001:4860:4860::8888', port: 53, type: 'udp' };
- function proxy(question, response, cb) {
- var request = dns.Request({
- question: question, // forwarding the question
- server: authority, // this is the DNS server we are asking
- timeout: 1000
- });
- // when we get answers, append them to the response
- request.on('message', (err, msg) => {
- msg.answer.forEach(a => {
- if (a.type != 1) {
- response.answer.push(a);
- }
- });
- });
- request.on('end', cb);
- request.send();
- }
- let async = require('async');
- function handleRequest(request, response) {
- let f = []; // array of functions
- // proxy all questions
- // since proxying is asynchronous, store all callbacks
- request.question.forEach(question => {
- f.push(cb => proxy(question, response, cb));
- });
- // do the proxying in parallel
- // when done, respond to the request by sending the response
- async.parallel(f, function() { response.send(); });
- }
- server.on('request', handleRequest);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement