private bool GetProxyForUrl(Uri url, out string?proxyHost, out string?bypass) { proxyHost = null; bypass = null; var handle = WinHttpOpen( IntPtr.Zero, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); try { var _proxyHelper = new WinInetProxyHelper(); if (_proxyHelper.GetProxyForUrl(handle, url, out WINHTTP_PROXY_INFO info)) { proxyHost = Marshal.PtrToStringAuto(info.Proxy); bypass = Marshal.PtrToStringAuto(info.ProxyBypass); return(true); } return(false); } finally { handle.Close(); } }
public void HttpProxy_WindowsProxy_PAC_Loaded(string rawProxyString, string rawInsecureUri, string rawSecureUri) { RemoteExecutor.Invoke((proxyString, insecureProxy, secureProxy) => { TestControl.ResetAll(); Assert.False(HttpWindowsProxy.TryCreate(out IWebProxy p)); FakeRegistry.WinInetProxySettings.AutoConfigUrl = "http://127.0.0.1/proxy.pac"; WinInetProxyHelper proxyHelper = new WinInetProxyHelper(); Assert.Null(proxyHelper.Proxy); Assert.Equal(FakeRegistry.WinInetProxySettings.AutoConfigUrl, proxyHelper.AutoConfigUrl); Assert.False(proxyHelper.ManualSettingsUsed); Assert.True(proxyHelper.AutoSettingsUsed); Assert.True(HttpWindowsProxy.TryCreate(out p)); Assert.NotNull(p); // With a HttpWindowsProxy created configured to use auto-config, now set Proxy so when it // attempts to resolve a proxy, it resolves our string. FakeRegistry.WinInetProxySettings.Proxy = proxyString; proxyHelper = new WinInetProxyHelper(); Assert.Equal(proxyString, proxyHelper.Proxy); Assert.Equal(!string.IsNullOrEmpty(insecureProxy) ? new Uri(insecureProxy) : null, p.GetProxy(new Uri(fooHttp))); Assert.Equal(!string.IsNullOrEmpty(secureProxy) ? new Uri(secureProxy) : null, p.GetProxy(new Uri(fooHttps))); Assert.Equal(!string.IsNullOrEmpty(insecureProxy) ? new Uri(insecureProxy) : null, p.GetProxy(new Uri(fooWs))); Assert.Equal(!string.IsNullOrEmpty(secureProxy) ? new Uri(secureProxy) : null, p.GetProxy(new Uri(fooWss))); }, rawProxyString, rawInsecureUri ?? string.Empty, rawSecureUri ?? string.Empty).Dispose(); }
public static bool TryCreate(out IWebProxy proxy) { // This will get basic proxy setting from system using existing // WinInetProxyHelper functions. If no proxy is enabled, it will return null. SafeWinHttpHandle sessionHandle = null; proxy = null; WinInetProxyHelper proxyHelper = new WinInetProxyHelper(); if (!proxyHelper.ManualSettingsOnly && !proxyHelper.AutoSettingsUsed) { return(false); } if (proxyHelper.AutoSettingsUsed) { sessionHandle = Interop.WinHttp.WinHttpOpen( IntPtr.Zero, Interop.WinHttp.WINHTTP_ACCESS_TYPE_NO_PROXY, Interop.WinHttp.WINHTTP_NO_PROXY_NAME, Interop.WinHttp.WINHTTP_NO_PROXY_BYPASS, (int)Interop.WinHttp.WINHTTP_FLAG_ASYNC); if (sessionHandle.IsInvalid) { // Proxy failures are currently ignored by managed handler. return(false); } } proxy = new HttpWindowsProxy(proxyHelper, sessionHandle); return(true); }
public void HttpProxy_WindowsProxy_Manual_Loaded(string rawProxyString, string rawInsecureUri, string rawSecureUri) { RemoteExecutor.Invoke((proxyString, insecureProxy, secureProxy) => { FakeRegistry.Reset(); Assert.False(HttpWindowsProxy.TryCreate(out IWebProxy p)); FakeRegistry.WinInetProxySettings.Proxy = proxyString; WinInetProxyHelper proxyHelper = new WinInetProxyHelper(); Assert.Null(proxyHelper.AutoConfigUrl); Assert.Equal(proxyString, proxyHelper.Proxy); Assert.False(proxyHelper.AutoSettingsUsed); Assert.True(proxyHelper.ManualSettingsUsed); Assert.True(HttpWindowsProxy.TryCreate(out p)); Assert.NotNull(p); Assert.Equal(!string.IsNullOrEmpty(insecureProxy) ? new Uri(insecureProxy) : null, p.GetProxy(new Uri(fooHttp))); Assert.Equal(!string.IsNullOrEmpty(secureProxy) ? new Uri(secureProxy) : null, p.GetProxy(new Uri(fooHttps))); Assert.Equal(!string.IsNullOrEmpty(insecureProxy) ? new Uri(insecureProxy) : null, p.GetProxy(new Uri(fooWs))); Assert.Equal(!string.IsNullOrEmpty(secureProxy) ? new Uri(secureProxy) : null, p.GetProxy(new Uri(fooWss))); return(RemoteExecutor.SuccessExitCode); }, rawProxyString, rawInsecureUri ?? string.Empty, rawSecureUri ?? string.Empty).Dispose(); }
public void HttpProxy_SystemProxy_Loaded() { IWebProxy p; FakeRegistry.Reset(); Assert.False(HttpSystemProxy.TryCreate(out p)); FakeRegistry.WinInetProxySettings.Proxy = FakeProxyString; WinInetProxyHelper proxyHelper = new WinInetProxyHelper(); Assert.True(HttpSystemProxy.TryCreate(out p)); Assert.NotNull(p); Assert.Equal(fakeProxyUri, p.GetProxy(fooHttp)); Assert.Equal(fakeProxyUri, p.GetProxy(fooHttps)); }
public void HttpProxy_WindowsProxy_Loaded(string rawProxyString, string expectedUri) { RemoteExecutor.Invoke((proxyString, expectedString) => { IWebProxy p; FakeRegistry.Reset(); FakeRegistry.WinInetProxySettings.Proxy = proxyString; WinInetProxyHelper proxyHelper = new WinInetProxyHelper(); Assert.True(HttpWindowsProxy.TryCreate(out p)); Assert.NotNull(p); Assert.Equal(expectedString, p.GetProxy(new Uri(fooHttp)).ToString()); Assert.Equal(expectedString, p.GetProxy(new Uri(fooHttps)).ToString()); }, rawProxyString, expectedUri).Dispose(); }
public void HttpProxy_InvalidWindowsProxy_Null(string rawProxyString) { RemoteExecutor.Invoke((proxyString) => { IWebProxy p; FakeRegistry.Reset(); Assert.False(HttpWindowsProxy.TryCreate(out p)); FakeRegistry.WinInetProxySettings.Proxy = proxyString; WinInetProxyHelper proxyHelper = new WinInetProxyHelper(); Assert.True(HttpWindowsProxy.TryCreate(out p)); Assert.NotNull(p); Assert.Null(p.GetProxy(new Uri(fooHttp))); Assert.Null(p.GetProxy(new Uri(fooHttps))); Assert.Null(p.GetProxy(new Uri(fooWs))); Assert.Null(p.GetProxy(new Uri(fooWss))); }, rawProxyString).Dispose(); }
public void HttpProxy_SystemProxy_Loaded(string rawProxyString, bool hasInsecureProxy, bool hasSecureProxy) { RemoteInvoke((proxyString, insecureProxy, secureProxy) => { IWebProxy p; FakeRegistry.Reset(); Assert.False(HttpSystemProxy.TryCreate(out p)); FakeRegistry.WinInetProxySettings.Proxy = proxyString; WinInetProxyHelper proxyHelper = new WinInetProxyHelper(); Assert.True(HttpSystemProxy.TryCreate(out p)); Assert.NotNull(p); Assert.Equal(Boolean.Parse(insecureProxy) ? new Uri(insecureProxyUri) : null, p.GetProxy(new Uri(fooHttp))); Assert.Equal(Boolean.Parse(secureProxy) ? new Uri(secureProxyUri) : null, p.GetProxy(new Uri(fooHttps))); Assert.Equal(Boolean.Parse(insecureProxy) ? new Uri(insecureProxyUri) : null, p.GetProxy(new Uri(fooWs))); Assert.Equal(Boolean.Parse(secureProxy) ? new Uri(secureProxyUri) : null, p.GetProxy(new Uri(fooWss))); return(SuccessExitCode); }, rawProxyString, hasInsecureProxy.ToString(), hasSecureProxy.ToString()).Dispose(); }
public void HttpProxy_InvalidSystemProxy_Null(string rawProxyString) { RemoteInvoke((proxyString) => { IWebProxy p; FakeRegistry.Reset(); Assert.False(HttpSystemProxy.TryCreate(out p)); FakeRegistry.WinInetProxySettings.Proxy = proxyString; WinInetProxyHelper proxyHelper = new WinInetProxyHelper(); Assert.True(HttpSystemProxy.TryCreate(out p)); Assert.NotNull(p); Assert.Equal(null, p.GetProxy(new Uri(fooHttp))); Assert.Equal(null, p.GetProxy(new Uri(fooHttps))); Assert.Equal(null, p.GetProxy(new Uri(fooWs))); Assert.Equal(null, p.GetProxy(new Uri(fooWss))); return(SuccessExitCode); }, rawProxyString).Dispose(); }
private HttpWindowsProxy(WinInetProxyHelper proxyHelper, SafeWinHttpHandle sessionHandle) { _proxyHelper = proxyHelper; _sessionHandle = sessionHandle; if (proxyHelper.ManualSettingsUsed) { ParseProxyConfig(proxyHelper.Proxy, out _insecureProxyUri, out _secureProxyUri); if (_insecureProxyUri == null && _secureProxyUri == null) { // If advanced parsing by protocol fails, fall-back to simplified parsing. _insecureProxyUri = _secureProxyUri = GetUriFromString(proxyHelper.Proxy); } if (!string.IsNullOrWhiteSpace(proxyHelper.ProxyBypass)) { int idx = 0; int start = 0; string tmp; // Process bypass list for manual setting. // Initial list size is best guess based on string length assuming each entry is at least 5 characters on average. _bypass = new List <Regex>(proxyHelper.ProxyBypass.Length / 5); while (idx < proxyHelper.ProxyBypass.Length) { // Strip leading spaces and scheme if any. while (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] == ' ') { idx += 1; } ; if (string.Compare(proxyHelper.ProxyBypass, idx, "http://", 0, 7, StringComparison.OrdinalIgnoreCase) == 0) { idx += 7; } else if (string.Compare(proxyHelper.ProxyBypass, idx, "https://", 0, 8, StringComparison.OrdinalIgnoreCase) == 0) { idx += 8; } if (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] == '[') { // Strip [] from IPv6 so we can use IdnHost laster for matching. idx += 1; } start = idx; while (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] != ' ' && proxyHelper.ProxyBypass[idx] != ';' && proxyHelper.ProxyBypass[idx] != ']') { idx += 1; } ; if (idx == start) { // Empty string. tmp = null; } else if (string.Compare(proxyHelper.ProxyBypass, start, "<local>", 0, 7, StringComparison.OrdinalIgnoreCase) == 0) { _bypassLocal = true; tmp = null; } else { tmp = proxyHelper.ProxyBypass.Substring(start, idx - start); } // Skip trailing characters if any. if (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] != ';') { // Got stopped at space or ']'. Strip until next ';' or end. while (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] != ';') { idx += 1; } ; } if (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] == ';') { idx++; } if (tmp == null) { continue; } try { // Escape any special characters and unescape * to get wildcard pattern match. Regex re = new Regex(Regex.Escape(tmp).Replace("\\*", ".*?") + "$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); _bypass.Add(re); } catch (Exception ex) { } } if (_bypass.Count == 0) { // Bypass string only had garbage we did not parse. _bypass = null; } } if (_bypassLocal) { _localIp = new List <IPAddress>(); foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) { IPInterfaceProperties ipProps = netInterface.GetIPProperties(); foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) { _localIp.Add(addr.Address); } } } } }