-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Avishagp
committed
Aug 31, 2022
1 parent
e5dee92
commit fb02b3f
Showing
3 changed files
with
159 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
function get_env_var(keys, try_lower) { | ||
var val, i = -1, env = process.env; | ||
while (!val && i < keys.length-1) { | ||
val = env[keys[++i]]; | ||
if (!val && try_lower) { | ||
val = env[keys[i].toLowerCase()]; | ||
} | ||
} | ||
return val; | ||
} | ||
|
||
// returns false if a no_proxy host matches given url | ||
function should_proxy_to(uri) { | ||
const noProxy = get_env_var(["NO_PROXY"], true); | ||
if (!noProxy) { | ||
return true; | ||
} | ||
|
||
let urlMatchedNoProxyPattern = false | ||
const requestUrl = new URL(uri); | ||
const patternList = noProxy.split(/[\s,]+/); | ||
|
||
// iterate over all NO_PROXY patterns and determine whether the given URL matches any of them | ||
for (const pattern of patternList) { | ||
if(pattern.trim().length == 0) { | ||
continue | ||
} | ||
|
||
// replace leading dot by asterisk, escape dots and finally replace asterisk by .* | ||
const preparedPattern = pattern.replace(/^\./, "*").replace(/[.]/g, '\\$&').replace(/\*/g, '.*') | ||
const regex = new RegExp(preparedPattern) | ||
const matches = (uri.match(regex)?.length > 0); | ||
if (matches) { | ||
// hostname + port of the request URL match a given NO_PROXY pattern | ||
urlMatchedNoProxyPattern = true | ||
break; | ||
} | ||
} | ||
|
||
return !urlMatchedNoProxyPattern; | ||
} | ||
|
||
module.exports.should_proxy_to = should_proxy_to; | ||
module.exports.get_env_var = get_env_var; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters