UNPKG

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