| | | 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; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | |
| | | 7 | | using SafeWinHttpHandle = Interop.WinHttp.SafeWinHttpHandle; |
| | | 8 | | |
| | | 9 | | namespace System.Net.Http |
| | | 10 | | { |
| | | 11 | | // This class is only used on OS versions where WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY |
| | | 12 | | // is not supported (i.e. before Win8.1/Win2K12R2) in the WinHttpOpen() function. |
| | | 13 | | internal sealed class WinInetProxyHelper |
| | | 14 | | { |
| | | 15 | | private const int RecentAutoDetectionInterval = 120_000; // 2 minutes in milliseconds. |
| | | 16 | | private readonly string? _autoConfigUrl, _proxy, _proxyBypass; |
| | | 17 | | private readonly bool _autoDetect; |
| | | 18 | | private readonly bool _useProxy; |
| | | 19 | | private bool _autoDetectionFailed; |
| | | 20 | | private int _lastTimeAutoDetectionFailed; // Environment.TickCount units (milliseconds). |
| | | 21 | | |
| | 0 | 22 | | public WinInetProxyHelper() |
| | 0 | 23 | | { |
| | 0 | 24 | | Interop.WinHttp.WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig = default; |
| | | 25 | | |
| | | 26 | | try |
| | 0 | 27 | | { |
| | 0 | 28 | | if (Interop.WinHttp.WinHttpGetIEProxyConfigForCurrentUser(out proxyConfig)) |
| | 0 | 29 | | { |
| | 0 | 30 | | _autoConfigUrl = Marshal.PtrToStringUni(proxyConfig.AutoConfigUrl)!; |
| | 0 | 31 | | _autoDetect = proxyConfig.AutoDetect != 0; |
| | 0 | 32 | | _proxy = Marshal.PtrToStringUni(proxyConfig.Proxy)!; |
| | 0 | 33 | | _proxyBypass = Marshal.PtrToStringUni(proxyConfig.ProxyBypass)!; |
| | | 34 | | |
| | 0 | 35 | | if (NetEventSource.Log.IsEnabled()) |
| | 0 | 36 | | { |
| | 0 | 37 | | NetEventSource.Info(this, $"AutoConfigUrl={AutoConfigUrl}, AutoDetect={AutoDetect}, Proxy={Proxy |
| | 0 | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | _useProxy = true; |
| | 0 | 41 | | } |
| | | 42 | | else |
| | 0 | 43 | | { |
| | | 44 | | // We match behavior of WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY and ignore errors. |
| | 0 | 45 | | int lastError = Marshal.GetLastWin32Error(); |
| | 0 | 46 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, $"error={lastError}"); |
| | 0 | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"_useProxy={_useProxy}"); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | finally |
| | 0 | 53 | | { |
| | | 54 | | // FreeHGlobal already checks for null pointer before freeing the memory. |
| | 0 | 55 | | Marshal.FreeHGlobal(proxyConfig.AutoConfigUrl); |
| | 0 | 56 | | Marshal.FreeHGlobal(proxyConfig.Proxy); |
| | 0 | 57 | | Marshal.FreeHGlobal(proxyConfig.ProxyBypass); |
| | 0 | 58 | | } |
| | 0 | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | public string? AutoConfigUrl => _autoConfigUrl; |
| | | 62 | | |
| | 0 | 63 | | public bool AutoDetect => _autoDetect; |
| | | 64 | | |
| | 0 | 65 | | public bool AutoSettingsUsed => AutoDetect || !string.IsNullOrEmpty(AutoConfigUrl); |
| | | 66 | | |
| | 0 | 67 | | public bool ManualSettingsUsed => !string.IsNullOrEmpty(Proxy); |
| | | 68 | | |
| | 0 | 69 | | public bool ManualSettingsOnly => !AutoSettingsUsed && ManualSettingsUsed; |
| | | 70 | | |
| | 0 | 71 | | public string? Proxy => _proxy; |
| | | 72 | | |
| | 0 | 73 | | public string? ProxyBypass => _proxyBypass; |
| | | 74 | | |
| | | 75 | | public bool RecentAutoDetectionFailure => |
| | 0 | 76 | | _autoDetectionFailed && |
| | 0 | 77 | | Environment.TickCount - _lastTimeAutoDetectionFailed <= RecentAutoDetectionInterval; |
| | | 78 | | |
| | | 79 | | public bool GetProxyForUrl( |
| | | 80 | | SafeWinHttpHandle? sessionHandle, |
| | | 81 | | Uri uri, |
| | | 82 | | out Interop.WinHttp.WINHTTP_PROXY_INFO proxyInfo) |
| | 0 | 83 | | { |
| | 0 | 84 | | proxyInfo.AccessType = Interop.WinHttp.WINHTTP_ACCESS_TYPE_NO_PROXY; |
| | 0 | 85 | | proxyInfo.Proxy = IntPtr.Zero; |
| | 0 | 86 | | proxyInfo.ProxyBypass = IntPtr.Zero; |
| | | 87 | | |
| | 0 | 88 | | if (!_useProxy) |
| | 0 | 89 | | { |
| | 0 | 90 | | return false; |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | bool useProxy = false; |
| | | 94 | | |
| | | 95 | | Interop.WinHttp.WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions; |
| | 0 | 96 | | autoProxyOptions.AutoConfigUrl = AutoConfigUrl; |
| | 0 | 97 | | autoProxyOptions.AutoDetectFlags = AutoDetect ? |
| | 0 | 98 | | (Interop.WinHttp.WINHTTP_AUTO_DETECT_TYPE_DHCP | Interop.WinHttp.WINHTTP_AUTO_DETECT_TYPE_DNS_A) : 0; |
| | 0 | 99 | | autoProxyOptions.AutoLoginIfChallenged = false; |
| | 0 | 100 | | autoProxyOptions.Flags = |
| | 0 | 101 | | (AutoDetect ? Interop.WinHttp.WINHTTP_AUTOPROXY_AUTO_DETECT : 0) | |
| | 0 | 102 | | (!string.IsNullOrEmpty(AutoConfigUrl) ? Interop.WinHttp.WINHTTP_AUTOPROXY_CONFIG_URL : 0); |
| | 0 | 103 | | autoProxyOptions.Reserved1 = IntPtr.Zero; |
| | 0 | 104 | | autoProxyOptions.Reserved2 = 0; |
| | | 105 | | |
| | | 106 | | // AutoProxy Cache. |
| | | 107 | | // https://learn.microsoft.com/windows/desktop/WinHttp/autoproxy-cache |
| | | 108 | | // If the out-of-process service is active when WinHttpGetProxyForUrl is called, the cached autoproxy |
| | | 109 | | // URL and script are available to the whole computer. However, if the out-of-process service is used, |
| | | 110 | | // and the fAutoLogonIfChallenged flag in the pAutoProxyOptions structure is true, then the autoproxy |
| | | 111 | | // URL and script are not cached. Therefore, calling WinHttpGetProxyForUrl with the fAutoLogonIfChallenged |
| | | 112 | | // member set to TRUE results in additional overhead operations that may affect performance. |
| | | 113 | | // The following steps can be used to improve performance: |
| | | 114 | | // 1. Call WinHttpGetProxyForUrl with the fAutoLogonIfChallenged parameter set to false. The autoproxy |
| | | 115 | | // URL and script are cached for future calls to WinHttpGetProxyForUrl. |
| | | 116 | | // 2. If Step 1 fails, with ERROR_WINHTTP_LOGIN_FAILURE, then call WinHttpGetProxyForUrl with the |
| | | 117 | | // fAutoLogonIfChallenged member set to TRUE. |
| | | 118 | | // |
| | | 119 | | // We match behavior of WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY and ignore errors. |
| | | 120 | | |
| | | 121 | | #pragma warning disable CA1845 // file is shared with a build that lacks string.Concat for spans |
| | | 122 | | // Underlying code does not understand WebSockets so we need to convert it to http or https. |
| | 0 | 123 | | string destination = uri.AbsoluteUri; |
| | 0 | 124 | | if (uri.Scheme == UriScheme.Wss) |
| | 0 | 125 | | { |
| | 0 | 126 | | destination = UriScheme.Https + destination.Substring(UriScheme.Wss.Length); |
| | 0 | 127 | | } |
| | 0 | 128 | | else if (uri.Scheme == UriScheme.Ws) |
| | 0 | 129 | | { |
| | 0 | 130 | | destination = UriScheme.Http + destination.Substring(UriScheme.Ws.Length); |
| | 0 | 131 | | } |
| | | 132 | | #pragma warning restore CA1845 |
| | | 133 | | |
| | 0 | 134 | | var repeat = false; |
| | | 135 | | do |
| | 0 | 136 | | { |
| | 0 | 137 | | _autoDetectionFailed = false; |
| | 0 | 138 | | if (Interop.WinHttp.WinHttpGetProxyForUrl( |
| | 0 | 139 | | sessionHandle!, |
| | 0 | 140 | | destination, |
| | 0 | 141 | | ref autoProxyOptions, |
| | 0 | 142 | | out proxyInfo)) |
| | 0 | 143 | | { |
| | 0 | 144 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, "Using autoconfig proxy settings"); |
| | 0 | 145 | | useProxy = true; |
| | | 146 | | |
| | 0 | 147 | | break; |
| | | 148 | | } |
| | | 149 | | else |
| | 0 | 150 | | { |
| | 0 | 151 | | var lastError = Marshal.GetLastWin32Error(); |
| | 0 | 152 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, $"error={lastError}"); |
| | | 153 | | |
| | 0 | 154 | | if (lastError == Interop.WinHttp.ERROR_WINHTTP_LOGIN_FAILURE) |
| | 0 | 155 | | { |
| | 0 | 156 | | if (repeat) |
| | 0 | 157 | | { |
| | | 158 | | // We don't retry more than once. |
| | 0 | 159 | | break; |
| | | 160 | | } |
| | | 161 | | else |
| | 0 | 162 | | { |
| | 0 | 163 | | repeat = true; |
| | 0 | 164 | | autoProxyOptions.AutoLoginIfChallenged = true; |
| | 0 | 165 | | } |
| | 0 | 166 | | } |
| | | 167 | | else |
| | 0 | 168 | | { |
| | 0 | 169 | | if (lastError == Interop.WinHttp.ERROR_WINHTTP_AUTODETECTION_FAILED) |
| | 0 | 170 | | { |
| | 0 | 171 | | _autoDetectionFailed = true; |
| | 0 | 172 | | _lastTimeAutoDetectionFailed = Environment.TickCount; |
| | 0 | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | break; |
| | | 176 | | } |
| | 0 | 177 | | } |
| | 0 | 178 | | } while (repeat); |
| | | 179 | | |
| | | 180 | | // Fall back to manual settings if available. |
| | 0 | 181 | | if (!useProxy && !string.IsNullOrEmpty(Proxy)) |
| | 0 | 182 | | { |
| | 0 | 183 | | proxyInfo.AccessType = Interop.WinHttp.WINHTTP_ACCESS_TYPE_NAMED_PROXY; |
| | 0 | 184 | | proxyInfo.Proxy = Marshal.StringToHGlobalUni(Proxy); |
| | 0 | 185 | | proxyInfo.ProxyBypass = string.IsNullOrEmpty(ProxyBypass) ? |
| | 0 | 186 | | IntPtr.Zero : Marshal.StringToHGlobalUni(ProxyBypass); |
| | | 187 | | |
| | 0 | 188 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"Fallback to Proxy={Proxy}, ProxyBypass={ |
| | 0 | 189 | | useProxy = true; |
| | 0 | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"useProxy={useProxy}"); |
| | | 193 | | |
| | 0 | 194 | | return useProxy; |
| | 0 | 195 | | } |
| | | 196 | | } |
| | | 197 | | } |