proxy.js 1.1 KB

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