proxy.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const url = require("url");
  4. function getProxyUrl(reqUrl) {
  5. let usingSsl = reqUrl.protocol === 'https:';
  6. let proxyUrl;
  7. if (checkBypass(reqUrl)) {
  8. return proxyUrl;
  9. }
  10. let proxyVar;
  11. if (usingSsl) {
  12. proxyVar = process.env["https_proxy"] ||
  13. process.env["HTTPS_PROXY"];
  14. }
  15. else {
  16. proxyVar = process.env["http_proxy"] ||
  17. process.env["HTTP_PROXY"];
  18. }
  19. if (proxyVar) {
  20. proxyUrl = url.parse(proxyVar);
  21. }
  22. return proxyUrl;
  23. }
  24. exports.getProxyUrl = getProxyUrl;
  25. function checkBypass(reqUrl) {
  26. if (!reqUrl.hostname) {
  27. return false;
  28. }
  29. let noProxy = process.env["no_proxy"] || process.env["NO_PROXY"] || '';
  30. if (!noProxy) {
  31. return false;
  32. }
  33. // Determine the request port
  34. let reqPort;
  35. if (reqUrl.port) {
  36. reqPort = Number(reqUrl.port);
  37. }
  38. else if (reqUrl.protocol === 'http:') {
  39. reqPort = 80;
  40. }
  41. else if (reqUrl.protocol === 'https:') {
  42. reqPort = 443;
  43. }
  44. // Format the request hostname and hostname with port
  45. let upperReqHosts = [reqUrl.hostname.toUpperCase()];
  46. if (typeof reqPort === 'number') {
  47. upperReqHosts.push(`${upperReqHosts[0]}:${reqPort}`);
  48. }
  49. // Compare request host against noproxy
  50. for (let upperNoProxyItem of noProxy.split(',').map(x => x.trim().toUpperCase()).filter(x => x)) {
  51. if (upperReqHosts.some(x => x === upperNoProxyItem)) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. exports.checkBypass = checkBypass;