| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | // Copied from https://raw.githubusercontent.com/dotnet/corefx/master/src/System.Net.Http/src/System/Net/Http/SocketsHtt |
| | 5 | |
|
| | 6 | | #nullable disable |
| | 7 | |
|
| | 8 | | using System; |
| | 9 | | using System.Net; |
| | 10 | | using System.Collections.Generic; |
| | 11 | |
|
| | 12 | | namespace Azure.Core.Pipeline |
| | 13 | | { |
| | 14 | |
|
| | 15 | | internal sealed partial class HttpEnvironmentProxy : IWebProxy |
| | 16 | | { |
| | 17 | | private const string EnvAllProxyUC = "ALL_PROXY"; |
| | 18 | | private const string EnvAllProxyLC = "all_proxy"; |
| | 19 | | private const string EnvHttpProxyLC = "http_proxy"; |
| | 20 | | private const string EnvHttpProxyUC = "HTTP_PROXY"; |
| | 21 | | private const string EnvHttpsProxyLC = "https_proxy"; |
| | 22 | | private const string EnvHttpsProxyUC = "HTTPS_PROXY"; |
| | 23 | | private const string EnvNoProxyLC = "no_proxy"; |
| | 24 | | private const string EnvNoProxyUC = "NO_PROXY"; |
| | 25 | | private const string EnvCGI = "GATEWAY_INTERFACE"; // Running in a CGI environment. |
| | 26 | |
|
| | 27 | | private readonly Uri _httpProxyUri; // String URI for HTTP requests |
| | 28 | | private readonly Uri _httpsProxyUri; // String URI for HTTPS requests |
| | 29 | | private readonly string[] _bypass;// list of domains not to proxy |
| | 30 | | private readonly ICredentials _credentials; |
| | 31 | |
|
| | 32 | | public static bool TryCreate(out IWebProxy proxy) |
| | 33 | | { |
| | 34 | | // Get environment variables. Protocol specific take precedence over |
| | 35 | | // general all_*, lower case variable has precedence over upper case. |
| | 36 | | // Note that curl uses HTTPS_PROXY but not HTTP_PROXY. |
| | 37 | |
|
| 724 | 38 | | Uri httpProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpProxyLC)); |
| 724 | 39 | | if (httpProxy == null && Environment.GetEnvironmentVariable(EnvCGI) == null) |
| | 40 | | { |
| 704 | 41 | | httpProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpProxyUC)); |
| | 42 | | } |
| | 43 | |
|
| 724 | 44 | | Uri httpsProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpsProxyLC)) ?? |
| 724 | 45 | | GetUriFromString(Environment.GetEnvironmentVariable(EnvHttpsProxyUC)); |
| | 46 | |
|
| 724 | 47 | | if (httpProxy == null || httpsProxy == null) |
| | 48 | | { |
| 722 | 49 | | Uri allProxy = GetUriFromString(Environment.GetEnvironmentVariable(EnvAllProxyLC)) ?? |
| 722 | 50 | | GetUriFromString(Environment.GetEnvironmentVariable(EnvAllProxyUC)); |
| | 51 | |
|
| 722 | 52 | | if (httpProxy == null) |
| | 53 | | { |
| 704 | 54 | | httpProxy = allProxy; |
| | 55 | | } |
| | 56 | |
|
| 722 | 57 | | if (httpsProxy == null) |
| | 58 | | { |
| 720 | 59 | | httpsProxy = allProxy; |
| | 60 | | } |
| | 61 | | } |
| | 62 | |
|
| | 63 | | // Do not instantiate if nothing is set. |
| | 64 | | // Caller may pick some other proxy type. |
| 724 | 65 | | if (httpProxy == null && httpsProxy == null) |
| | 66 | | { |
| 666 | 67 | | proxy = null; |
| 666 | 68 | | return false; |
| | 69 | | } |
| | 70 | |
|
| 58 | 71 | | string noProxy = Environment.GetEnvironmentVariable(EnvNoProxyLC) ?? |
| 58 | 72 | | Environment.GetEnvironmentVariable(EnvNoProxyUC); |
| 58 | 73 | | proxy = new HttpEnvironmentProxy(httpProxy, httpsProxy, noProxy); |
| | 74 | |
|
| 58 | 75 | | return true; |
| | 76 | | } |
| | 77 | |
|
| 58 | 78 | | private HttpEnvironmentProxy(Uri httpProxy, Uri httpsProxy, string bypassList) |
| | 79 | | { |
| 58 | 80 | | _httpProxyUri = httpProxy; |
| 58 | 81 | | _httpsProxyUri = httpsProxy; |
| | 82 | |
|
| 58 | 83 | | _credentials = HttpEnvironmentProxyCredentials.TryCreate(httpProxy, httpsProxy); |
| | 84 | |
|
| 58 | 85 | | if (!string.IsNullOrWhiteSpace(bypassList)) |
| | 86 | | { |
| 10 | 87 | | string[] list = bypassList.Split(','); |
| 10 | 88 | | List<string> tmpList = new List<string>(list.Length); |
| | 89 | |
|
| 64 | 90 | | foreach (string value in list) |
| | 91 | | { |
| 22 | 92 | | string tmp = value.Trim(); |
| 22 | 93 | | if (tmp.Length > 0) |
| | 94 | | { |
| 20 | 95 | | tmpList.Add(tmp); |
| | 96 | | } |
| | 97 | | } |
| 10 | 98 | | if (tmpList.Count > 0) |
| | 99 | | { |
| 10 | 100 | | _bypass = tmpList.ToArray(); |
| | 101 | | } |
| | 102 | | } |
| 58 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// This function will evaluate given string and it will try to convert |
| | 107 | | /// it to Uri object. The string could contain URI fragment, IP address and port |
| | 108 | | /// tuple or just IP address or name. It will return null if parsing fails. |
| | 109 | | /// </summary> |
| | 110 | | private static Uri GetUriFromString(string value) |
| | 111 | | { |
| 4272 | 112 | | if (string.IsNullOrEmpty(value)) |
| | 113 | | { |
| 4196 | 114 | | return null; |
| | 115 | | } |
| 76 | 116 | | if (value.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) |
| | 117 | | { |
| 50 | 118 | | value = value.Substring(7); |
| | 119 | | } |
| | 120 | |
|
| 76 | 121 | | string user = null; |
| 76 | 122 | | string password = null; |
| 76 | 123 | | ushort port = 80; |
| | 124 | |
|
| | 125 | | // Check if there is authentication part with user and possibly password. |
| | 126 | | // Curl accepts raw text and that may break URI parser. |
| 76 | 127 | | int separatorIndex = value.LastIndexOf('@'); |
| 76 | 128 | | if (separatorIndex != -1) |
| | 129 | | { |
| 28 | 130 | | string auth = value.Substring(0, separatorIndex); |
| | 131 | |
|
| | 132 | | // The User and password may or may not be URL encoded. |
| | 133 | | // Curl seems to accept both. To match that, |
| | 134 | | // we do opportunistic decode and we use original string if it fails. |
| | 135 | | try |
| | 136 | | { |
| 28 | 137 | | auth = Uri.UnescapeDataString(auth); |
| 28 | 138 | | } |
| 0 | 139 | | catch { }; |
| | 140 | |
|
| 28 | 141 | | value = value.Substring(separatorIndex + 1); |
| 28 | 142 | | separatorIndex = auth.IndexOf(':'); |
| 28 | 143 | | if (separatorIndex == -1) |
| | 144 | | { |
| 6 | 145 | | user = auth; |
| | 146 | | } |
| | 147 | | else |
| | 148 | | { |
| 22 | 149 | | user = auth.Substring(0, separatorIndex); |
| 22 | 150 | | password = auth.Substring(separatorIndex + 1); |
| | 151 | | } |
| | 152 | | } |
| | 153 | |
|
| 76 | 154 | | int ipV6AddressEnd = value.IndexOf(']'); |
| 76 | 155 | | separatorIndex = value.LastIndexOf(':'); |
| | 156 | | string host; |
| | 157 | | // No ':' or it is part of IPv6 address. |
| 76 | 158 | | if (separatorIndex == -1 || (ipV6AddressEnd != -1 && separatorIndex < ipV6AddressEnd)) |
| | 159 | | { |
| 16 | 160 | | host = value; |
| | 161 | | } |
| | 162 | | else |
| | 163 | | { |
| 60 | 164 | | host = value.Substring(0, separatorIndex); |
| 60 | 165 | | int endIndex = separatorIndex + 1; |
| | 166 | | // Strip any trailing characters after port number. |
| 304 | 167 | | while (endIndex < value.Length) |
| | 168 | | { |
| 256 | 169 | | if (!char.IsDigit(value[endIndex])) |
| | 170 | | { |
| | 171 | | break; |
| | 172 | | } |
| 244 | 173 | | endIndex += 1; |
| | 174 | | } |
| | 175 | |
|
| 60 | 176 | | if (!ushort.TryParse(value.Substring(separatorIndex + 1, endIndex - separatorIndex - 1), out port)) |
| | 177 | | { |
| 0 | 178 | | return null; |
| | 179 | | } |
| | 180 | | } |
| | 181 | |
|
| | 182 | | try |
| | 183 | | { |
| 76 | 184 | | UriBuilder ub = new UriBuilder("http", host, port); |
| 76 | 185 | | if (user != null) |
| | 186 | | { |
| 28 | 187 | | ub.UserName = Uri.EscapeDataString(user); |
| | 188 | | } |
| | 189 | |
|
| 76 | 190 | | if (password != null) |
| | 191 | | { |
| 22 | 192 | | ub.Password = Uri.EscapeDataString(password); |
| | 193 | | } |
| | 194 | |
|
| 76 | 195 | | return ub.Uri; |
| | 196 | | } |
| 16 | 197 | | catch { }; |
| 8 | 198 | | return null; |
| 68 | 199 | | } |
| | 200 | |
|
| | 201 | | /// <summary> |
| | 202 | | /// This function returns true if given Host match bypass list. |
| | 203 | | /// Note, that the list is common for http and https. |
| | 204 | | /// </summary> |
| | 205 | | private bool IsMatchInBypassList(Uri input) |
| | 206 | | { |
| 34 | 207 | | if (_bypass != null) |
| | 208 | | { |
| 116 | 209 | | foreach (string s in _bypass) |
| | 210 | | { |
| 40 | 211 | | if (s[0] == '.') |
| | 212 | | { |
| | 213 | | // This should match either domain it self or any subdomain or host |
| | 214 | | // .foo.com will match foo.com it self or *.foo.com |
| 26 | 215 | | if ((s.Length - 1) == input.Host.Length && |
| 26 | 216 | | string.Compare(s, 1, input.Host, 0, input.Host.Length, StringComparison.OrdinalIgnoreCase) = |
| | 217 | | { |
| 10 | 218 | | return true; |
| | 219 | | } |
| 16 | 220 | | else if (input.Host.EndsWith(s, StringComparison.OrdinalIgnoreCase)) |
| | 221 | | { |
| 2 | 222 | | return true; |
| | 223 | | } |
| | 224 | |
|
| | 225 | | } |
| | 226 | | else |
| | 227 | | { |
| 14 | 228 | | if (string.Equals(s, input.Host, StringComparison.OrdinalIgnoreCase)) |
| | 229 | | { |
| 4 | 230 | | return true; |
| | 231 | | } |
| | 232 | | } |
| | 233 | | } |
| | 234 | | } |
| 18 | 235 | | return false; |
| | 236 | | } |
| | 237 | |
|
| | 238 | | /// <summary> |
| | 239 | | /// Gets the proxy URI. (iWebProxy interface). |
| | 240 | | /// </summary> |
| | 241 | | public Uri GetProxy(Uri uri) |
| | 242 | | { |
| 96 | 243 | | return uri.Scheme == Uri.UriSchemeHttp ? _httpProxyUri : _httpsProxyUri; |
| | 244 | | } |
| | 245 | |
|
| | 246 | | /// <summary> |
| | 247 | | /// Checks if URI is subject to proxy or not. |
| | 248 | | /// </summary> |
| | 249 | | public bool IsBypassed(Uri uri) |
| | 250 | | { |
| 34 | 251 | | return GetProxy(uri) == null ? true : IsMatchInBypassList(uri); |
| | 252 | | } |
| | 253 | |
|
| | 254 | | public ICredentials Credentials |
| | 255 | | { |
| | 256 | | get |
| | 257 | | { |
| 32 | 258 | | return _credentials; |
| | 259 | | } |
| 0 | 260 | | set { throw new NotSupportedException(); } |
| | 261 | | } |
| | 262 | | } |
| | 263 | | } |