Last active
September 25, 2019 09:53
-
-
Save abodalevsky/a8935b4110fc4e91d5ed46227c7ee1ae to your computer and use it in GitHub Desktop.
Detects Proxy settings URI, UserName, Password
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
using System; | |
using System.Net; | |
namespace Helpers | |
{ | |
/// <summary> | |
/// Checks proxy settings for given URI. | |
/// </summary> | |
internal class ProxyHelper | |
{ | |
private Uri proxyAddress; | |
/// <summary> | |
/// Indicates weither proxy is discovered for given URI | |
/// </summary> | |
public bool IsBypassed { get; private set; } = true; | |
/// <summary> | |
/// Returns proxy address in case if proxy server was discovered | |
/// Throws exception if <see cref="IsBypassed"/> set to false. | |
/// </summary> | |
public string ProxyAddress => proxyAddress.AbsoluteUri; | |
/// <summary> | |
/// Gets host of proxy server. | |
/// Throws exception if <see cref="IsBypassed"/> set to false. | |
/// </summary> | |
public string Host => proxyAddress.Host; | |
/// <summary> | |
/// Gets port of proxy server. | |
/// Throws exception if <see cref="IsBypassed"/> set to false. | |
/// </summary> | |
public int Port => proxyAddress.Port; | |
/// <summary> | |
/// Returns User name form credentials cache, if any | |
/// </summary> | |
public string UserName { get; set; } = string.Empty; | |
/// <summary> | |
/// Returns User password form credentials cache, if any | |
/// </summary> | |
public string Password { get; private set; } = string.Empty; | |
/// <summary> | |
/// Extracts proxy and credential info from network configuration | |
/// </summary> | |
/// <param name="address"></param> | |
public ProxyHelper(Uri address) | |
{ | |
var normalizedAddress = GetNormalizedUri(address); | |
var proxy = WebRequest.GetSystemWebProxy(); | |
proxy.Credentials = CredentialCache.DefaultNetworkCredentials; | |
IsBypassed = proxy.IsBypassed(normalizedAddress); | |
if (!IsBypassed) | |
{ | |
proxyAddress = proxy.GetProxy(normalizedAddress); | |
var defCreds = CredentialCache.DefaultCredentials as NetworkCredential; | |
if (defCreds == null) | |
{ | |
return; | |
} | |
UserName = string.IsNullOrEmpty(defCreds.Domain) ? defCreds.UserName : $"{defCreds.Domain}\\{defCreds.UserName}"; | |
Password = defCreds.Password; | |
} | |
} | |
/// <summary> | |
/// Normalizes URI | |
/// we get wss:// (or ws://) scheme that is not all proxies handle correctly. | |
/// For better autodiscovering replace wss:// to https:// (ws:// to http:// accordingly) | |
/// </summary> | |
/// <param name="wsAddress"></param> | |
/// <returns></returns> | |
public static Uri GetNormalizedUri(Uri wsAddress) | |
{ | |
return new UriBuilder(wsAddress.Scheme == "wss" ? Uri.UriSchemeHttps : Uri.UriSchemeHttp, wsAddress.DnsSafeHost, wsAddress.Port).Uri; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment