This commit is contained in:
2025-05-12 05:38:44 +09:00
parent dced21c3f8
commit 6d78bfa46e
8120 changed files with 1161564 additions and 0 deletions

View File

@ -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 {};
};

View File

@ -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
};

View File

@ -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
};

View File

@ -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;

View File

@ -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;

View File

@ -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
};
};

View File

@ -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;