1 | proxy-agent
|
2 | ===========
|
3 | ### Maps proxy protocols to `http.Agent` implementations
|
4 |
|
5 | This module provides a function that returns proxying `http.Agent` instances to
|
6 | use based off of a given proxy URI.
|
7 |
|
8 | An LRU cache is used so that `http.Agent` instances are transparently re-used for
|
9 | subsequent HTTP requests to the same proxy server.
|
10 |
|
11 | The currently implemented protocol mappings are listed in the table below:
|
12 |
|
13 |
|
14 | | Protocol | Proxy Agent for `http` requests | Proxy Agent for `https` requests | Example
|
15 | |:---------:|:-------------------------------:|:--------------------------------:|:--------:
|
16 | | `http` | [http-proxy-agent][] | [https-proxy-agent][] | `http://proxy-server-over-tcp.com:3128`
|
17 | | `https` | [http-proxy-agent][] | [https-proxy-agent][] | `https://proxy-server-over-tls.com:3129`
|
18 | | `socks` | [socks-proxy-agent][] | [socks-proxy-agent][] | `socks://some-socks-proxy.com:9050`
|
19 |
|
20 |
|
21 | Installation
|
22 | ------------
|
23 |
|
24 | Install with `npm`:
|
25 |
|
26 | ``` bash
|
27 | $ npm install proxy-agent
|
28 | ```
|
29 |
|
30 |
|
31 | Example
|
32 | -------
|
33 |
|
34 | ``` js
|
35 | var http = require('http');
|
36 | var proxy = require('proxy-agent');
|
37 |
|
38 | // HTTP, HTTPS, or SOCKS proxy to use
|
39 | var proxyUri = process.env.http_proxy || 'http://168.63.43.102:3128';
|
40 |
|
41 | var opts = {
|
42 | method: 'GET',
|
43 | host: 'jsonip.org',
|
44 | path: '/',
|
45 | // this is the important part!
|
46 | agent: proxy(proxyUri)
|
47 | };
|
48 |
|
49 | // the rest works just like any other normal HTTP request
|
50 | http.get(opts, onresponse);
|
51 |
|
52 | function onresponse (res) {
|
53 | console.log(res.statusCode, res.headers);
|
54 | res.pipe(process.stdout);
|
55 | }
|
56 | ```
|
57 |
|
58 |
|
59 | API
|
60 | ---
|
61 |
|
62 | ### proxy(String uri, Boolean secure) → http.Agent
|
63 |
|
64 | Returns an `http.Agent` instance based off of the given proxy `uri`, and `secure`
|
65 | boolean flag. An LRU cache is used, so the same `http.Agent` instance will be
|
66 | returned if identical args are passed in.
|
67 |
|
68 |
|
69 | License
|
70 | -------
|
71 |
|
72 | (The MIT License)
|
73 |
|
74 | Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
|
75 |
|
76 | Permission is hereby granted, free of charge, to any person obtaining
|
77 | a copy of this software and associated documentation files (the
|
78 | 'Software'), to deal in the Software without restriction, including
|
79 | without limitation the rights to use, copy, modify, merge, publish,
|
80 | distribute, sublicense, and/or sell copies of the Software, and to
|
81 | permit persons to whom the Software is furnished to do so, subject to
|
82 | the following conditions:
|
83 |
|
84 | The above copyright notice and this permission notice shall be
|
85 | included in all copies or substantial portions of the Software.
|
86 |
|
87 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
88 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
89 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
90 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
91 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
92 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
93 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
94 |
|
95 |
|
96 | [http-proxy-agent]: https://github.com/TooTallNate/node-http-proxy-agent
|
97 | [https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
|
98 | [socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
|