DNSServer.js.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>DNSServer.js - Documentation</title>
  6. <script src="scripts/prettify/prettify.js"></script>
  7. <script src="scripts/prettify/lang-css.js"></script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. </head>
  15. <body>
  16. <input type="checkbox" id="nav-trigger" class="nav-trigger" />
  17. <label for="nav-trigger" class="navicon-button x">
  18. <div class="navicon"></div>
  19. </label>
  20. <label for="nav-trigger" class="overlay"></label>
  21. <nav>
  22. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ControlServer.html">ControlServer</a><ul class='methods'><li data-type='method'><a href="ControlServer.html#.instance_info">instance_info</a></li><li data-type='method'><a href="ControlServer.html#close">close</a></li><li data-type='method'><a href="ControlServer.html#createDNSServer">createDNSServer</a></li><li data-type='method'><a href="ControlServer.html#createHTTPServer">createHTTPServer</a></li><li data-type='method'><a href="ControlServer.html#createSOCKSServer">createSOCKSServer</a></li><li data-type='method'><a href="ControlServer.html#createTorPool">createTorPool</a></li><li data-type='method'><a href="ControlServer.html#listen">listen</a></li><li data-type='method'><a href="ControlServer.html#listenTcp">listenTcp</a></li><li data-type='method'><a href="ControlServer.html#listenWs">listenWs</a></li></ul></li><li><a href="DNSServer.html">DNSServer</a><ul class='methods'><li data-type='method'><a href="DNSServer.html#listen">listen</a></li></ul></li><li><a href="HTTPServer.html">HTTPServer</a><ul class='methods'><li data-type='method'><a href="HTTPServer.html#listen">listen</a></li></ul></li><li><a href="SOCKSServer.html">SOCKSServer</a><ul class='methods'><li data-type='method'><a href="SOCKSServer.html#listen">listen</a></li></ul></li><li><a href="TorPool.html">TorPool</a><ul class='methods'><li data-type='method'><a href="TorPool.html#add">add</a></li><li data-type='method'><a href="TorPool.html#add_instance_to_group">add_instance_to_group</a></li><li data-type='method'><a href="TorPool.html#add_instance_to_group_at">add_instance_to_group_at</a></li><li data-type='method'><a href="TorPool.html#add_instance_to_group_by_name">add_instance_to_group_by_name</a></li><li data-type='method'><a href="TorPool.html#create">create</a></li><li data-type='method'><a href="TorPool.html#create_instance">create_instance</a></li><li data-type='method'><a href="TorPool.html#exit">exit</a></li><li data-type='method'><a href="TorPool.html#get_config_at">get_config_at</a></li><li data-type='method'><a href="TorPool.html#get_config_by_name">get_config_by_name</a></li><li data-type='method'><a href="TorPool.html#instance_at">instance_at</a></li><li data-type='method'><a href="TorPool.html#instance_by_name">instance_by_name</a></li><li data-type='method'><a href="TorPool.html#instances_by_group">instances_by_group</a></li><li data-type='method'><a href="TorPool.html#new_identites">new_identites</a></li><li data-type='method'><a href="TorPool.html#new_identites_by_group">new_identites_by_group</a></li><li data-type='method'><a href="TorPool.html#new_identity_at">new_identity_at</a></li><li data-type='method'><a href="TorPool.html#new_identity_by_name">new_identity_by_name</a></li><li data-type='method'><a href="TorPool.html#next">next</a></li><li data-type='method'><a href="TorPool.html#next_by_group">next_by_group</a></li><li data-type='method'><a href="TorPool.html#remove">remove</a></li><li data-type='method'><a href="TorPool.html#remove_at">remove_at</a></li><li data-type='method'><a href="TorPool.html#remove_by_name">remove_by_name</a></li><li data-type='method'><a href="TorPool.html#remove_instance_from_group">remove_instance_from_group</a></li><li data-type='method'><a href="TorPool.html#remove_instance_from_group_at">remove_instance_from_group_at</a></li><li data-type='method'><a href="TorPool.html#remove_instance_from_group_by_name">remove_instance_from_group_by_name</a></li><li data-type='method'><a href="TorPool.html#set_config_all">set_config_all</a></li><li data-type='method'><a href="TorPool.html#set_config_at">set_config_at</a></li><li data-type='method'><a href="TorPool.html#set_config_by_group">set_config_by_group</a></li><li data-type='method'><a href="TorPool.html#set_config_by_name">set_config_by_name</a></li><li data-type='method'><a href="TorPool.html#signal_all">signal_all</a></li><li data-type='method'><a href="TorPool.html#signal_at">signal_at</a></li><li data-type='method'><a href="TorPool.html#signal_by_group">signal_by_group</a></li><li data-type='method'><a href="TorPool.html#signal_by_name">signal_by_name</a></li></ul></li><li><a href="TorProcess.html">TorProcess</a><ul class='methods'><li data-type='method'><a href="TorProcess.html#create">create</a></li><li data-type='method'><a href="TorProcess.html#exit">exit</a></li><li data-type='method'><a href="TorProcess.html#get_config">get_config</a></li><li data-type='method'><a href="TorProcess.html#new_identity">new_identity</a></li><li data-type='method'><a href="TorProcess.html#set_config">set_config</a></li><li data-type='method'><a href="TorProcess.html#signal">signal</a></li></ul></li></ul><h3>Modules</h3><ul><li><a href="module-tor-router.html">tor-router</a></li><li><a href="module-tor-router_ControlServer.html">tor-router/ControlServer</a></li><li><a href="module-tor-router_default_config.html">tor-router/default_config</a></li><li><a href="module-tor-router_default_ports.html">tor-router/default_ports</a></li><li><a href="module-tor-router_DNSServer.html">tor-router/DNSServer</a></li><li><a href="module-tor-router_HTTPServer.html">tor-router/HTTPServer</a></li><li><a href="module-tor-router_launch.html">tor-router/launch</a></li><li><a href="module-tor-router_nconf_load_env.html">tor-router/nconf_load_env</a></li><li><a href="module-tor-router_SOCKSServer.html">tor-router/SOCKSServer</a></li><li><a href="module-tor-router_TorPool.html">tor-router/TorPool</a></li><li><a href="module-tor-router_TorProcess.html">tor-router/TorProcess</a></li><li><a href="module-tor-router_winston_silent_logger.html">tor-router/winston_silent_logger</a></li></ul><h3>Events</h3><ul><li><a href="DNSServer.html#event:instance-connection">instance-connection</a></li><li><a href="HTTPServer.html#event:instance-connection">instance-connection</a></li><li><a href="SOCKSServer.html#event:instance-connection">instance-connection</a></li><li><a href="TorPool.html#event:instance_created">instance_created</a></li><li><a href="TorProcess.html#event:control_listen">control_listen</a></li><li><a href="TorProcess.html#event:controller_ready">controller_ready</a></li><li><a href="TorProcess.html#event:dns_listen">dns_listen</a></li><li><a href="TorProcess.html#event:error">error</a></li><li><a href="TorProcess.html#event:process_exit">process_exit</a></li><li><a href="TorProcess.html#event:ready">ready</a></li><li><a href="TorProcess.html#event:socks_listen">socks_listen</a></li></ul><h3>Global</h3><ul><li><a href="global.html#assembleHost">assembleHost</a></li><li><a href="global.html#cleanUp">cleanUp</a></li><li><a href="global.html#env_whitelist">env_whitelist</a></li><li><a href="global.html#logger">logger</a></li><li><a href="global.html#main">main</a></li><li><a href="global.html#nconf">nconf</a></li><li><a href="global.html#REALM">REALM</a></li><li><a href="global.html#setup_nconf_env">setup_nconf_env</a></li><li><a href="global.html#TOR_ROUTER_PROXY_AGENT">TOR_ROUTER_PROXY_AGENT</a></li></ul>
  23. </nav>
  24. <div id="main">
  25. <h1 class="page-title">DNSServer.js</h1>
  26. <section>
  27. <article>
  28. <pre class="prettyprint source linenums"><code>const dns = require('native-dns');
  29. const { UDPServer, Request } = dns;
  30. const Promise = require('bluebird');
  31. /**
  32. * A DNS proxy server that will route requests to instances in the TorPool provided.
  33. * @extends UDPServer
  34. */
  35. class DNSServer extends UDPServer {
  36. /**
  37. * Binds the server to a port and IP Address.
  38. *
  39. * @async
  40. * @param {number} port - The port to bind to.
  41. * @param {string} [host="::"] - Address to bind to. Will default to :: or 0.0.0.0 if not specified.
  42. * @returns {Promise}
  43. *
  44. */
  45. async listen() {
  46. let args = Array.from(arguments);
  47. let inner_func = super.serve;
  48. if (!args[1])
  49. args[1] = null;
  50. return await new Promise((resolve, reject) => {
  51. args.push(() => {
  52. let args = Array.from(arguments);
  53. resolve.apply(args);
  54. });
  55. try {
  56. inner_func.apply(this, args);
  57. } catch (error) {
  58. reject(error);
  59. }
  60. });
  61. }
  62. /**
  63. * Creates an instance of `DNSServer`.
  64. * @param {TorPool} tor_pool - The pool of instances that will be used for requests.
  65. * @param {Object} [dns_options] - Options that will be passed to the parent constructor.
  66. * @param {number} dns_timeout - How long to wait before each outbound DNS request before timing out.
  67. * @param {Logger} [logger] - Winston logger that will be used for logging. If not specified will disable logging.
  68. */
  69. constructor(tor_pool, dns_options, dns_timeout, logger) {
  70. /**
  71. * Handles an incoming DNS request.
  72. *
  73. * @function handle_request
  74. * @param {Request} req - Incoming DNS request.
  75. * @param {Response} res - Outgoing DNS response.
  76. * @private
  77. */
  78. const handle_request = (req, res) => {
  79. let connect = (tor_instance) => {
  80. for (let question of req.question) {
  81. let dns_port = (tor_instance.dns_port);
  82. let outbound_req = Request({
  83. question,
  84. server: { address: '127.0.0.1', port: dns_port, type: 'udp' },
  85. timeout: this.dns_timeout
  86. });
  87. outbound_req.on('message', (err, answer) => {
  88. if (!err &amp;&amp; answer) {
  89. for (let a of answer.answer){
  90. res.answer.push(a);
  91. }
  92. }
  93. });
  94. outbound_req.on('error', (err) => {
  95. this.logger.error(`[dns]: an error occured while handling the request: ${err.message}`);
  96. });
  97. outbound_req.on('end', () => {
  98. let source = { hostname: req.address.address, port: req.address.port, proto: 'dns' };
  99. /**
  100. * Fires when the proxy has made a connection through an instance.
  101. *
  102. * @event DNSServer#instance-connection
  103. * @param {TorProcess} instance - Instance that has been connected to.
  104. * @param {InstanceConnectionSource} source - Details on the source of the connection.
  105. */
  106. this.emit('instance_connection', tor_instance, source);
  107. this.logger.verbose(`[dns]: ${source.hostname}:${source.port} → 127.0.0.1:${dns_port}${tor_instance.definition.Name ? ' ('+tor_instance.definition.Name+')' : '' }`);
  108. res.send();
  109. });
  110. outbound_req.send();
  111. };
  112. };
  113. if (this.tor_pool.instances.length) {
  114. connect(this.tor_pool.next());
  115. }
  116. else {
  117. this.logger.debug(`[dns]: a connection has been attempted, but no tor instances are live... waiting for an instance to come online`);
  118. this.tor_pool.once('instance_created', connect);
  119. }
  120. };
  121. super(dns_options);
  122. /**
  123. * Winston logger
  124. * @type {Logger}
  125. * @public
  126. */
  127. this.logger = logger || require('./winston_silent_logger');
  128. /**
  129. * The pool of instances that will be used for requests.
  130. * @type {TorPool}
  131. * @public
  132. */
  133. this.tor_pool = tor_pool;
  134. /**
  135. * Timeout for each outbound DNS request
  136. * @type {number}
  137. * @public
  138. */
  139. this.dns_timeout = dns_timeout;
  140. this.on('request', handle_request);
  141. }
  142. };
  143. /**
  144. * Module that contains the {@link DNSSErver} class.
  145. * @module tor-router/DNSServer
  146. * @see DNSServer
  147. */
  148. module.exports = DNSServer;</code></pre>
  149. </article>
  150. </section>
  151. </div>
  152. <br class="clear">
  153. <footer>
  154. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Sep 25 2018 12:53:23 GMT-0400 (Eastern Daylight Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
  155. </footer>
  156. <script>prettyPrint();</script>
  157. <script src="scripts/linenumber.js"></script>
  158. </body>
  159. </html>