fix bugs related no_proxy and ports: 1) https://foo.com should match no_proxy=foo.com:443 and 2) https://foo.com:8080 should match no_proxy=foo.com

This commit is contained in:
eric sciple
2020-01-22 23:09:35 -05:00
parent 9e8142913f
commit d745dd984c
4 changed files with 138 additions and 61 deletions

View File

@@ -1,30 +1,13 @@
import * as url from 'url';
export function getProxyUrl(reqUrl: url.Url): url.Url {
export function getProxyUrl(reqUrl: url.Url): url.Url | undefined {
let usingSsl = reqUrl.protocol === 'https:';
let noProxy: string = process.env["no_proxy"] ||
process.env["NO_PROXY"];
let bypass: boolean;
if (noProxy && typeof noProxy === 'string') {
let bypassList = noProxy.split(',');
for (let i=0; i < bypassList.length; i++) {
let item = bypassList[i];
if (item &&
typeof item === "string" &&
reqUrl.host.toLocaleLowerCase() == item.trim().toLocaleLowerCase()) {
bypass = true;
break;
}
}
}
let proxyUrl: url.Url;
if (bypass) {
if (checkBypass(reqUrl)) {
return proxyUrl;
}
let proxyVar: string;
if (usingSsl) {
proxyVar = process.env["https_proxy"] ||
@@ -40,4 +23,43 @@ export function getProxyUrl(reqUrl: url.Url): url.Url {
}
return proxyUrl;
}
export function checkBypass(reqUrl: url.Url): boolean {
if (!reqUrl.hostname) {
return false
}
let noProxy = process.env["no_proxy"] || process.env["NO_PROXY"] || '';
if (!noProxy) {
return false
}
// Determine the request port
let reqPort: number
if (reqUrl.port) {
reqPort = Number(reqUrl.port)
}
else if (reqUrl.protocol === 'http:') {
reqPort = 80
}
else if (reqUrl.protocol === 'https:') {
reqPort = 443
}
// Format the request hostname and hostname with port
let upperReqHosts = [reqUrl.hostname.toUpperCase()]
if (typeof reqPort === 'number') {
upperReqHosts.push(`${upperReqHosts[0]}:${reqPort}`)
}
// Compare request host against noproxy
for (let upperNoProxyItem of noProxy.split(',').map(x => x.trim().toUpperCase()).filter(x => x)) {
if (upperReqHosts.some(x => x === upperNoProxyItem)) {
return true
}
}
return false
}