Skip to content

Commit

Permalink
Fixed default system proxy to handle null credentials
Browse files Browse the repository at this point in the history
Also fixed the code to check if the targetUri is bypassed.

Thanks to @jwonagel for these fixes.

Fixes pr #1852
  • Loading branch information
jstedfast committed Dec 2, 2024
1 parent 95e7da1 commit 5af38db
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions MailKit/Net/Proxy/WebProxyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ static Uri GetTargetUri (string host, int port)

static NetworkCredential GetNetworkCredential (ICredentials credentials, Uri uri)
{
if (credentials == null)
return null;

if (credentials is NetworkCredential network)
return network;

Expand All @@ -88,11 +91,19 @@ static ProxyClient GetProxyClient (Uri proxyUri, ICredentials credentials)
{
var credential = GetNetworkCredential (credentials, proxyUri);

if (proxyUri.Scheme.Equals ("https", StringComparison.OrdinalIgnoreCase))
return new HttpsProxyClient (proxyUri.Host, proxyUri.Port, credential);
if (proxyUri.Scheme.Equals ("https", StringComparison.OrdinalIgnoreCase)) {
if (credential != null)
return new HttpsProxyClient (proxyUri.Host, proxyUri.Port, credential);

return new HttpsProxyClient (proxyUri.Host, proxyUri.Port);
}

if (proxyUri.Scheme.Equals ("http", StringComparison.OrdinalIgnoreCase)) {
if (credential != null)
return new HttpProxyClient (proxyUri.Host, proxyUri.Port, credential);

if (proxyUri.Scheme.Equals ("http", StringComparison.OrdinalIgnoreCase))
return new HttpProxyClient (proxyUri.Host, proxyUri.Port, credential);
return new HttpProxyClient (proxyUri.Host, proxyUri.Port);
}

throw new NotImplementedException ($"The default system proxy does not support {proxyUri.Scheme}.");
}
Expand Down Expand Up @@ -132,7 +143,7 @@ public override Stream Connect (string host, int port, CancellationToken cancell
var targetUri = GetTargetUri (host, port);
var proxyUri = proxy.GetProxy (targetUri);

if (proxyUri is null) {
if (proxyUri is null || proxy.IsBypassed (targetUri)) {
// Note: if the proxy URI is null, then it means that the proxy should be bypassed.
var socket = SocketUtils.Connect (host, port, LocalEndPoint, cancellationToken);
return new NetworkStream (socket, true);
Expand Down

0 comments on commit 5af38db

Please sign in to comment.