Skip to content

Commit

Permalink
Handle list of hosts/domains in NO_PROXY (#1950)
Browse files Browse the repository at this point in the history
  • Loading branch information
poveden authored and tonyanziano committed Nov 1, 2019
1 parent da43a5c commit 0a0990e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
27 changes: 21 additions & 6 deletions packages/app/main/src/fetchProxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,27 @@ describe('fetch proxy support', () => {
expect(mockFetchArgs.init.agent).not.toBeNull();
});

it('should not add the https-proxy-agent when the HTTPS_PROXY env var exists but the NO_PROXY omits the url', () => {
process.env.HTTPS_PROXY = 'https://proxy';
process.env.NO_PROXY = 'localhost';
fetch('https://localhost').catch();
expect(mockFetchArgs.init).toBeUndefined();
});
it.each([
['https://host1', true],
['https://xhost1x', false],
['https://another/host1', false],
['https://host.domain1', true],
['https://domain1', false],
['https://host.domain2', true],
['https://domain2', false],
])(
'should not add the https-proxy-agent when the HTTPS_PROXY env var exists but the NO_PROXY omits the url (%#)',
(url: string, direct: boolean) => {
process.env.HTTPS_PROXY = 'https://proxy';
process.env.NO_PROXY = 'host1,.domain1,*.domain2';
fetch(url).catch();
if (direct) {
expect(mockFetchArgs.init).toBeUndefined();
} else {
expect(mockFetchArgs.init).not.toBeUndefined();
}
}
);

it('should not add the http-proxy-agent when the HTTPS_PROXY is omitted', () => {
delete process.env.HTTPS_PROXY;
Expand Down
24 changes: 22 additions & 2 deletions packages/app/main/src/fetchProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,29 @@ declare function fetch(input: RequestInfo, init?: RequestInit): Promise<Response
}

// No Proxy
const url: string = typeof urlOrRequest === 'string' ? urlOrRequest : urlOrRequest.url;

if (!process.env.HTTPS_PROXY || (process.env.NO_PROXY && url.includes(process.env.NO_PROXY))) {
if (!process.env.HTTPS_PROXY) {
return nodeFetch(...args);
}

const url: URL = new URL(typeof urlOrRequest === 'string' ? urlOrRequest : urlOrRequest.url);

// Reference: https://www.gnu.org/software/emacs/manual/html_node/url/Proxies.html
const noProxyList = (process.env.NO_PROXY || '')
.split(',')
.map(x =>
x
.trim()
.toLowerCase()
.replace(/^\*/, '')
)
.filter(x => x)
.map(name => ({
name,
isDomain: name.startsWith('.'),
}));

if (noProxyList.some(x => (x.isDomain ? url.hostname.endsWith(x.name) : url.hostname === x.name))) {
return nodeFetch(...args);
}

Expand Down

0 comments on commit 0a0990e

Please sign in to comment.