Advertisement
Guest User

DNS A record filter proxy

a guest
Aug 25th, 2017
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. let dns = require('native-dns');
  4. let server = dns.createUDPServer({ dgram_type: 'udp6' });
  5.  
  6. server.on('listening', () => console.log('server listening on', server.address()));
  7. server.on('close', () => console.log('server closed', server.address()));
  8. server.on('error', (err, buff, req, res) => console.error(err.stack));
  9. server.on('socketError', (err, socket) => console.error(err));
  10.  
  11. server.serve(53, 'localhost');
  12.  
  13. let authority = { address: '2001:4860:4860::8888', port: 53, type: 'udp' };
  14.  
  15. function proxy(question, response, cb) {
  16.  
  17.     var request = dns.Request({
  18.         question: question, // forwarding the question
  19.         server: authority,  // this is the DNS server we are asking
  20.         timeout: 1000
  21.     });
  22.  
  23.     // when we get answers, append them to the response
  24.     request.on('message', (err, msg) => {
  25.         msg.answer.forEach(a => {
  26.             if (a.type != 1) {
  27.                 response.answer.push(a);
  28.             }
  29.         });
  30.     });
  31.  
  32.     request.on('end', cb);
  33.     request.send();
  34. }
  35.  
  36. let async = require('async');
  37.  
  38. function handleRequest(request, response) {
  39.  
  40.     let f = []; // array of functions
  41.  
  42.     // proxy all questions
  43.     // since proxying is asynchronous, store all callbacks
  44.     request.question.forEach(question => {
  45.         f.push(cb => proxy(question, response, cb));
  46.     });
  47.  
  48.     // do the proxying in parallel
  49.     // when done, respond to the request by sending the response
  50.     async.parallel(f, function() { response.send(); });
  51. }
  52.  
  53. server.on('request', handleRequest);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement