| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Concurrent; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using System.Net.NetworkInformation; |
| | | 10 | | using System.Runtime.InteropServices; |
| | | 11 | | using System.Text; |
| | | 12 | | using System.Threading; |
| | | 13 | | using Microsoft.Win32; |
| | | 14 | | using SafeWinHttpHandle = Interop.WinHttp.SafeWinHttpHandle; |
| | | 15 | | |
| | | 16 | | namespace System.Net.Http |
| | | 17 | | { |
| | | 18 | | internal sealed class HttpWindowsProxy : IMultiWebProxy, IDisposable |
| | | 19 | | { |
| | 0 | 20 | | private readonly RegistryKey? _internetSettingsRegistry = Registry.CurrentUser?.OpenSubKey("Software\\Microsoft\ |
| | | 21 | | private MultiProxy _insecureProxy; // URI of the http system proxy if set |
| | | 22 | | private MultiProxy _secureProxy; // URI of the https system proxy if set |
| | 0 | 23 | | private FailedProxyCache _failedProxies = new FailedProxyCache(); |
| | | 24 | | private List<string>? _bypass; // list of domains not to proxy |
| | | 25 | | private List<IPAddress>? _localIp; |
| | | 26 | | private ICredentials? _credentials; |
| | | 27 | | private WinInetProxyHelper _proxyHelper; |
| | | 28 | | private SafeWinHttpHandle? _sessionHandle; |
| | | 29 | | private bool _disposed; |
| | 0 | 30 | | private EventWaitHandle _waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset); |
| | | 31 | | private const int RegistrationFlags = Interop.Advapi32.REG_NOTIFY_CHANGE_NAME | Interop.Advapi32.REG_NOTIFY_CHAN |
| | | 32 | | private RegisteredWaitHandle? _registeredWaitHandle; |
| | | 33 | | |
| | | 34 | | // 'proxy' used from tests via Reflection |
| | 0 | 35 | | public HttpWindowsProxy(WinInetProxyHelper? proxy = null) |
| | 0 | 36 | | { |
| | | 37 | | |
| | 0 | 38 | | if (_internetSettingsRegistry != null && proxy == null) |
| | 0 | 39 | | { |
| | | 40 | | // we register for change notifications so we can react to changes during lifetime. |
| | 0 | 41 | | if (Interop.Advapi32.RegNotifyChangeKeyValue(_internetSettingsRegistry.Handle, true, RegistrationFlags, |
| | 0 | 42 | | { |
| | 0 | 43 | | _registeredWaitHandle = ThreadPool.RegisterWaitForSingleObject(_waitHandle, RegistryChangeNotificati |
| | 0 | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | UpdateConfiguration(proxy); |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | private static void RegistryChangeNotificationCallback(object? state, bool timedOut) |
| | 0 | 51 | | { |
| | 0 | 52 | | HttpWindowsProxy proxy = (HttpWindowsProxy)state!; |
| | 0 | 53 | | if (!proxy._disposed) |
| | 0 | 54 | | { |
| | | 55 | | |
| | | 56 | | // This is executed from threadpool. we should not ever throw here. |
| | | 57 | | try |
| | 0 | 58 | | { |
| | | 59 | | // We need to register for notification every time. We regisrerand lock before we process configurat |
| | | 60 | | // so if there is update it would be serialized to ensure consistency. |
| | 0 | 61 | | Interop.Advapi32.RegNotifyChangeKeyValue(proxy._internetSettingsRegistry!.Handle, true, Registration |
| | 0 | 62 | | lock (proxy) |
| | 0 | 63 | | { |
| | 0 | 64 | | proxy.UpdateConfiguration(); |
| | 0 | 65 | | } |
| | 0 | 66 | | } |
| | 0 | 67 | | catch (Exception ex) |
| | 0 | 68 | | { |
| | 0 | 69 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(proxy, $"Failed to refresh proxy configurat |
| | 0 | 70 | | } |
| | 0 | 71 | | } |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | [MemberNotNull(nameof(_proxyHelper))] |
| | | 75 | | private void UpdateConfiguration(WinInetProxyHelper? proxyHelper = null) |
| | 0 | 76 | | { |
| | | 77 | | |
| | 0 | 78 | | proxyHelper ??= new WinInetProxyHelper(); |
| | | 79 | | |
| | 0 | 80 | | if (proxyHelper.AutoSettingsUsed) |
| | 0 | 81 | | { |
| | 0 | 82 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(proxyHelper, $"AutoSettingsUsed, calling {nameof |
| | 0 | 83 | | SafeWinHttpHandle? sessionHandle = Interop.WinHttp.WinHttpOpen( |
| | 0 | 84 | | IntPtr.Zero, |
| | 0 | 85 | | Interop.WinHttp.WINHTTP_ACCESS_TYPE_NO_PROXY, |
| | 0 | 86 | | Interop.WinHttp.WINHTTP_NO_PROXY_NAME, |
| | 0 | 87 | | Interop.WinHttp.WINHTTP_NO_PROXY_BYPASS, |
| | 0 | 88 | | (int)Interop.WinHttp.WINHTTP_FLAG_ASYNC); |
| | | 89 | | |
| | 0 | 90 | | if (sessionHandle.IsInvalid) |
| | 0 | 91 | | { |
| | | 92 | | // Proxy failures are currently ignored by managed handler. |
| | 0 | 93 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(proxyHelper, $"{nameof(Interop.WinHttp.WinH |
| | 0 | 94 | | sessionHandle.Dispose(); |
| | 0 | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | _sessionHandle = sessionHandle; |
| | 0 | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | if (proxyHelper.ManualSettingsUsed) |
| | 0 | 101 | | { |
| | 0 | 102 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(proxyHelper, $"ManualSettingsUsed, {proxyHelper. |
| | | 103 | | |
| | 0 | 104 | | _secureProxy = MultiProxy.ParseManualSettings(_failedProxies, proxyHelper.Proxy, true); |
| | 0 | 105 | | _insecureProxy = MultiProxy.ParseManualSettings(_failedProxies, proxyHelper.Proxy, false); |
| | | 106 | | |
| | 0 | 107 | | if (!string.IsNullOrWhiteSpace(proxyHelper.ProxyBypass)) |
| | 0 | 108 | | { |
| | 0 | 109 | | int idx = 0; |
| | | 110 | | string? tmp; |
| | 0 | 111 | | bool bypassLocal = false; |
| | 0 | 112 | | List<IPAddress>? localIp = null; |
| | | 113 | | |
| | | 114 | | // Process bypass list for manual setting. |
| | | 115 | | // Initial list size is best guess based on string length assuming each entry is at least 5 characte |
| | 0 | 116 | | List<string>? bypass = new List<string>(proxyHelper.ProxyBypass.Length / 5); |
| | | 117 | | |
| | 0 | 118 | | while (idx < proxyHelper.ProxyBypass.Length) |
| | 0 | 119 | | { |
| | | 120 | | // Strip leading spaces and scheme if any. |
| | 0 | 121 | | while (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] == ' ') { idx += 1; |
| | 0 | 122 | | if (string.Compare(proxyHelper.ProxyBypass, idx, "http://", 0, 7, StringComparison.OrdinalIgnore |
| | 0 | 123 | | { |
| | 0 | 124 | | idx += 7; |
| | 0 | 125 | | } |
| | 0 | 126 | | else if (string.Compare(proxyHelper.ProxyBypass, idx, "https://", 0, 8, StringComparison.Ordinal |
| | 0 | 127 | | { |
| | 0 | 128 | | idx += 8; |
| | 0 | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | if (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] == '[') |
| | 0 | 132 | | { |
| | | 133 | | // Strip [] from IPv6 so we can use IdnHost laster for matching. |
| | 0 | 134 | | idx += 1; |
| | 0 | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | int start = idx; |
| | 0 | 138 | | while (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] != ' ' && proxyHelpe |
| | | 139 | | |
| | 0 | 140 | | if (idx == start) |
| | 0 | 141 | | { |
| | | 142 | | // Empty string. |
| | 0 | 143 | | tmp = null; |
| | 0 | 144 | | } |
| | 0 | 145 | | else if (string.Compare(proxyHelper.ProxyBypass, start, "<local>", 0, 7, StringComparison.Ordina |
| | 0 | 146 | | { |
| | 0 | 147 | | bypassLocal = true; |
| | 0 | 148 | | tmp = null; |
| | 0 | 149 | | } |
| | | 150 | | else |
| | 0 | 151 | | { |
| | 0 | 152 | | tmp = proxyHelper.ProxyBypass.Substring(start, idx - start); |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | // Skip trailing characters if any. |
| | 0 | 156 | | if (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] != ';') |
| | 0 | 157 | | { |
| | | 158 | | // Got stopped at space or ']'. Strip until next ';' or end. |
| | 0 | 159 | | while (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] != ';') { idx += |
| | 0 | 160 | | } |
| | 0 | 161 | | if (idx < proxyHelper.ProxyBypass.Length && proxyHelper.ProxyBypass[idx] == ';') |
| | 0 | 162 | | { |
| | 0 | 163 | | idx++; |
| | 0 | 164 | | } |
| | 0 | 165 | | if (tmp == null) |
| | 0 | 166 | | { |
| | 0 | 167 | | continue; |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | bypass.Add(tmp); |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | _bypass = bypass.Count > 0 ? bypass : null; |
| | | 174 | | |
| | 0 | 175 | | if (bypassLocal) |
| | 0 | 176 | | { |
| | 0 | 177 | | localIp = new List<IPAddress>(); |
| | 0 | 178 | | foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) |
| | 0 | 179 | | { |
| | 0 | 180 | | IPInterfaceProperties ipProps = netInterface.GetIPProperties(); |
| | 0 | 181 | | foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) |
| | 0 | 182 | | { |
| | 0 | 183 | | localIp.Add(addr.Address); |
| | 0 | 184 | | } |
| | 0 | 185 | | } |
| | 0 | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | _localIp = localIp?.Count > 0 ? localIp : null; |
| | 0 | 189 | | } |
| | 0 | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | _proxyHelper = proxyHelper; |
| | 0 | 193 | | } |
| | | 194 | | |
| | | 195 | | public void Dispose() |
| | 0 | 196 | | { |
| | 0 | 197 | | if (!_disposed) |
| | 0 | 198 | | { |
| | 0 | 199 | | _disposed = true; |
| | | 200 | | |
| | 0 | 201 | | if (_sessionHandle != null && !_sessionHandle.IsInvalid) |
| | 0 | 202 | | { |
| | 0 | 203 | | SafeWinHttpHandle.DisposeAndClearHandle(ref _sessionHandle); |
| | 0 | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | _waitHandle?.Dispose(); |
| | 0 | 207 | | _internetSettingsRegistry?.Dispose(); |
| | 0 | 208 | | _registeredWaitHandle?.Unregister(null); |
| | 0 | 209 | | } |
| | 0 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Gets the proxy URI. (IWebProxy interface) |
| | | 214 | | /// </summary> |
| | | 215 | | public Uri? GetProxy(Uri uri) |
| | 0 | 216 | | { |
| | 0 | 217 | | if (!_proxyHelper.AutoSettingsUsed && !_proxyHelper.ManualSettingsOnly) |
| | 0 | 218 | | { |
| | 0 | 219 | | return null; |
| | | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | GetMultiProxy(uri).ReadNext(out Uri? proxyUri, out _); |
| | 0 | 223 | | return proxyUri; |
| | 0 | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Gets the proxy URIs. |
| | | 228 | | /// </summary> |
| | | 229 | | public MultiProxy GetMultiProxy(Uri uri) |
| | 0 | 230 | | { |
| | | 231 | | // We need WinHTTP to detect and/or process a PAC (JavaScript) file. This maps to |
| | | 232 | | // "Automatically detect settings" and/or "Use automatic configuration script" from IE |
| | | 233 | | // settings. But, calling into WinHTTP can be slow especially when it has to call into |
| | | 234 | | // the out-of-process service to discover, load, and run the PAC file. So, we skip |
| | | 235 | | // calling into WinHTTP if there was a recent failure to detect a PAC file on the network. |
| | | 236 | | // This is a common error. The default IE settings on a Windows machine consist of the |
| | | 237 | | // single checkbox for "Automatically detect settings" turned on and most networks |
| | | 238 | | // won't actually discover a PAC file on the network since WPAD protocol isn't configured. |
| | 0 | 239 | | if (_proxyHelper.AutoSettingsUsed && !_proxyHelper.RecentAutoDetectionFailure) |
| | 0 | 240 | | { |
| | 0 | 241 | | Interop.WinHttp.WINHTTP_PROXY_INFO proxyInfo = default; |
| | | 242 | | try |
| | 0 | 243 | | { |
| | 0 | 244 | | if (_proxyHelper.GetProxyForUrl(_sessionHandle, uri, out proxyInfo)) |
| | 0 | 245 | | { |
| | | 246 | | // If WinHTTP just specified a Proxy with no ProxyBypass list, then |
| | | 247 | | // we can return the Proxy uri directly. |
| | 0 | 248 | | if (proxyInfo.ProxyBypass == IntPtr.Zero) |
| | 0 | 249 | | { |
| | 0 | 250 | | if (proxyInfo.Proxy != IntPtr.Zero) |
| | 0 | 251 | | { |
| | 0 | 252 | | string proxyStr = Marshal.PtrToStringUni(proxyInfo.Proxy)!; |
| | | 253 | | |
| | 0 | 254 | | return MultiProxy.CreateLazy(_failedProxies, proxyStr, IsSecureUri(uri)); |
| | | 255 | | } |
| | | 256 | | else |
| | 0 | 257 | | { |
| | 0 | 258 | | return MultiProxy.Empty; |
| | | 259 | | } |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | // A bypass list was also specified. This means that WinHTTP has fallen back to |
| | | 263 | | // using the manual IE settings specified and there is a ProxyBypass list also. |
| | | 264 | | // Since we're not really using the full WinHTTP stack, we need to use HttpSystemProxy |
| | | 265 | | // to do the computation of the final proxy uri merging the information from the Proxy |
| | | 266 | | // and ProxyBypass strings. |
| | 0 | 267 | | } |
| | | 268 | | else |
| | 0 | 269 | | { |
| | 0 | 270 | | return MultiProxy.Empty; |
| | | 271 | | } |
| | 0 | 272 | | } |
| | | 273 | | finally |
| | 0 | 274 | | { |
| | 0 | 275 | | Marshal.FreeHGlobal(proxyInfo.Proxy); |
| | 0 | 276 | | Marshal.FreeHGlobal(proxyInfo.ProxyBypass); |
| | 0 | 277 | | } |
| | 0 | 278 | | } |
| | | 279 | | |
| | | 280 | | // Fallback to manual settings if present. |
| | 0 | 281 | | if (_proxyHelper.ManualSettingsUsed) |
| | 0 | 282 | | { |
| | 0 | 283 | | if (_localIp != null) |
| | 0 | 284 | | { |
| | | 285 | | IPAddress? address; |
| | | 286 | | |
| | 0 | 287 | | if (uri.IsLoopback) |
| | 0 | 288 | | { |
| | | 289 | | // This is optimization for loopback addresses. |
| | | 290 | | // Unfortunately this does not work for all local addresses. |
| | 0 | 291 | | return MultiProxy.Empty; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | // Pre-Check if host may be IP address to avoid parsing. |
| | 0 | 295 | | if (uri.HostNameType == UriHostNameType.IPv6 || uri.HostNameType == UriHostNameType.IPv4) |
| | 0 | 296 | | { |
| | | 297 | | // RFC1123 allows labels to start with number. |
| | | 298 | | // Leading number may or may not be IP address. |
| | | 299 | | // IPv6 [::1] notation. '[' is not valid character in names. |
| | 0 | 300 | | if (IPAddress.TryParse(uri.IdnHost, out address)) |
| | 0 | 301 | | { |
| | | 302 | | // Host is valid IP address. |
| | | 303 | | // Check if it belongs to local system. |
| | 0 | 304 | | foreach (IPAddress a in _localIp) |
| | 0 | 305 | | { |
| | 0 | 306 | | if (a.Equals(address)) |
| | 0 | 307 | | { |
| | 0 | 308 | | return MultiProxy.Empty; |
| | | 309 | | } |
| | 0 | 310 | | } |
| | 0 | 311 | | } |
| | 0 | 312 | | } |
| | 0 | 313 | | if (uri.HostNameType != UriHostNameType.IPv6 && !uri.IdnHost.Contains('.')) |
| | 0 | 314 | | { |
| | | 315 | | // Not address and does not have a dot. |
| | | 316 | | // Hosts without FQDN are considered local. |
| | 0 | 317 | | return MultiProxy.Empty; |
| | | 318 | | } |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | // Check if we have other rules for bypass. |
| | 0 | 322 | | if (_bypass != null) |
| | 0 | 323 | | { |
| | 0 | 324 | | foreach (string entry in _bypass) |
| | 0 | 325 | | { |
| | | 326 | | // IdnHost does not have []. |
| | 0 | 327 | | if (SimpleRegex.IsMatchWithStarWildcard(uri.IdnHost, entry)) |
| | 0 | 328 | | { |
| | 0 | 329 | | return MultiProxy.Empty; |
| | | 330 | | } |
| | 0 | 331 | | } |
| | 0 | 332 | | } |
| | | 333 | | |
| | | 334 | | // We did not find match on bypass list. |
| | 0 | 335 | | return IsSecureUri(uri) ? _secureProxy : _insecureProxy; |
| | | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | return MultiProxy.Empty; |
| | 0 | 339 | | } |
| | | 340 | | |
| | | 341 | | private static bool IsSecureUri(Uri uri) |
| | 0 | 342 | | { |
| | 0 | 343 | | return uri.Scheme == UriScheme.Https || uri.Scheme == UriScheme.Wss; |
| | 0 | 344 | | } |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// Checks if URI is subject to proxy or not. |
| | | 348 | | /// </summary> |
| | | 349 | | public bool IsBypassed(Uri uri) |
| | 0 | 350 | | { |
| | | 351 | | // This HttpSystemProxy class is only consumed by SocketsHttpHandler and is not exposed outside of |
| | | 352 | | // SocketsHttpHandler. The current pattern for consumption of IWebProxy is to call IsBypassed first. |
| | | 353 | | // If it returns false, then the caller will call GetProxy. For this proxy implementation, computing |
| | | 354 | | // the return value for IsBypassed is as costly as calling GetProxy. We want to avoid doing extra |
| | | 355 | | // work. So, this proxy implementation for the IsBypassed method can always return false. Then the |
| | | 356 | | // GetProxy method will return non-null for a proxy, or null if no proxy should be used. |
| | 0 | 357 | | return false; |
| | 0 | 358 | | } |
| | | 359 | | |
| | | 360 | | public ICredentials? Credentials |
| | | 361 | | { |
| | | 362 | | get |
| | 0 | 363 | | { |
| | 0 | 364 | | return _credentials; |
| | 0 | 365 | | } |
| | | 366 | | set |
| | 0 | 367 | | { |
| | 0 | 368 | | _credentials = value; |
| | 0 | 369 | | } |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | // Access function for unit tests. |
| | | 373 | | internal List<string>? BypassList => _bypass; |
| | | 374 | | } |
| | | 375 | | } |