diff options
Diffstat (limited to 'includes/external/addressbook/node_modules/http2-wrapper/source/proxies')
7 files changed, 259 insertions, 0 deletions
diff --git a/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/get-auth-headers.js b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/get-auth-headers.js new file mode 100644 index 0000000..364a858 --- /dev/null +++ b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/get-auth-headers.js @@ -0,0 +1,17 @@ +'use strict'; + +module.exports = self => { + const {username, password} = self.proxyOptions.url; + + if (username || password) { + const data = `${username}:${password}`; + const authorization = `Basic ${Buffer.from(data).toString('base64')}`; + + return { + 'proxy-authorization': authorization, + authorization + }; + } + + return {}; +}; diff --git a/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h1-over-h2.js b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h1-over-h2.js new file mode 100644 index 0000000..15a4f78 --- /dev/null +++ b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h1-over-h2.js @@ -0,0 +1,90 @@ +'use strict'; +const tls = require('tls'); +const http = require('http'); +const https = require('https'); +const JSStreamSocket = require('../utils/js-stream-socket.js'); +const {globalAgent} = require('../agent.js'); +const UnexpectedStatusCodeError = require('./unexpected-status-code-error.js'); +const initialize = require('./initialize.js'); +const getAuthorizationHeaders = require('./get-auth-headers.js'); + +const createConnection = (self, options, callback) => { + (async () => { + try { + const {proxyOptions} = self; + const {url, headers, raw} = proxyOptions; + + const stream = await globalAgent.request(url, proxyOptions, { + ...getAuthorizationHeaders(self), + ...headers, + ':method': 'CONNECT', + ':authority': `${options.host}:${options.port}` + }); + + stream.once('error', callback); + stream.once('response', headers => { + const statusCode = headers[':status']; + + if (statusCode !== 200) { + callback(new UnexpectedStatusCodeError(statusCode, '')); + return; + } + + const encrypted = self instanceof https.Agent; + + if (raw && encrypted) { + options.socket = stream; + const secureStream = tls.connect(options); + + secureStream.once('close', () => { + stream.destroy(); + }); + + callback(null, secureStream); + return; + } + + const socket = new JSStreamSocket(stream); + socket.encrypted = false; + socket._handle.getpeername = out => { + out.family = undefined; + out.address = undefined; + out.port = undefined; + }; + + callback(null, socket); + }); + } catch (error) { + callback(error); + } + })(); +}; + +class HttpOverHttp2 extends http.Agent { + constructor(options) { + super(options); + + initialize(this, options.proxyOptions); + } + + createConnection(options, callback) { + createConnection(this, options, callback); + } +} + +class HttpsOverHttp2 extends https.Agent { + constructor(options) { + super(options); + + initialize(this, options.proxyOptions); + } + + createConnection(options, callback) { + createConnection(this, options, callback); + } +} + +module.exports = { + HttpOverHttp2, + HttpsOverHttp2 +}; diff --git a/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h2-over-h1.js b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h2-over-h1.js new file mode 100644 index 0000000..8764f07 --- /dev/null +++ b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h2-over-h1.js @@ -0,0 +1,48 @@ +'use strict'; +const http = require('http'); +const https = require('https'); +const Http2OverHttpX = require('./h2-over-hx.js'); +const getAuthorizationHeaders = require('./get-auth-headers.js'); + +const getStream = request => new Promise((resolve, reject) => { + const onConnect = (response, socket, head) => { + socket.unshift(head); + + request.off('error', reject); + resolve([socket, response.statusCode, response.statusMessage]); + }; + + request.once('error', reject); + request.once('connect', onConnect); +}); + +class Http2OverHttp extends Http2OverHttpX { + async _getProxyStream(authority) { + const {proxyOptions} = this; + const {url, headers} = this.proxyOptions; + + const network = url.protocol === 'https:' ? https : http; + + // `new URL('https://localhost/httpbin.org:443')` results in + // a `/httpbin.org:443` path, which has an invalid leading slash. + const request = network.request({ + ...proxyOptions, + hostname: url.hostname, + port: url.port, + path: authority, + headers: { + ...getAuthorizationHeaders(this), + ...headers, + host: authority + }, + method: 'CONNECT' + }).end(); + + return getStream(request); + } +} + +module.exports = { + Http2OverHttp, + Http2OverHttps: Http2OverHttp +}; diff --git a/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h2-over-h2.js b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h2-over-h2.js new file mode 100644 index 0000000..414c5e9 --- /dev/null +++ b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h2-over-h2.js @@ -0,0 +1,32 @@ +'use strict'; +const {globalAgent} = require('../agent.js'); +const Http2OverHttpX = require('./h2-over-hx.js'); +const getAuthorizationHeaders = require('./get-auth-headers.js'); + +const getStatusCode = stream => new Promise((resolve, reject) => { + stream.once('error', reject); + stream.once('response', headers => { + stream.off('error', reject); + resolve(headers[':status']); + }); +}); + +class Http2OverHttp2 extends Http2OverHttpX { + async _getProxyStream(authority) { + const {proxyOptions} = this; + + const headers = { + ...getAuthorizationHeaders(this), + ...proxyOptions.headers, + ':method': 'CONNECT', + ':authority': authority + }; + + const stream = await globalAgent.request(proxyOptions.url, proxyOptions, headers); + const statusCode = await getStatusCode(stream); + + return [stream, statusCode, '']; + } +} + +module.exports = Http2OverHttp2; diff --git a/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h2-over-hx.js b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h2-over-hx.js new file mode 100644 index 0000000..0f5a104 --- /dev/null +++ b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/h2-over-hx.js @@ -0,0 +1,40 @@ +'use strict'; +const {Agent} = require('../agent.js'); +const JSStreamSocket = require('../utils/js-stream-socket.js'); +const UnexpectedStatusCodeError = require('./unexpected-status-code-error.js'); +const initialize = require('./initialize.js'); + +class Http2OverHttpX extends Agent { + constructor(options) { + super(options); + + initialize(this, options.proxyOptions); + } + + async createConnection(origin, options) { + const authority = `${origin.hostname}:${origin.port || 443}`; + + const [stream, statusCode, statusMessage] = await this._getProxyStream(authority); + if (statusCode !== 200) { + throw new UnexpectedStatusCodeError(statusCode, statusMessage); + } + + if (this.proxyOptions.raw) { + options.socket = stream; + } else { + const socket = new JSStreamSocket(stream); + socket.encrypted = false; + socket._handle.getpeername = out => { + out.family = undefined; + out.address = undefined; + out.port = undefined; + }; + + return socket; + } + + return super.createConnection(origin, options); + } +} + +module.exports = Http2OverHttpX; diff --git a/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/initialize.js b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/initialize.js new file mode 100644 index 0000000..e4c5889 --- /dev/null +++ b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/initialize.js @@ -0,0 +1,21 @@ +'use strict'; +// See https://github.com/facebook/jest/issues/2549 +// eslint-disable-next-line node/prefer-global/url +const {URL} = require('url'); +const checkType = require('../utils/check-type.js'); + +module.exports = (self, proxyOptions) => { + checkType('proxyOptions', proxyOptions, ['object']); + checkType('proxyOptions.headers', proxyOptions.headers, ['object', 'undefined']); + checkType('proxyOptions.raw', proxyOptions.raw, ['boolean', 'undefined']); + checkType('proxyOptions.url', proxyOptions.url, [URL, 'string']); + + const url = new URL(proxyOptions.url); + + self.proxyOptions = { + raw: true, + ...proxyOptions, + headers: {...proxyOptions.headers}, + url + }; +}; diff --git a/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/unexpected-status-code-error.js b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/unexpected-status-code-error.js new file mode 100644 index 0000000..c7f0216 --- /dev/null +++ b/includes/external/addressbook/node_modules/http2-wrapper/source/proxies/unexpected-status-code-error.js @@ -0,0 +1,11 @@ +'use strict'; + +class UnexpectedStatusCodeError extends Error { + constructor(statusCode, statusMessage = '') { + super(`The proxy server rejected the request with status code ${statusCode} (${statusMessage || 'empty status message'})`); + this.statusCode = statusCode; + this.statusMessage = statusMessage; + } +} + +module.exports = UnexpectedStatusCodeError; |