index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 url_1 = require("url");
  33. const lru_cache_1 = __importDefault(require("lru-cache"));
  34. const agent_base_1 = require("agent-base");
  35. const debug_1 = __importDefault(require("debug"));
  36. const proxy_from_env_1 = require("proxy-from-env");
  37. const pac_proxy_agent_1 = require("pac-proxy-agent");
  38. const http_proxy_agent_1 = require("http-proxy-agent");
  39. const https_proxy_agent_1 = require("https-proxy-agent");
  40. const socks_proxy_agent_1 = require("socks-proxy-agent");
  41. const debug = (0, debug_1.default)('proxy-agent');
  42. const PROTOCOLS = [
  43. ...http_proxy_agent_1.HttpProxyAgent.protocols,
  44. ...socks_proxy_agent_1.SocksProxyAgent.protocols,
  45. ...pac_proxy_agent_1.PacProxyAgent.protocols,
  46. ];
  47. /**
  48. * Supported proxy types.
  49. */
  50. exports.proxies = {
  51. http: [http_proxy_agent_1.HttpProxyAgent, https_proxy_agent_1.HttpsProxyAgent],
  52. https: [http_proxy_agent_1.HttpProxyAgent, https_proxy_agent_1.HttpsProxyAgent],
  53. socks: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  54. socks4: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  55. socks4a: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  56. socks5: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  57. socks5h: [socks_proxy_agent_1.SocksProxyAgent, socks_proxy_agent_1.SocksProxyAgent],
  58. 'pac+data': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  59. 'pac+file': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  60. 'pac+ftp': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  61. 'pac+http': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  62. 'pac+https': [pac_proxy_agent_1.PacProxyAgent, pac_proxy_agent_1.PacProxyAgent],
  63. };
  64. function isValidProtocol(v) {
  65. return PROTOCOLS.includes(v);
  66. }
  67. /**
  68. * Uses the appropriate `Agent` subclass based off of the "proxy"
  69. * environment variables that are currently set.
  70. *
  71. * An LRU cache is used, to prevent unnecessary creation of proxy
  72. * `http.Agent` instances.
  73. */
  74. class ProxyAgent extends agent_base_1.Agent {
  75. constructor(opts) {
  76. super(opts);
  77. /**
  78. * Cache for `Agent` instances.
  79. */
  80. this.cache = new lru_cache_1.default({ max: 20 });
  81. debug('Creating new ProxyAgent instance: %o', opts);
  82. this.connectOpts = opts;
  83. this.httpAgent = opts?.httpAgent || new http.Agent(opts);
  84. this.httpsAgent =
  85. opts?.httpsAgent || new https.Agent(opts);
  86. this.getProxyForUrl = opts?.getProxyForUrl || proxy_from_env_1.getProxyForUrl;
  87. }
  88. async connect(req, opts) {
  89. const { secureEndpoint } = opts;
  90. const isWebSocket = req.getHeader('upgrade') === 'websocket';
  91. const protocol = secureEndpoint
  92. ? isWebSocket
  93. ? 'wss:'
  94. : 'https:'
  95. : isWebSocket
  96. ? 'ws:'
  97. : 'http:';
  98. const host = req.getHeader('host');
  99. const url = new url_1.URL(req.path, `${protocol}//${host}`).href;
  100. const proxy = await this.getProxyForUrl(url);
  101. if (!proxy) {
  102. debug('Proxy not enabled for URL: %o', url);
  103. return secureEndpoint ? this.httpsAgent : this.httpAgent;
  104. }
  105. debug('Request URL: %o', url);
  106. debug('Proxy URL: %o', proxy);
  107. // attempt to get a cached `http.Agent` instance first
  108. const cacheKey = `${protocol}+${proxy}`;
  109. let agent = this.cache.get(cacheKey);
  110. if (!agent) {
  111. const proxyUrl = new url_1.URL(proxy);
  112. const proxyProto = proxyUrl.protocol.replace(':', '');
  113. if (!isValidProtocol(proxyProto)) {
  114. throw new Error(`Unsupported protocol for proxy URL: ${proxy}`);
  115. }
  116. const ctor = exports.proxies[proxyProto][secureEndpoint || isWebSocket ? 1 : 0];
  117. // @ts-expect-error meh…
  118. agent = new ctor(proxy, this.connectOpts);
  119. this.cache.set(cacheKey, agent);
  120. }
  121. else {
  122. debug('Cache hit for proxy URL: %o', proxy);
  123. }
  124. return agent;
  125. }
  126. destroy() {
  127. for (const agent of this.cache.values()) {
  128. agent.destroy();
  129. }
  130. super.destroy();
  131. }
  132. }
  133. exports.ProxyAgent = ProxyAgent;
  134. //# sourceMappingURL=index.js.map