index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.ProxyAgent = exports.proxies = void 0;
  30. const http = __importStar(require("http"));
  31. const https = __importStar(require("https"));
  32. const lru_cache_1 = __importDefault(require("lru-cache"));
  33. const agent_base_1 = require("agent-base");
  34. const debug_1 = __importDefault(require("debug"));
  35. const proxy_from_env_1 = require("proxy-from-env");
  36. const pac_proxy_agent_1 = require("pac-proxy-agent");
  37. const http_proxy_agent_1 = require("http-proxy-agent");
  38. const https_proxy_agent_1 = require("https-proxy-agent");
  39. const socks_proxy_agent_1 = require("socks-proxy-agent");
  40. const debug = (0, debug_1.default)('proxy-agent');
  41. const PROTOCOLS = [
  42. ...http_proxy_agent_1.HttpProxyAgent.protocols,
  43. ...socks_proxy_agent_1.SocksProxyAgent.protocols,
  44. ...pac_proxy_agent_1.PacProxyAgent.protocols,
  45. ];
  46. /**
  47. * Supported proxy types.
  48. */
  49. exports.proxies = {
  50. http: [http_proxy_agent_1.HttpProxyAgent, https_proxy_agent_1.HttpsProxyAgent],
  51. https: [http_proxy_agent_1.HttpProxyAgent, https_proxy_agent_1.HttpsProxyAgent],
  52. socks: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  53. socks4: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  54. socks4a: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  55. socks5: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  56. socks5h: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  57. 'pac+data': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  58. 'pac+file': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  59. 'pac+ftp': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  60. 'pac+http': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  61. 'pac+https': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  62. };
  63. function isValidProtocol(v) {
  64. return PROTOCOLS.includes(v);
  65. }
  66. /**
  67. * Uses the appropriate `Agent` subclass based off of the "proxy"
  68. * environment variables that are currently set.
  69. *
  70. * An LRU cache is used, to prevent unnecessary creation of proxy
  71. * `http.Agent` instances.
  72. */
  73. class ProxyAgent extends agent_base_1.Agent {
  74. constructor(opts) {
  75. super(opts);
  76. /**
  77. * Cache for `Agent` instances.
  78. */
  79. this.cache = new lru_cache_1.default({ max: 20 });
  80. debug('Creating new ProxyAgent instance: %o', opts);
  81. this.connectOpts = opts;
  82. this.httpAgent = opts?.httpAgent || new http.Agent(opts);
  83. this.httpsAgent =
  84. opts?.httpsAgent || new https.Agent(opts);
  85. this.getProxyForUrl = opts?.getProxyForUrl || proxy_from_env_1.getProxyForUrl;
  86. }
  87. async connect(req, opts) {
  88. const { secureEndpoint } = opts;
  89. const isWebSocket = req.getHeader('upgrade') === 'websocket';
  90. const protocol = secureEndpoint
  91. ? isWebSocket
  92. ? 'wss:'
  93. : 'https:'
  94. : isWebSocket
  95. ? 'ws:'
  96. : 'http:';
  97. const host = req.getHeader('host');
  98. const url = new URL(req.path, `${protocol}//${host}`).href;
  99. const proxy = this.getProxyForUrl(url);
  100. if (!proxy) {
  101. debug('Proxy not enabled for URL: %o', url);
  102. return secureEndpoint ? this.httpsAgent : this.httpAgent;
  103. }
  104. debug('Request URL: %o', url);
  105. debug('Proxy URL: %o', proxy);
  106. // attempt to get a cached `http.Agent` instance first
  107. const cacheKey = `${protocol}+${proxy}`;
  108. let agent = this.cache.get(cacheKey);
  109. if (!agent) {
  110. const proxyUrl = new URL(proxy);
  111. const proxyProto = proxyUrl.protocol.replace(':', '');
  112. if (!isValidProtocol(proxyProto)) {
  113. throw new Error(`Unsupported protocol for proxy URL: ${proxy}`);
  114. }
  115. const ctor = exports.proxies[proxyProto][secureEndpoint || isWebSocket ? 1 : 0];
  116. // @ts-expect-error meh…
  117. agent = new ctor(proxy, this.connectOpts);
  118. this.cache.set(cacheKey, agent);
  119. }
  120. else {
  121. debug('Cache hit for proxy URL: %o', proxy);
  122. }
  123. return agent;
  124. }
  125. destroy() {
  126. for (const agent of this.cache.values()) {
  127. agent.destroy();
  128. }
  129. super.destroy();
  130. }
  131. }
  132. exports.ProxyAgent = ProxyAgent;
  133. //# sourceMappingURL=index.js.map