forked from cloudflare/node-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): support HTTPS_PROXY and NO_PROXY environment variables
By default, use the system-defined HTTPS proxy settings when connecting to the Cloudflare API. This module will use the proxy defined by HTTPS_PROXY unless the Cloudflare API host is matched by the pattern specified in NO_PROXY. Bug: cloudflare#36
- Loading branch information
1 parent
6b8177a
commit fdd155a
Showing
6 changed files
with
189 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2014-present Cloudflare, Inc. | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const shouldProxy = require('should-proxy'); | ||
const HttpsProxyAgent = require('https-proxy-agent'); | ||
|
||
/** | ||
* proxyAgent returns an HTTPS agent to use to access the base URL. | ||
* | ||
* @private | ||
* @param {string} httpsProxy - HTTPS Proxy URL | ||
* @param {string} noProxy - URLs that should be excluded from proxying | ||
* @param {string} base - The client base URL | ||
* @returns {https.Agent|null} - The HTTPS agent, if required to access the base URL. | ||
*/ | ||
const proxyAgent = function proxyAgent(httpsProxy, noProxy, base) { | ||
if (!httpsProxy) { | ||
return null; | ||
} | ||
noProxy = noProxy || ''; | ||
|
||
const ok = shouldProxy(base, { | ||
no_proxy: noProxy, // eslint-disable-line camelcase | ||
}); | ||
|
||
if (!ok) { | ||
return null; | ||
} | ||
|
||
return new HttpsProxyAgent(httpsProxy); | ||
}; | ||
|
||
module.exports.proxyAgent = proxyAgent; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (C) 2014-present Cloudflare, Inc. | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const assert = require('power-assert'); | ||
const mocha = require('mocha'); | ||
|
||
const proxy = require('../lib/proxy'); | ||
|
||
const describe = mocha.describe; | ||
const it = mocha.it; | ||
|
||
describe('proxy agents', () => { | ||
it('should not return an agent when parameters are not set', done => { | ||
const tests = [ | ||
['', '', 'example.com'], | ||
[null, null, 'http://example.com/'], | ||
]; | ||
|
||
tests.forEach(test => { | ||
const agent = proxy.proxyAgent(test[0], test[1], test[2]); | ||
|
||
assert.ok(!agent, 'agent was unexpected'); | ||
}); | ||
|
||
done(); | ||
}); | ||
|
||
it('should not return an agent when noProxy matches base', done => { | ||
const tests = [ | ||
['http://10.0.0.1:1234', 'example.com', 'http://example.com'], | ||
['http://10.0.0.1:1234', '.example.com', 'http://example.com'], | ||
['http://10.0.0.1:1234', 'foobar.com,.example.com', 'http://example.com'], | ||
]; | ||
|
||
tests.forEach(test => { | ||
const agent = proxy.proxyAgent(test[0], test[1], test[2]); | ||
|
||
assert.ok(!agent, 'agent was unexpected'); | ||
}); | ||
|
||
done(); | ||
}); | ||
|
||
it('should return an agent when noProxy is not set', done => { | ||
const tests = [ | ||
['http://10.0.0.1:1234', null, 'http://example.com'], | ||
['http://10.0.0.1:1234', '', 'http://example.com'], | ||
]; | ||
|
||
tests.forEach(test => { | ||
const agent = proxy.proxyAgent(test[0], test[1], test[2]); | ||
|
||
assert.ok(agent, 'expected an agent'); | ||
}); | ||
|
||
done(); | ||
}); | ||
|
||
it("should return an agent when noProxy doesn't match", done => { | ||
const agent = proxy.proxyAgent( | ||
'http://10.0.0.1:1234', | ||
'.example.com', | ||
'https://example.org' | ||
); | ||
|
||
assert.ok(agent, 'expected an agent'); | ||
|
||
done(); | ||
}); | ||
}); |