1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var url = require('url');
|
7 | var LRU = require('lru-cache');
|
8 | var HttpProxyAgent = require('http-proxy-agent');
|
9 | var HttpsProxyAgent = require('https-proxy-agent');
|
10 | var PacProxyAgent = require('pac-proxy-agent');
|
11 | var SocksProxyAgent = require('socks-proxy-agent');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | exports = module.exports = proxy;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var cacheSize = 20;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | exports.cache = new LRU(cacheSize);
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | exports.proxies = Object.create(null);
|
39 | exports.proxies.http = httpOrHttpsProxy;
|
40 | exports.proxies.https = httpOrHttpsProxy;
|
41 | exports.proxies.socks = socksProxy;
|
42 |
|
43 | PacProxyAgent.protocols.forEach(function (protocol) {
|
44 | exports.proxies['pac+' + protocol] = pacProxy;
|
45 | });
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function proxy (uri, secure) {
|
61 |
|
62 | if (!uri) {
|
63 | throw new TypeError('You must pass a proxy "uri" to connect to');
|
64 | }
|
65 |
|
66 |
|
67 | var proxyParsed = uri;
|
68 | if ('string' == typeof uri) {
|
69 | proxyParsed = url.parse(uri);
|
70 | }
|
71 |
|
72 |
|
73 | var protocol = proxyParsed.protocol;
|
74 | if (!protocol) {
|
75 | throw new TypeError('You must specify a string "protocol" for the ' +
|
76 | 'proxy type (' + types().join(', ') + ')');
|
77 | }
|
78 |
|
79 |
|
80 | if (':' == protocol[protocol.length - 1]) {
|
81 | protocol = protocol.substring(0, protocol.length - 1);
|
82 | }
|
83 |
|
84 |
|
85 | var proxyFn = exports.proxies[protocol];
|
86 | if ('function' != typeof proxyFn) {
|
87 | throw new TypeError('unsupported proxy protocol: "' + protocol + '"');
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | var proxyUri = url.format({
|
94 | protocol: protocol + ':',
|
95 | slashes: true,
|
96 | hostname: proxyParsed.hostname || proxyParsed.host,
|
97 | port: proxyParsed.port
|
98 | });
|
99 |
|
100 |
|
101 | var key = proxyUri;
|
102 | if (secure) key += ' secure';
|
103 |
|
104 |
|
105 | var agent = exports.cache.get(key);
|
106 | if (!agent) {
|
107 |
|
108 | agent = proxyFn(proxyParsed, secure);
|
109 | if (agent) exports.cache.set(key, agent);
|
110 | } else {
|
111 |
|
112 | }
|
113 |
|
114 | return agent;
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | function httpOrHttpsProxy (proxy, secure) {
|
124 | if (secure) {
|
125 |
|
126 | return new HttpsProxyAgent(proxy);
|
127 | } else {
|
128 |
|
129 | return new HttpProxyAgent(proxy);
|
130 | }
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | function socksProxy (proxy, secure) {
|
140 | return new SocksProxyAgent(proxy, secure);
|
141 | }
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | function pacProxy (proxy, secure) {
|
150 | var agent = new PacProxyAgent(proxy, secure);
|
151 | agent.secureEndpoint = secure;
|
152 | return agent;
|
153 | }
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | function types () {
|
163 | var rtn = [];
|
164 |
|
165 |
|
166 | for (var type in exports.proxies) rtn.push(type);
|
167 | return rtn;
|
168 | }
|