-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proxy support for plugin installer #12753
Changes from 3 commits
de65b36
a499033
4434ca0
2aecac8
eb4291a
6b7b090
3a67239
fe339a8
e08c35a
bc844f7
4a422d8
04d7ffa
85b2f0e
5ba96e1
151cb79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,47 @@ import Wreck from 'wreck'; | |
import Progress from '../progress'; | ||
import { fromNode as fn } from 'bluebird'; | ||
import { createWriteStream } from 'fs'; | ||
import HttpProxyAgent from 'http-proxy-agent'; | ||
import URL from 'url'; | ||
|
||
function sendRequest({ sourceUrl, timeout }) { | ||
function getProxyAgent(sourceUrl, logger) { | ||
const httpProxy = process.env.http_proxy || process.env.HTTP_PROXY; | ||
// we have a proxy detected, lets use it | ||
if (httpProxy) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe change to early return instead?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have some styleguide about early return? I am personally not a fan of early return, if it's not the very first statement and just checks function parameters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
logger.log(`Picked up proxy ${httpProxy} from http_proxy environment variable.`); | ||
// get the hostname of the sourceUrl | ||
const hostname = URL.parse(sourceUrl).hostname; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick, maybe:
|
||
const noProxy = process.env.no_proxy || process.env.NO_PROXY || ''; | ||
const excludedHosts = noProxy.split(','); | ||
|
||
// proxy if the hostname is not in noProxy | ||
const shouldProxy = (excludedHosts.indexOf(hostname) === -1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also: might need to handle whitespace in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done the first. For the second: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works for me, just make it super-clear in the docs :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, saw https://github.com/Rob--W/proxy-from-env. If it makes our lives easier we could just fork it and bring it into the codebase and make the necessary changes. It also has a good test suite we can use as a starting point: https://github.com/Rob--W/proxy-from-env/blob/master/test.js |
||
|
||
if (shouldProxy) { | ||
logger.log(`Use proxy to download plugin.`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
logger.log(`Hint: you can add ${hostname} to the no_proxy environment variable, ` | ||
+ `to exclude that host from proxying.`); | ||
return new HttpProxyAgent(httpProxy); | ||
} else { | ||
logger.log(`Found exception for host ${hostname} in no_proxy environment variable. ` | ||
+ `Skipping proxy.`); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function sendRequest({ sourceUrl, timeout }, logger) { | ||
const maxRedirects = 11; //Because this one goes to 11. | ||
return fn(cb => { | ||
const req = Wreck.request('GET', sourceUrl, { timeout, redirects: maxRedirects }, (err, resp) => { | ||
const reqOptions = { timeout, redirects: maxRedirects }; | ||
const proxyAgent = getProxyAgent(sourceUrl, logger); | ||
|
||
if (proxyAgent) { | ||
reqOptions.agent = proxyAgent; | ||
} | ||
|
||
const req = Wreck.request('GET', sourceUrl, reqOptions, (err, resp) => { | ||
if (err) { | ||
if (err.code === 'ECONNREFUSED') { | ||
err = new Error('ENOTFOUND'); | ||
|
@@ -50,7 +86,7 @@ Responsible for managing http transfers | |
*/ | ||
export default async function downloadUrl(logger, sourceUrl, targetPath, timeout) { | ||
try { | ||
const { req, resp } = await sendRequest({ sourceUrl, timeout }); | ||
const { req, resp } = await sendRequest({ sourceUrl, timeout }, logger); | ||
|
||
try { | ||
const totalSize = parseFloat(resp.headers['content-length']) || 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: Maybe extract a
getEnv
: