agent.js 817 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. const url = require('url');
  3. const tunnelAgent = require('tunnel-agent');
  4. const is = require('./is');
  5. const proxies = [
  6. 'HTTPS_PROXY',
  7. 'https_proxy',
  8. 'HTTP_PROXY',
  9. 'http_proxy',
  10. 'npm_config_https_proxy',
  11. 'npm_config_proxy'
  12. ];
  13. function env (key) {
  14. return process.env[key];
  15. }
  16. module.exports = function () {
  17. try {
  18. const proxy = new url.URL(proxies.map(env).find(is.string));
  19. const tunnel = proxy.protocol === 'https:'
  20. ? tunnelAgent.httpsOverHttps
  21. : tunnelAgent.httpsOverHttp;
  22. const proxyAuth = proxy.username && proxy.password
  23. ? `${proxy.username}:${proxy.password}`
  24. : null;
  25. return tunnel({
  26. proxy: {
  27. port: Number(proxy.port),
  28. host: proxy.hostname,
  29. proxyAuth
  30. }
  31. });
  32. } catch (err) {
  33. return null;
  34. }
  35. };