123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
-
- <meta charset="utf-8">
- <title>HTTPServer.js - Documentation</title>
-
-
- <script src="scripts/prettify/prettify.js"></script>
- <script src="scripts/prettify/lang-css.js"></script>
- <!--[if lt IE 9]>
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <link type="text/css" rel="stylesheet" href="styles/prettify.css">
- <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- <input type="checkbox" id="nav-trigger" class="nav-trigger" />
- <label for="nav-trigger" class="navicon-button x">
- <div class="navicon"></div>
- </label>
- <label for="nav-trigger" class="overlay"></label>
- <nav>
-
- <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>
- </nav>
- <div id="main">
-
- <h1 class="page-title">HTTPServer.js</h1>
-
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>const http = require('http');
- const URL = require('url');
- const { Server } = http;
- const Promise = require('bluebird');
- const socks = require('socksv5');
- /**
- * Value of the "Proxy-Agent" header that will be sent with each http-connect (https) request
- * @constant
- * @type {string}
- * @default
- */
- const TOR_ROUTER_PROXY_AGENT = 'tor-router';
- /**
- * What will show up when an unauthenticated user attempts to connect when an invalid username
- * @constant
- * @type {string}
- * @default
- */
- const REALM = 'Name of instance to route to';
- /**
- * A HTTP(S) proxy server that will route requests to instances in the TorPool provided.
- * @extends Server
- */
- class HTTPServer extends Server {
- /**
- * Binds the server to a port and IP Address.
- *
- * @async
- * @param {number} port - The port to bind to
- * @param {string} [host="::"] - Address to bind to. Will default to :: or 0.0.0.0 if not specified.
- * @returns {Promise}
- *
- */
- async listen() {
- return await new Promise((resolve, reject) => {
- let args = Array.from(arguments);
- let inner_func = super.listen;
- args.push(() => {
- let args = Array.from(arguments);
- resolve.apply(args);
- });
- inner_func.apply(this, args);
- });
- }
- /**
- * Handles username authentication for HTTP requests
- * @param {ClientRequest} req - Incoming HTTP request
- * @param {ClientResponse} res - Outgoing HTTP response
- * @private
- */
- authenticate_user_http(req, res) {
- return this.authenticate_user(req, () => {
- res.writeHead(407, { 'Proxy-Authenticate': `Basic realm="${REALM}"` });
- res.end();
- return false;
- })
- }
- /**
- * Handles username authentication for HTTP-Connect requests
- * @param {ClientRequest} req - Incoming HTTP request
- * @param {Socket} socket - Inbound HTTP-Connect socket
- * @private
- */
- authenticate_user_connect(req, socket) {
- return this.authenticate_user(req, () => {
- socket.write(`HTTP/1.1 407 Proxy Authentication Required\r\n'+'Proxy-Authenticate: Basic realm="${REALM}"\r\n` +'\r\n');
- socket.end();
- return false;
- })
- }
- /**
- * Checks the username provided against all groups (for "group" mode) or all instances (for "individual" mode).
- * @param {ClientRequest} req - Incoming HTTP request
- * @param {Function} deny - Function that when called will deny the connection to the proxy server and prompt the user for credentials (HTTP 407).
- * @private
- * @throws If the {@link HTTPServer#proxy_by_name} mode is invalid
- */
- authenticate_user(req, deny) {
- if (!this.proxy_by_name)
- return true;
-
- let deny_un = this.proxy_by_name.deny_unidentified_users;
-
- let header = req.headers['authorization'] || req.headers['proxy-authorization'];
- if (!header && deny_un) return deny();
- else if (!header) return true;
- let token = header.split(/\s+/).pop();
- if (!token && deny_un) return deny();
- else if (!token) return true;
- let buf = new Buffer.from(token, 'base64').toString();
- if ( !buf && deny_un ) return deny();
- else if (!buf) return true;
- let username = buf.split(/:/).shift();
- if ( !username && deny_un ) return deny();
- else if (!username) return true;
- let instance;
- if (this.proxy_by_name.mode === 'individual')
- instance = this.tor_pool.instance_by_name(username);
- else if (this.proxy_by_name.mode === 'group') {
- if (!this.tor_pool.group_names.has(username)) return deny();
- instance = this.tor_pool.next_by_group(username);
- }
- else
- throw Error(`Unknown "proxy_by_name" mode ${this.proxy_by_name.mode}`);
-
- if (!instance) return deny();
- req.instance = instance;
- return true;
- }
- /**
- * Creates an instance of `HTTPServer`.
- * @param {TorPool} tor_pool - The pool of instances that will be used for requests.
- * @param {Logger} [logger] - Winston logger that will be used for logging. If not specified will disable logging.
- * @param {ProxyByNameConfig} [proxy_by_name] - Enable routing to specific instances or groups of instances using the username field (http://instance-1:@my-server:9050) when connecting.
- */
- constructor(tor_pool, logger, proxy_by_name) {
- /**
- * Handles incoming HTTP Connections.
- * @function handle_http_connections
- * @param {ClientRequest} - Incoming HTTP request.
- * @param {ClientResponse} - Outgoing HTTP response.
- * @private
- */
- const handle_http_connections = (req, res) => {
- if (!this.authenticate_user_http(req, res))
- return;
-
- let { instance } = req;
-
- let url = URL.parse(req.url);
- url.port = url.port || 80;
- let buffer = [];
- function onIncomingData(chunk) {
- buffer.push(chunk);
- }
- function preConnectClosed() {
- req.finished = true;
- }
- req.on('data', onIncomingData);
- req.on('end', preConnectClosed);
- req.on('error', function (err) {
- this.logger.error("[http-proxy]: an error occured: "+err.message);
- });
- let connect = (tor_instance) => {
- let source = { hostname: req.connection.remoteAddress, port: req.connection.remotePort, proto: 'http', by_name: Boolean(instance) };
- let socks_port = tor_instance.socks_port;
-
- let proxy_req = http.request({
- method: req.method,
- hostname: url.hostname,
- port: url.port,
- path: url.path,
- headers: req.headers,
- agent: socks.HttpAgent({
- proxyHost: '127.0.0.1',
- proxyPort: socks_port,
- auths: [ socks.auth.None() ],
- localDNS: false
- })
- }, (proxy_res) => {
- /**
- * Fires when the proxy has made a connection through an instance using HTTP or HTTP-Connect.
- *
- * @event HTTPServer#instance-connection
- * @param {TorProcess} instance - Instance that has been connected to.
- * @param {InstanceConnectionSource} source - Details on the source of the connection.
- */
- this.emit('instance_connection', tor_instance, source);
- this.logger.verbose(`[http-proxy]: ${source.hostname}:${source.port} → 127.0.0.1:${socks_port}${tor_instance.definition.Name ? ' ('+tor_instance.definition.Name+')' : '' } → ${url.hostname}:${url.port}`);
-
- proxy_res.on('data', (chunk) => {
- res.write(chunk);
- });
- proxy_res.on('end', () => {
- res.end();
- });
- res.writeHead(proxy_res.statusCode, proxy_res.headers);
- });
- req.removeListener('data', onIncomingData);
- req.on('data', (chunk) => {
- proxy_req.write(chunk);
- })
- req.on('end', () => {
- proxy_req.end();
- })
- while (buffer.length) {
- proxy_req.write(buffer.shift());
- }
- if (req.finished)
- proxy_req.end();
- };
-
- if (instance) {
- if (instance.ready) {
- connect(instance);
- }
- else {
- this.logger.debug(`[http-proxy]: a connection has been attempted to "${instance.instance_name}", but it is not live... waiting for the instance to come online`);
- instance.once('ready', (() => connect(instance)));
- }
- }
- else if (this.tor_pool.instances.length) {
- connect(tor_pool.next());
- } else {
- this.logger.debug(`[http-proxy]: a connection has been attempted, but no tor instances are live... waiting for an instance to come online`);
- tor_pool.once('instance_created', connect);
- }
- }
- /**
- * Handles incoming HTTP-Connect connections.
- * @function handle_connect_connections
- * @param {ClientRequest} req - Incoming HTTP Request.
- * @param {Socket} inbound_socket - Incoming socket.
- * @param {Buffer|string} head - HTTP Request head.
- * @private
- */
- const handle_connect_connections = (req, inbound_socket, head) => {
- if (!this.authenticate_user_connect(req, inbound_socket))
- return;
-
- let { instance } = req;
- let hostname = req.url.split(':').shift();
- let port = Number(req.url.split(':').pop());
- let connect = (tor_instance) => {
- let source = { hostname: req.connection.remoteAddress, port: req.connection.remotePort, proto: 'http-connect', by_name: Boolean(instance) };
- let socks_port = tor_instance.socks_port;
- var outbound_socket;
- let onClose = (error) => {
- inbound_socket && inbound_socket.end();
- outbound_socket && outbound_socket.end();
- inbound_socket = outbound_socket = void(0);
- if (error instanceof Error)
- this.logger.error(`[http-connect]: an error occured: ${error.message}`)
- };
- inbound_socket.on('error', onClose);
- inbound_socket.on('close', onClose);
- socks.connect({
- host: hostname,
- port: port,
- proxyHost: '127.0.0.1',
- proxyPort: socks_port,
- localDNS: false,
- auths: [ socks.auth.None() ]
- }, ($outbound_socket) => {
- this.emit('instance_connection', tor_instance, source);
- this.logger && this.logger.verbose(`[http-connect]: ${source.hostname}:${source.port} → 127.0.0.1:${socks_port}${tor_instance.definition.Name ? ' ('+tor_instance.definition.Name+')' : '' } → ${hostname}:${port}`)
-
- outbound_socket = $outbound_socket;
- outbound_socket.on('close', onClose);
- outbound_socket.on('error', onClose);
- inbound_socket.write(`HTTP/1.1 200 Connection Established\r\n'+'Proxy-agent: ${TOR_ROUTER_PROXY_AGENT}\r\n` +'\r\n');
- outbound_socket.write(head);
- outbound_socket.pipe(inbound_socket);
- inbound_socket.pipe(outbound_socket);
- });
- };
- if (instance) {
- if (instance.ready) {
- connect(instance);
- }
- else {
- this.logger.debug(`[http-connect]: a connection has been attempted to "${instance.instance_name}", but it is not live... waiting for the instance to come online`);
- instance.once('ready', (() => connect(instance)));
- }
- }
- else if (this.tor_pool.instances.length) {
- connect(this.tor_pool.next());
- } else {
- this.logger.debug(`[http-connect]: a connection has been attempted, but no tor instances are live... waiting for an instance to come online`);
- this.tor_pool.once('instance_created', connect);
- }
- }
- super(handle_http_connections);
- this.on('connect', handle_connect_connections);
-
- /**
- * Winston logger.
- * @type {Logger}
- * @public
- */
- this.logger = logger || require('./winston_silent_logger');
- /**
- * The pool of instances that will be used for requests.
- * @type {TorPool}
- * @public
- */
- this.tor_pool = tor_pool;
- /**
- * Configuration for "proxy by name" feature.
- * @type {ProxyByNameConfig}
- * @public
- */
- this.proxy_by_name = proxy_by_name;
- }
- };
- /**
- * Module that contains the {@link HTTPServer} class.
- * @module tor-router/HTTPServer
- * @see HTTPServer
- */
- module.exports = HTTPServer;</code></pre>
- </article>
- </section>
-
-
- </div>
- <br class="clear">
- <footer>
- 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.
- </footer>
- <script>prettyPrint();</script>
- <script src="scripts/linenumber.js"></script>
- </body>
- </html>
|