[6a3a178] | 1 | https-proxy-agent
|
---|
| 2 | ================
|
---|
| 3 | ### An HTTP(s) proxy `http.Agent` implementation for HTTPS
|
---|
| 4 | [![Build Status](https://github.com/TooTallNate/node-https-proxy-agent/workflows/Node%20CI/badge.svg)](https://github.com/TooTallNate/node-https-proxy-agent/actions?workflow=Node+CI)
|
---|
| 5 |
|
---|
| 6 | This module provides an `http.Agent` implementation that connects to a specified
|
---|
| 7 | HTTP or HTTPS proxy server, and can be used with the built-in `https` module.
|
---|
| 8 |
|
---|
| 9 | Specifically, this `Agent` implementation connects to an intermediary "proxy"
|
---|
| 10 | server and issues the [CONNECT HTTP method][CONNECT], which tells the proxy to
|
---|
| 11 | open a direct TCP connection to the destination server.
|
---|
| 12 |
|
---|
| 13 | Since this agent implements the CONNECT HTTP method, it also works with other
|
---|
| 14 | protocols that use this method when connecting over proxies (i.e. WebSockets).
|
---|
| 15 | See the "Examples" section below for more.
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | Installation
|
---|
| 19 | ------------
|
---|
| 20 |
|
---|
| 21 | Install with `npm`:
|
---|
| 22 |
|
---|
| 23 | ``` bash
|
---|
| 24 | $ npm install https-proxy-agent
|
---|
| 25 | ```
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | Examples
|
---|
| 29 | --------
|
---|
| 30 |
|
---|
| 31 | #### `https` module example
|
---|
| 32 |
|
---|
| 33 | ``` js
|
---|
| 34 | var url = require('url');
|
---|
| 35 | var https = require('https');
|
---|
| 36 | var HttpsProxyAgent = require('https-proxy-agent');
|
---|
| 37 |
|
---|
| 38 | // HTTP/HTTPS proxy to connect to
|
---|
| 39 | var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';
|
---|
| 40 | console.log('using proxy server %j', proxy);
|
---|
| 41 |
|
---|
| 42 | // HTTPS endpoint for the proxy to connect to
|
---|
| 43 | var endpoint = process.argv[2] || 'https://graph.facebook.com/tootallnate';
|
---|
| 44 | console.log('attempting to GET %j', endpoint);
|
---|
| 45 | var options = url.parse(endpoint);
|
---|
| 46 |
|
---|
| 47 | // create an instance of the `HttpsProxyAgent` class with the proxy server information
|
---|
| 48 | var agent = new HttpsProxyAgent(proxy);
|
---|
| 49 | options.agent = agent;
|
---|
| 50 |
|
---|
| 51 | https.get(options, function (res) {
|
---|
| 52 | console.log('"response" event!', res.headers);
|
---|
| 53 | res.pipe(process.stdout);
|
---|
| 54 | });
|
---|
| 55 | ```
|
---|
| 56 |
|
---|
| 57 | #### `ws` WebSocket connection example
|
---|
| 58 |
|
---|
| 59 | ``` js
|
---|
| 60 | var url = require('url');
|
---|
| 61 | var WebSocket = require('ws');
|
---|
| 62 | var HttpsProxyAgent = require('https-proxy-agent');
|
---|
| 63 |
|
---|
| 64 | // HTTP/HTTPS proxy to connect to
|
---|
| 65 | var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';
|
---|
| 66 | console.log('using proxy server %j', proxy);
|
---|
| 67 |
|
---|
| 68 | // WebSocket endpoint for the proxy to connect to
|
---|
| 69 | var endpoint = process.argv[2] || 'ws://echo.websocket.org';
|
---|
| 70 | var parsed = url.parse(endpoint);
|
---|
| 71 | console.log('attempting to connect to WebSocket %j', endpoint);
|
---|
| 72 |
|
---|
| 73 | // create an instance of the `HttpsProxyAgent` class with the proxy server information
|
---|
| 74 | var options = url.parse(proxy);
|
---|
| 75 |
|
---|
| 76 | var agent = new HttpsProxyAgent(options);
|
---|
| 77 |
|
---|
| 78 | // finally, initiate the WebSocket connection
|
---|
| 79 | var socket = new WebSocket(endpoint, { agent: agent });
|
---|
| 80 |
|
---|
| 81 | socket.on('open', function () {
|
---|
| 82 | console.log('"open" event!');
|
---|
| 83 | socket.send('hello world');
|
---|
| 84 | });
|
---|
| 85 |
|
---|
| 86 | socket.on('message', function (data, flags) {
|
---|
| 87 | console.log('"message" event! %j %j', data, flags);
|
---|
| 88 | socket.close();
|
---|
| 89 | });
|
---|
| 90 | ```
|
---|
| 91 |
|
---|
| 92 | API
|
---|
| 93 | ---
|
---|
| 94 |
|
---|
| 95 | ### new HttpsProxyAgent(Object options)
|
---|
| 96 |
|
---|
| 97 | The `HttpsProxyAgent` class implements an `http.Agent` subclass that connects
|
---|
| 98 | to the specified "HTTP(s) proxy server" in order to proxy HTTPS and/or WebSocket
|
---|
| 99 | requests. This is achieved by using the [HTTP `CONNECT` method][CONNECT].
|
---|
| 100 |
|
---|
| 101 | The `options` argument may either be a string URI of the proxy server to use, or an
|
---|
| 102 | "options" object with more specific properties:
|
---|
| 103 |
|
---|
| 104 | * `host` - String - Proxy host to connect to (may use `hostname` as well). Required.
|
---|
| 105 | * `port` - Number - Proxy port to connect to. Required.
|
---|
| 106 | * `protocol` - String - If `https:`, then use TLS to connect to the proxy.
|
---|
| 107 | * `headers` - Object - Additional HTTP headers to be sent on the HTTP CONNECT method.
|
---|
| 108 | * Any other options given are passed to the `net.connect()`/`tls.connect()` functions.
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | License
|
---|
| 112 | -------
|
---|
| 113 |
|
---|
| 114 | (The MIT License)
|
---|
| 115 |
|
---|
| 116 | Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
|
---|
| 117 |
|
---|
| 118 | Permission is hereby granted, free of charge, to any person obtaining
|
---|
| 119 | a copy of this software and associated documentation files (the
|
---|
| 120 | 'Software'), to deal in the Software without restriction, including
|
---|
| 121 | without limitation the rights to use, copy, modify, merge, publish,
|
---|
| 122 | distribute, sublicense, and/or sell copies of the Software, and to
|
---|
| 123 | permit persons to whom the Software is furnished to do so, subject to
|
---|
| 124 | the following conditions:
|
---|
| 125 |
|
---|
| 126 | The above copyright notice and this permission notice shall be
|
---|
| 127 | included in all copies or substantial portions of the Software.
|
---|
| 128 |
|
---|
| 129 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
---|
| 130 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
---|
| 131 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
---|
| 132 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
---|
| 133 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
---|
| 134 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
---|
| 135 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
| 136 |
|
---|
| 137 | [CONNECT]: http://en.wikipedia.org/wiki/HTTP_tunnel#HTTP_CONNECT_Tunneling
|
---|