* Called when the node-core HTTP client library is creating a * new HTTP request.
(req, opts)
| 26256 | * new HTTP request. |
| 26257 | */ |
| 26258 | async connect(req, opts) { |
| 26259 | const { proxy } = this; |
| 26260 | if (!opts.host) { |
| 26261 | throw new TypeError('No "host" provided'); |
| 26262 | } |
| 26263 | let socket; |
| 26264 | if (proxy.protocol === "https:") { |
| 26265 | debug5("Creating `tls.Socket`: %o", this.connectOpts); |
| 26266 | socket = tls2.connect(setServernameFromNonIpHost2(this.connectOpts)); |
| 26267 | } else { |
| 26268 | debug5("Creating `net.Socket`: %o", this.connectOpts); |
| 26269 | socket = net3.connect(this.connectOpts); |
| 26270 | } |
| 26271 | const headers = typeof this.proxyHeaders === "function" ? this.proxyHeaders() : { ...this.proxyHeaders }; |
| 26272 | const host = net3.isIPv6(opts.host) ? `[${opts.host}]` : opts.host; |
| 26273 | let payload = `CONNECT ${host}:${opts.port} HTTP/1.1\r |
| 26274 | `; |
| 26275 | if (proxy.username || proxy.password) { |
| 26276 | const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`; |
| 26277 | headers["Proxy-Authorization"] = `Basic ${Buffer.from(auth).toString("base64")}`; |
| 26278 | } |
| 26279 | headers.Host = `${host}:${opts.port}`; |
| 26280 | if (!headers["Proxy-Connection"]) { |
| 26281 | headers["Proxy-Connection"] = this.keepAlive ? "Keep-Alive" : "close"; |
| 26282 | } |
| 26283 | for (const name of Object.keys(headers)) { |
| 26284 | payload += `${name}: ${headers[name]}\r |
| 26285 | `; |
| 26286 | } |
| 26287 | const proxyResponsePromise = (0, parse_proxy_response_1.parseProxyResponse)(socket); |
| 26288 | socket.write(`${payload}\r |
| 26289 | `); |
| 26290 | const { connect: connect3, buffered } = await proxyResponsePromise; |
| 26291 | req.emit("proxyConnect", connect3); |
| 26292 | this.emit("proxyConnect", connect3, req); |
| 26293 | if (connect3.statusCode === 200) { |
| 26294 | req.once("socket", resume2); |
| 26295 | if (opts.secureEndpoint) { |
| 26296 | debug5("Upgrading socket connection to TLS"); |
| 26297 | return tls2.connect({ |
| 26298 | ...omit2(setServernameFromNonIpHost2(opts), "host", "path", "port"), |
| 26299 | socket |
| 26300 | }); |
| 26301 | } |
| 26302 | return socket; |
| 26303 | } |
| 26304 | socket.destroy(); |
| 26305 | const fakeSocket = new net3.Socket({ writable: false }); |
| 26306 | fakeSocket.readable = true; |
| 26307 | req.once("socket", (s2) => { |
| 26308 | debug5("Replaying proxy buffer for failed request"); |
| 26309 | (0, assert_1.default)(s2.listenerCount("data") > 0); |
| 26310 | s2.push(buffered); |
| 26311 | s2.push(null); |
| 26312 | }); |
| 26313 | return fakeSocket; |
| 26314 | } |
| 26315 | }; |
no test coverage detected