Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ export function createProxyResolver(params: ProxyAgentParams) {
}
}

function getCacheKey(url: nodeurl.UrlWithStringQuery) {
function getCacheKey(url: nodeurl.URL) {
// Expecting proxies to usually be the same per scheme://host:port. Assuming that for performance.
return nodeurl.format({ ...url, ...{ pathname: undefined, search: undefined, hash: undefined } });
return `${url.protocol}//${url.host}`;
}
function getCachedProxy(key: string) {
checkAndFlushCacheIfNetworkChanged();
Expand Down Expand Up @@ -346,10 +346,10 @@ export function createProxyResolver(params: ProxyAgentParams) {
}

function useProxySettings(url: string, req: http.ClientRequest | undefined, stackText: string, callback: (proxy: string | undefined, source: ProxyResolveSource) => void) {
const parsedUrl = nodeurl.parse(url); // Coming from Node's URL, sticking with that.
const parsedUrl = new nodeurl.URL(url);

const hostname = parsedUrl.hostname;
if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' || hostname === '::ffff:127.0.0.1') {
const hostname = parsedUrl.hostname.replace(/^\[(.*)\]$/, '$1'); // WHATWG URL keeps the brackets around IPv6 addresses.
if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' || hostname === '::ffff:7f00:1') {
localhostCount++;
callback('DIRECT', 'localhost');
log.debug('ProxyResolver#resolveProxy localhost', url, 'DIRECT', stackText);
Expand Down
46 changes: 46 additions & 0 deletions tests/src/resolveProxyByURL.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as assert from 'assert';
import { spawnSync } from 'child_process';
import * as path from 'path';
import { createProxyResolver, getOrLoadAdditionalCertificates, loadSystemCertificates, Log, LogLevel, ProxyAgentParams, resetCaches } from '../../src';

function createParams(overrides: Partial<ProxyAgentParams>): ProxyAgentParams {
Expand Down Expand Up @@ -70,6 +72,50 @@ describe('resolveProxyByURL', function () {
});
});

it('reports IPv6 loopback addresses as a direct connection', async function () {
const { resolveProxyByURL } = createProxyResolver(createParams({
getProxyURL: () => 'http://proxy.example.com:8080',
}));
const direct = { url: undefined, type: 'DIRECT', source: 'localhost', result: 'DIRECT' };
assert.deepStrictEqual([
await resolveProxyByURL('http://[::1]:3000/'),
await resolveProxyByURL('http://[::ffff:127.0.0.1]:3000/'),
], [direct, direct]);
});

it('caches system results per scheme, host and port', async function () {
const { resolveProxyByURL } = createProxyResolver(createParams({
resolveProxy: async url => `PROXY ${new URL(url).port || 'default'}.example.com:8080`,
}));
assert.deepStrictEqual([
(await resolveProxyByURL('https://example.com/a?q#h')).result,
(await resolveProxyByURL('https://example.com:8443/a')).result,
(await resolveProxyByURL('https://example.com/b')),
], [
'PROXY default.example.com:8080',
'PROXY 8443.example.com:8080',
{ url: 'http://default.example.com:8080', type: 'PROXY', source: 'system_cached', result: 'PROXY default.example.com:8080' },
]);
});

it('does not emit the url.parse() deprecation warning (DEP0169)', function () {
const script = `require('./src').createProxyResolver({ ${[
'getProxyURL: () => undefined',
'getNoProxyConfig: () => []',
'addCertificatesV1: () => false',
'loadSystemCertificatesFromNode: () => false',
'log: { debug() { }, error() { } }',
'isUseHostProxyEnabled: () => true',
'resolveProxy: async () => "DIRECT"',
'env: {}',
].join(', ')} }).resolveProxyByURL('https://example.com/');`;
const { status, stderr } = spawnSync(process.execPath, ['--throw-deprecation', '-r', 'ts-node/register', '-e', script], {
cwd: path.join(__dirname, '..', '..'),
encoding: 'utf8',
});
assert.deepStrictEqual({ status, stderr }, { status: 0, stderr: '' });
});

it('reports the http.proxy setting source', async function () {
const { resolveProxyByURL } = createProxyResolver(createParams({
getProxyURL: () => 'http://proxy.example.com:8080',
Expand Down