Skip to content

Commit a51fa83

Browse files
committed
upgrade to "agent-base" v2 API
Simplifies the code here quite a bit: - User no longer need to specify `secureEndpoint`; that happens implicitly based on whether "http" or "https" module is used. - No longer need to merge default `port` options, "agent-base" handles that for us now. - No longer need to merge the `proxy` options to get things like `rejectUnauthorized`, since "agent-base" now always passes those values through properly.
1 parent 2b8c9e1 commit a51fa83

3 files changed

Lines changed: 59 additions & 54 deletions

File tree

https-proxy-agent.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ function HttpsProxyAgent (opts) {
3636
// if `true`, then connect to the proxy server over TLS. defaults to `false`.
3737
this.secureProxy = proxy.protocol ? /^https:?$/i.test(proxy.protocol) : false;
3838

39-
// if `true`, then connect to the destination endpoint over TLS, defaults to `true`
40-
this.secureEndpoint = opts.secureEndpoint !== false;
41-
4239
// prefer `hostname` over `host`, and set the `port` if needed
4340
proxy.host = proxy.hostname || proxy.host;
4441
proxy.port = +proxy.port || (this.secureProxy ? 443 : 80);
@@ -55,42 +52,24 @@ function HttpsProxyAgent (opts) {
5552
}
5653
inherits(HttpsProxyAgent, Agent);
5754

58-
/**
59-
* Default options for the "connect" opts object.
60-
*/
61-
62-
var defaults = { port: 80 };
63-
var secureDefaults = { port: 443 };
64-
6555
/**
6656
* Called when the node-core HTTP client library is creating a new HTTP request.
6757
*
6858
* @api public
6959
*/
7060

71-
function connect (req, _opts, fn) {
61+
function connect (req, opts, fn) {
7262

7363
var proxy = this.proxy;
74-
var secureProxy = this.secureProxy;
75-
var secureEndpoint = this.secureEndpoint;
7664

7765
// create a socket connection to the proxy server
7866
var socket;
79-
if (secureProxy) {
67+
if (this.secureProxy) {
8068
socket = tls.connect(proxy);
8169
} else {
8270
socket = net.connect(proxy);
8371
}
8472

85-
// these `opts` are the connect options to connect to the destination endpoint
86-
// XXX: we mix in the proxy options so that TLS options like
87-
// `rejectUnauthorized` get passed to the destination endpoint as well
88-
var proxyOpts = extend({}, proxy);
89-
delete proxyOpts.host;
90-
delete proxyOpts.hostname;
91-
delete proxyOpts.port;
92-
var opts = extend({}, proxyOpts, secureEndpoint ? secureDefaults : defaults, _opts);
93-
9473
// we need to buffer any HTTP traffic that happens with the proxy before we get
9574
// the CONNECT response, so that if the response is anything other than an "200"
9675
// response code, then we can re-play the "data" events on the socket once the
@@ -153,7 +132,7 @@ function connect (req, _opts, fn) {
153132
// nullify the buffered data since we won't be needing it
154133
buffers = buffered = null;
155134

156-
if (secureEndpoint) {
135+
if (opts.secureEndpoint) {
157136
// since the proxy is connecting to an SSL server, we have
158137
// to upgrade this socket connection to an SSL connection
159138
debug('upgrading proxy-connected socket to TLS connection: %o', opts.host);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"url": "https://github.com/TooTallNate/node-https-proxy-agent/issues"
2323
},
2424
"dependencies": {
25-
"agent-base": "~1.0.1",
25+
"agent-base": "2",
2626
"debug": "2",
2727
"extend": "3"
2828
},

test/test.js

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,6 @@ describe('HttpsProxyAgent', function () {
109109
assert.equal('127.0.0.1', agent.proxy.host);
110110
assert.equal(proxyPort, agent.proxy.port);
111111
});
112-
describe('secureEndpoint', function () {
113-
it('should default to `true`', function () {
114-
var agent = new HttpsProxyAgent('http://127.0.0.1:' + proxyPort);
115-
assert.equal(true, agent.secureEndpoint);
116-
});
117-
it('should be `false` when passed in as an option', function () {
118-
var opts = url.parse('http://127.0.0.1:' + proxyPort);
119-
opts.secureEndpoint = false;
120-
var agent = new HttpsProxyAgent(opts);
121-
assert.equal(false, agent.secureEndpoint);
122-
});
123-
it('should be `true` when passed in as an option', function () {
124-
var opts = url.parse('http://127.0.0.1:' + proxyPort);
125-
opts.secureEndpoint = true;
126-
var agent = new HttpsProxyAgent(opts);
127-
assert.equal(true, agent.secureEndpoint);
128-
});
129-
});
130112
describe('secureProxy', function () {
131113
it('should default to `false`', function () {
132114
var agent = new HttpsProxyAgent({ port: proxyPort });
@@ -153,6 +135,61 @@ describe('HttpsProxyAgent', function () {
153135
delete proxy.authenticate;
154136
});
155137

138+
it('should work over an HTTP proxy', function (done) {
139+
server.once('request', function (req, res) {
140+
res.end(JSON.stringify(req.headers));
141+
});
142+
143+
var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
144+
var agent = new HttpsProxyAgent(proxy);
145+
146+
var opts = url.parse('http://127.0.0.1:' + serverPort);
147+
opts.agent = agent;
148+
149+
var req = http.get(opts, function (res) {
150+
var data = '';
151+
res.setEncoding('utf8');
152+
res.on('data', function (b) {
153+
data += b;
154+
});
155+
res.on('end', function () {
156+
data = JSON.parse(data);
157+
assert.equal('127.0.0.1:' + serverPort, data.host);
158+
done();
159+
});
160+
});
161+
req.once('error', done);
162+
});
163+
// XXX: doesn't fire the HTTP "response" event for some reason :\
164+
// TODO: look into this some more and fix, even though proxy servers
165+
// over TLS are pretty rare to begin with
166+
it.skip('should work over an HTTPS proxy', function (done) {
167+
sslServer.once('request', function (req, res) {
168+
res.end(JSON.stringify(req.headers));
169+
});
170+
171+
var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://127.0.0.1:' + sslProxyPort;
172+
proxy = url.parse(proxy);
173+
proxy.rejectUnauthorized = false;
174+
var agent = new HttpsProxyAgent(proxy);
175+
176+
var opts = url.parse('http://127.0.0.1:' + serverPort);
177+
opts.agent = agent;
178+
179+
http.get(opts, function (res) {
180+
var data = '';
181+
res.setEncoding('utf8');
182+
res.on('data', function (b) {
183+
data += b;
184+
});
185+
res.on('end', function () {
186+
data = JSON.parse(data);
187+
console.log(data);
188+
assert.equal('127.0.0.1:' + serverPort, data.host);
189+
done();
190+
});
191+
});
192+
});
156193
it('should receive the 407 authorization code on the `http.ClientResponse`', function (done) {
157194
// set a proxy authentication function for this test
158195
proxy.authenticate = function (req, fn) {
@@ -194,19 +231,11 @@ describe('HttpsProxyAgent', function () {
194231

195232
describe('"https" module', function () {
196233
it('should work over an HTTP proxy', function (done) {
197-
// set HTTP "request" event handler for this test
198234
sslServer.once('request', function (req, res) {
199235
res.end(JSON.stringify(req.headers));
200236
});
201237

202238
var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://127.0.0.1:' + proxyPort;
203-
proxy = url.parse(proxy);
204-
// `rejectUnauthorized` shoudn't *technically* be necessary here,
205-
// but up until node v0.11.6, the `http.Agent` class didn't have
206-
// access to the *entire* request "options" object. Thus,
207-
// `https-proxy-agent` will *also* merge in options you pass here
208-
// to the destination endpoints…
209-
proxy.rejectUnauthorized = false;
210239
var agent = new HttpsProxyAgent(proxy);
211240

212241
var opts = url.parse('https://127.0.0.1:' + sslServerPort);
@@ -232,15 +261,12 @@ describe('HttpsProxyAgent', function () {
232261
// See: https://github.com/joyent/node/issues/6204
233262

234263
it('should work over an HTTPS proxy', function (done) {
235-
// set HTTP "request" event handler for this test
236264
sslServer.once('request', function (req, res) {
237265
res.end(JSON.stringify(req.headers));
238266
});
239267

240268
var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://127.0.0.1:' + sslProxyPort;
241269
proxy = url.parse(proxy);
242-
// `rejectUnauthorized` is actually necessary this time since the HTTPS
243-
// proxy server itself is using a self-signed SSL certificate…
244270
proxy.rejectUnauthorized = false;
245271
var agent = new HttpsProxyAgent(proxy);
246272

0 commit comments

Comments
 (0)