123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- var {
- _nullishCoalesce,
- _optionalChain
- } = require('@sentry/utils');
- Object.defineProperty(exports, '__esModule', { value: true });
- const assert = require('assert');
- const net = require('net');
- const tls = require('tls');
- const url = require('url');
- const utils = require('@sentry/utils');
- const base = require('./base.js');
- const parseProxyResponse = require('./parse-proxy-response.js');
- function debug(...args) {
- utils.logger.log('[https-proxy-agent]', ...args);
- }
- class HttpsProxyAgent extends base.Agent {
- static __initStatic() {this.protocols = ['http', 'https']; }
- constructor(proxy, opts) {
- super(opts);
- this.options = {};
- this.proxy = typeof proxy === 'string' ? new url.URL(proxy) : proxy;
- this.proxyHeaders = _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _2 => _2.headers]), () => ( {}));
- debug('Creating new HttpsProxyAgent instance: %o', this.proxy.href);
-
- const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, '');
- const port = this.proxy.port ? parseInt(this.proxy.port, 10) : this.proxy.protocol === 'https:' ? 443 : 80;
- this.connectOpts = {
-
- ALPNProtocols: ['http/1.1'],
- ...(opts ? omit(opts, 'headers') : null),
- host,
- port,
- };
- }
-
- async connect(req, opts) {
- const { proxy } = this;
- if (!opts.host) {
- throw new TypeError('No "host" provided');
- }
-
- let socket;
- if (proxy.protocol === 'https:') {
- debug('Creating `tls.Socket`: %o', this.connectOpts);
- const servername = this.connectOpts.servername || this.connectOpts.host;
- socket = tls.connect({
- ...this.connectOpts,
- servername: servername && net.isIP(servername) ? undefined : servername,
- });
- } else {
- debug('Creating `net.Socket`: %o', this.connectOpts);
- socket = net.connect(this.connectOpts);
- }
- const headers =
- typeof this.proxyHeaders === 'function' ? this.proxyHeaders() : { ...this.proxyHeaders };
- const host = net.isIPv6(opts.host) ? `[${opts.host}]` : opts.host;
- let payload = `CONNECT ${host}:${opts.port} HTTP/1.1\r\n`;
-
- if (proxy.username || proxy.password) {
- const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`;
- headers['Proxy-Authorization'] = `Basic ${Buffer.from(auth).toString('base64')}`;
- }
- headers.Host = `${host}:${opts.port}`;
- if (!headers['Proxy-Connection']) {
- headers['Proxy-Connection'] = this.keepAlive ? 'Keep-Alive' : 'close';
- }
- for (const name of Object.keys(headers)) {
- payload += `${name}: ${headers[name]}\r\n`;
- }
- const proxyResponsePromise = parseProxyResponse.parseProxyResponse(socket);
- socket.write(`${payload}\r\n`);
- const { connect, buffered } = await proxyResponsePromise;
- req.emit('proxyConnect', connect);
-
-
- this.emit('proxyConnect', connect, req);
- if (connect.statusCode === 200) {
- req.once('socket', resume);
- if (opts.secureEndpoint) {
-
-
- debug('Upgrading socket connection to TLS');
- const servername = opts.servername || opts.host;
- return tls.connect({
- ...omit(opts, 'host', 'path', 'port'),
- socket,
- servername: net.isIP(servername) ? undefined : servername,
- });
- }
- return socket;
- }
-
-
-
-
-
-
-
-
-
-
- socket.destroy();
- const fakeSocket = new net.Socket({ writable: false });
- fakeSocket.readable = true;
-
- req.once('socket', (s) => {
- debug('Replaying proxy buffer for failed request');
- assert.default(s.listenerCount('data') > 0);
-
-
-
- s.push(buffered);
- s.push(null);
- });
- return fakeSocket;
- }
- }HttpsProxyAgent.__initStatic();
- function resume(socket) {
- socket.resume();
- }
- function omit(
- obj,
- ...keys
- )
- {
- const ret = {}
- ;
- let key;
- for (key in obj) {
- if (!keys.includes(key)) {
- ret[key] = obj[key];
- }
- }
- return ret;
- }
- exports.HttpsProxyAgent = HttpsProxyAgent;
|