codekingpro/portable-devtools
115k
1"use strict";
2var __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}));
13var __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});
18var __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};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.HttpsProxyAgent = void 0;
30const net = __importStar(require("net"));
31const tls = __importStar(require("tls"));
32const assert_1 = __importDefault(require("assert"));
33const debug_1 = __importDefault(require("debug"));
34const agent_base_1 = require("agent-base");
35const url_1 = require("url");
36const parse_proxy_response_1 = require("./parse-proxy-response");
37const debug = (0, debug_1.default)('https-proxy-agent');
38const setServernameFromNonIpHost = (options) => {
39 if (options.servername === undefined &&
40 options.host &&
41 !net.isIP(options.host)) {
42 return {
43 ...options,
44 servername: options.host,
45 };
46 }
47 return options;
48};
49/**
50 * The `HttpsProxyAgent` implements an HTTP Agent subclass that connects to
51 * the specified "HTTP(s) proxy server" in order to proxy HTTPS requests.
52 *
53 * Outgoing HTTP requests are first tunneled through the proxy server using the
54 * `CONNECT` HTTP request method to establish a connection to the proxy server,
55 * and then the proxy server connects to the destination target and issues the
56 * HTTP request from the proxy server.
57 *
58 * `https:` requests have their socket connection upgraded to TLS once
59 * the connection to the proxy server has been established.
60 */
61class HttpsProxyAgent extends agent_base_1.Agent {
62 constructor(proxy, opts) {
63 super(opts);
64 this.options = { path: undefined };
65 this.proxy = typeof proxy === 'string' ? new url_1.URL(proxy) : proxy;
66 this.proxyHeaders = opts?.headers ?? {};
67 debug('Creating new HttpsProxyAgent instance: %o', this.proxy.href);
68 // Trim off the brackets from IPv6 addresses
69 const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, '');
70 const port = this.proxy.port
71 ? parseInt(this.proxy.port, 10)
72 : this.proxy.protocol === 'https:'
73 ? 443
74 : 80;
75 this.connectOpts = {
76 // Attempt to negotiate http/1.1 for proxy servers that support http/2
77 ALPNProtocols: ['http/1.1'],
78 ...(opts ? omit(opts, 'headers') : null),
79 host,
80 port,
81 };
82 }
83 /**
84 * Called when the node-core HTTP client library is creating a
85 * new HTTP request.
86 */
87 async connect(req, opts) {
88 const { proxy } = this;
89 if (!opts.host) {
90 throw new TypeError('No "host" provided');
91 }
92 // Create a socket connection to the proxy server.
93 let socket;
94 if (proxy.protocol === 'https:') {
95 debug('Creating `tls.Socket`: %o', this.connectOpts);
96 socket = tls.connect(setServernameFromNonIpHost(this.connectOpts));
97 }
98 else {
99 debug('Creating `net.Socket`: %o', this.connectOpts);
100 socket = net.connect(this.connectOpts);
101 }
102 const headers = typeof this.proxyHeaders === 'function'
103 ? this.proxyHeaders()
104 : { ...this.proxyHeaders };
105 const host = net.isIPv6(opts.host) ? `[${opts.host}]` : opts.host;
106 let payload = `CONNECT ${host}:${opts.port} HTTP/1.1\r\n`;
107 // Inject the `Proxy-Authorization` header if necessary.
108 if (proxy.username || proxy.password) {
109 const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`;
110 headers['Proxy-Authorization'] = `Basic ${Buffer.from(auth).toString('base64')}`;
111 }
112 headers.Host = `${host}:${opts.port}`;
113 if (!headers['Proxy-Connection']) {
114 headers['Proxy-Connection'] = this.keepAlive
115 ? 'Keep-Alive'
116 : 'close';
117 }
118 for (const name of Object.keys(headers)) {
119 payload += `${name}: ${headers[name]}\r\n`;
120 }
121 const proxyResponsePromise = (0, parse_proxy_response_1.parseProxyResponse)(socket);
122 socket.write(`${payload}\r\n`);
123 const { connect, buffered } = await proxyResponsePromise;
124 req.emit('proxyConnect', connect);
125 this.emit('proxyConnect', connect, req);
126 if (connect.statusCode === 200) {
127 req.once('socket', resume);
128 if (opts.secureEndpoint) {
129 // The proxy is connecting to a TLS server, so upgrade
130 // this socket connection to a TLS connection.
131 debug('Upgrading socket connection to TLS');
132 return tls.connect({
133 ...omit(setServernameFromNonIpHost(opts), 'host', 'path', 'port'),
134 socket,
135 });
136 }
137 return socket;
138 }
139 // Some other status code that's not 200... need to re-play the HTTP
140 // header "data" events onto the socket once the HTTP machinery is
141 // attached so that the node core `http` can parse and handle the
142 // error status code.
143 // Close the original socket, and a new "fake" socket is returned
144 // instead, so that the proxy doesn't get the HTTP request
145 // written to it (which may contain `Authorization` headers or other
146 // sensitive data).
147 //
148 // See: https://hackerone.com/reports/541502
149 socket.destroy();
150 const fakeSocket = new net.Socket({ writable: false });
151 fakeSocket.readable = true;
152 // Need to wait for the "socket" event to re-play the "data" events.
153 req.once('socket', (s) => {
154 debug('Replaying proxy buffer for failed request');
155 (0, assert_1.default)(s.listenerCount('data') > 0);
156 // Replay the "buffered" Buffer onto the fake `socket`, since at
157 // this point the HTTP module machinery has been hooked up for
158 // the user.
159 s.push(buffered);
160 s.push(null);
161 });
162 return fakeSocket;
163 }
164}
165HttpsProxyAgent.protocols = ['http', 'https'];
166exports.HttpsProxyAgent = HttpsProxyAgent;
167function resume(socket) {
168 socket.resume();
169}
170function omit(obj, ...keys) {
171 const ret = {};
172 let key;
173 for (key in obj) {
174 if (!keys.includes(key)) {
175 ret[key] = obj[key];
176 }
177 }
178 return ret;
179}
180//# sourceMappingURL=index.js.map