1 |
|
2 |
|
3 |
|
4 |
|
5 | var net = require('net');
|
6 | var tls = require('tls');
|
7 | var url = require('url');
|
8 | var events = require('events');
|
9 | var Agent = require('agent-base');
|
10 | var inherits = require('util').inherits;
|
11 | var debug = require('debug')('https-proxy-agent');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | module.exports = HttpsProxyAgent;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function HttpsProxyAgent(opts) {
|
27 | if (!(this instanceof HttpsProxyAgent)) return new HttpsProxyAgent(opts);
|
28 | if ('string' == typeof opts) opts = url.parse(opts);
|
29 | if (!opts)
|
30 | throw new Error(
|
31 | 'an HTTP(S) proxy server `host` and `port` must be specified!'
|
32 | );
|
33 | debug('creating new HttpsProxyAgent instance: %o', opts);
|
34 | Agent.call(this, opts);
|
35 |
|
36 | var proxy = Object.assign({}, opts);
|
37 |
|
38 |
|
39 | this.secureProxy = proxy.protocol ? /^https:?$/i.test(proxy.protocol) : false;
|
40 |
|
41 |
|
42 | proxy.host = proxy.hostname || proxy.host;
|
43 | proxy.port = +proxy.port || (this.secureProxy ? 443 : 80);
|
44 |
|
45 |
|
46 |
|
47 | if (this.secureProxy && !('ALPNProtocols' in proxy)) {
|
48 | proxy.ALPNProtocols = ['http 1.1']
|
49 | }
|
50 |
|
51 | if (proxy.host && proxy.path) {
|
52 |
|
53 |
|
54 |
|
55 | delete proxy.path;
|
56 | delete proxy.pathname;
|
57 | }
|
58 |
|
59 | this.proxy = proxy;
|
60 | this.defaultPort = 443;
|
61 | }
|
62 | inherits(HttpsProxyAgent, Agent);
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) {
|
71 | var proxy = this.proxy;
|
72 |
|
73 |
|
74 | var socket;
|
75 | if (this.secureProxy) {
|
76 | socket = tls.connect(proxy);
|
77 | } else {
|
78 | socket = net.connect(proxy);
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | var buffers = [];
|
86 | var buffersLength = 0;
|
87 |
|
88 | function read() {
|
89 | var b = socket.read();
|
90 | if (b) ondata(b);
|
91 | else socket.once('readable', read);
|
92 | }
|
93 |
|
94 | function cleanup() {
|
95 | socket.removeListener('end', onend);
|
96 | socket.removeListener('error', onerror);
|
97 | socket.removeListener('close', onclose);
|
98 | socket.removeListener('readable', read);
|
99 | }
|
100 |
|
101 | function onclose(err) {
|
102 | debug('onclose had error %o', err);
|
103 | }
|
104 |
|
105 | function onend() {
|
106 | debug('onend');
|
107 | }
|
108 |
|
109 | function onerror(err) {
|
110 | cleanup();
|
111 | fn(err);
|
112 | }
|
113 |
|
114 | function ondata(b) {
|
115 | buffers.push(b);
|
116 | buffersLength += b.length;
|
117 | var buffered = Buffer.concat(buffers, buffersLength);
|
118 | var str = buffered.toString('ascii');
|
119 |
|
120 | if (!~str.indexOf('\r\n\r\n')) {
|
121 |
|
122 | debug('have not received end of HTTP headers yet...');
|
123 | read();
|
124 | return;
|
125 | }
|
126 |
|
127 | var firstLine = str.substring(0, str.indexOf('\r\n'));
|
128 | var statusCode = +firstLine.split(' ')[1];
|
129 | debug('got proxy server response: %o', firstLine);
|
130 |
|
131 | if (200 == statusCode) {
|
132 |
|
133 | var sock = socket;
|
134 |
|
135 |
|
136 | buffers = buffered = null;
|
137 |
|
138 | if (opts.secureEndpoint) {
|
139 |
|
140 |
|
141 | debug(
|
142 | 'upgrading proxy-connected socket to TLS connection: %o',
|
143 | opts.host
|
144 | );
|
145 | opts.socket = socket;
|
146 | opts.servername = opts.servername || opts.host;
|
147 | opts.host = null;
|
148 | opts.hostname = null;
|
149 | opts.port = null;
|
150 | sock = tls.connect(opts);
|
151 | }
|
152 |
|
153 | cleanup();
|
154 | req.once('socket', resume);
|
155 | fn(null, sock);
|
156 | } else {
|
157 |
|
158 |
|
159 |
|
160 | cleanup();
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | socket.destroy();
|
169 | socket = new events.EventEmitter();
|
170 |
|
171 |
|
172 | buffers = buffered;
|
173 |
|
174 |
|
175 | req.once('socket', onsocket);
|
176 |
|
177 | fn(null, socket);
|
178 | }
|
179 | }
|
180 |
|
181 | function onsocket(socket) {
|
182 | debug('replaying proxy buffer for failed request');
|
183 |
|
184 |
|
185 |
|
186 | if (socket.listenerCount('data') > 0) {
|
187 | socket.emit('data', buffers);
|
188 | } else {
|
189 |
|
190 | throw new Error('should not happen...');
|
191 | }
|
192 |
|
193 |
|
194 | buffers = null;
|
195 | }
|
196 |
|
197 | socket.on('error', onerror);
|
198 | socket.on('close', onclose);
|
199 | socket.on('end', onend);
|
200 |
|
201 | read();
|
202 |
|
203 | var hostname = opts.host + ':' + opts.port;
|
204 | var msg = 'CONNECT ' + hostname + ' HTTP/1.1\r\n';
|
205 |
|
206 | var headers = Object.assign({}, proxy.headers);
|
207 | if (proxy.auth) {
|
208 | headers['Proxy-Authorization'] =
|
209 | 'Basic ' + Buffer.from(proxy.auth).toString('base64');
|
210 | }
|
211 |
|
212 |
|
213 |
|
214 | var host = opts.host;
|
215 | if (!isDefaultPort(opts.port, opts.secureEndpoint)) {
|
216 | host += ':' + opts.port;
|
217 | }
|
218 | headers['Host'] = host;
|
219 |
|
220 | headers['Connection'] = 'close';
|
221 | Object.keys(headers).forEach(function(name) {
|
222 | msg += name + ': ' + headers[name] + '\r\n';
|
223 | });
|
224 |
|
225 | socket.write(msg + '\r\n');
|
226 | };
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | function resume(socket) {
|
236 | socket.resume();
|
237 | }
|
238 |
|
239 | function isDefaultPort(port, secure) {
|
240 | return Boolean((!secure && port === 80) || (secure && port === 443));
|
241 | }
|