proxy.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var url = require('url')
  2. var tunnel = require('tunnel-agent')
  3. var noop = Object.assign({
  4. http: function () {},
  5. silly: function () {}
  6. }, require('noop-logger'))
  7. function applyProxy (reqOpts, opts) {
  8. var log = opts.log || noop
  9. var proxy = opts['https-proxy'] || opts.proxy
  10. if (proxy) {
  11. // eslint-disable-next-line node/no-deprecated-api
  12. var parsedDownloadUrl = url.parse(reqOpts.url)
  13. // eslint-disable-next-line node/no-deprecated-api
  14. var parsedProxy = url.parse(proxy)
  15. var uriProtocol = (parsedDownloadUrl.protocol === 'https:' ? 'https' : 'http')
  16. var proxyProtocol = (parsedProxy.protocol === 'https:' ? 'Https' : 'Http')
  17. var tunnelFnName = [uriProtocol, proxyProtocol].join('Over')
  18. reqOpts.agent = tunnel[tunnelFnName]({
  19. proxy: {
  20. host: parsedProxy.hostname,
  21. port: +parsedProxy.port,
  22. proxyAuth: parsedProxy.auth
  23. }
  24. })
  25. log.http('request', 'Proxy setup detected (Host: ' +
  26. parsedProxy.hostname + ', Port: ' +
  27. parsedProxy.port + ', Authentication: ' +
  28. (parsedProxy.auth ? 'Yes' : 'No') + ')' +
  29. ' Tunneling with ' + tunnelFnName)
  30. }
  31. return reqOpts
  32. }
  33. module.exports = applyProxy