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 |
|
48 |
|
49 | exports.proxies = {
|
50 | http: http_proxy_agent_1.HttpProxyAgent,
|
51 | https: https_proxy_agent_1.HttpsProxyAgent,
|
52 | socks: socks_proxy_agent_1.SocksProxyAgent,
|
53 | socks4: socks_proxy_agent_1.SocksProxyAgent,
|
54 | socks4a: socks_proxy_agent_1.SocksProxyAgent,
|
55 | socks5: socks_proxy_agent_1.SocksProxyAgent,
|
56 | socks5h: socks_proxy_agent_1.SocksProxyAgent,
|
57 | 'pac-data': pac_proxy_agent_1.PacProxyAgent,
|
58 | 'pac-file': pac_proxy_agent_1.PacProxyAgent,
|
59 | 'pac-ftp': pac_proxy_agent_1.PacProxyAgent,
|
60 | 'pac-http': pac_proxy_agent_1.PacProxyAgent,
|
61 | 'pac-https': pac_proxy_agent_1.PacProxyAgent,
|
62 | };
|
63 | function isValidProtocol(v) {
|
64 | return PROTOCOLS.includes(v);
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | class ProxyAgent extends agent_base_1.Agent {
|
74 | constructor(opts) {
|
75 | super(opts);
|
76 | |
77 |
|
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 | }
|
86 | async connect(req, opts) {
|
87 | const protocol = opts.secureEndpoint ? 'https:' : 'http:';
|
88 | const host = req.getHeader('host');
|
89 | const url = new URL(req.path, `${protocol}//${host}`).href;
|
90 | const proxy = (0, proxy_from_env_1.getProxyForUrl)(url);
|
91 | if (!proxy) {
|
92 | debug('Proxy not enabled for URL: %o', url);
|
93 | return opts.secureEndpoint ? this.httpsAgent : this.httpAgent;
|
94 | }
|
95 | debug('Request URL: %o', url);
|
96 | debug('Proxy URL: %o', proxy);
|
97 |
|
98 | let agent = this.cache.get(proxy);
|
99 | if (!agent) {
|
100 | const proxyUrl = new URL(proxy);
|
101 | const proxyProto = proxyUrl.protocol.replace(':', '');
|
102 | if (!isValidProtocol(proxyProto)) {
|
103 | throw new Error(`Unsupported protocol for proxy URL: ${proxy}`);
|
104 | }
|
105 | const ctor = exports.proxies[proxyProto];
|
106 |
|
107 | agent = new ctor(proxy, this.connectOpts);
|
108 | this.cache.set(proxy, agent);
|
109 | }
|
110 | else {
|
111 | debug('Cache hit for proxy URL: %o', proxy);
|
112 | }
|
113 | return agent;
|
114 | }
|
115 | destroy() {
|
116 | for (const agent of this.cache.values()) {
|
117 | agent.destroy();
|
118 | }
|
119 | super.destroy();
|
120 | }
|
121 | }
|
122 | exports.ProxyAgent = ProxyAgent;
|
123 |
|
\ | No newline at end of file |