Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NO_PROXY ignored by .net core 3.0 on Windows

We are trying to get proxy functionality to work in a .NET Core 3.0 (or 3.1) app on Windows. What we find is that the HTTPS_PROXY and HTTP_PROXY variables are recognized and processed as expected, but the default proxy does not appear to honor the NO_PROXY variable. Here is sample code:

 HttpClient client = new HttpClient();
 IWebProxy myProxy = HttpClient.DefaultProxy;
 Uri target = new Uri(args[0]);
 Uri proxy = myProxy.GetProxy(target);
 Console.WriteLine($"Target proxy for {target} is {proxy} {myProxy.IsBypassed(target)}");

If I run this from PowerShell as such:

if (-not $env:path.Contains('c:\data\src')) {
    $env:path = "$env:Path;C:\data\src\Local\DefaultProxy\bin\Debug\netcoreapp3.0"
}
$env:HTTPS_PROXY = 'http://10.11.10.10'
$env:NO_PROXY = '*'
DefaultProxy https://www.contoso.com

I get:

Target proxy for https://www.contoso.com/ is http://10.11.10.10/ False

I expect to get blank and true.

What should I change so that I can use NO_PROXY to exclude domains from the default proxy?

like image 235
Prof Von Lemongargle Avatar asked Dec 17 '22 12:12

Prof Von Lemongargle


2 Answers

I pulled the code for System.Net.Http.HttpEnvironmentProxy in .NET Core 3.1 (and in latest main development branch) and read through it, mainly through the IsMatchInBypassList method. There is no support for wildcards like asterisk (*) in NO_PROXY, hence this will not work at all. Also, the call to GetProxy completely ignores the NO_PROXY values.

To achieve the "wildcard" behavior, domains in the NO_PROXY list need to be prefixed with a dot (.), which is standard, but not particularly well documented. I can use this information to change my interpretation of the documentation and get better results.

So, if I modify the PowerShell code as follows:

$env:HTTPS_PROXY = 'http://10.11.10.10'
$env:NO_PROXY = '.contoso.com'
DefaultProxy https://www.contoso.com

Then I get:

Target proxy for https://www.contoso.com/ is http://10.11.10.10/ True

Presumably the DefaultProxy logic will use the IsBypassed result of true to stop proxy on the NO_PROXY list.

like image 101
Prof Von Lemongargle Avatar answered Dec 28 '22 08:12

Prof Von Lemongargle


there is never a "standard" in the no_proxy. In Linux, most applications recognized it as lower_case. Sure, in Windows, it is always case-insensitive. But when it comes to wildcard matching or regex matching, there is no standard at all. Each application works its own way as it see fit, for no_proxy.

like image 38
Ben L Avatar answered Dec 28 '22 06:12

Ben L