/// <summary> /// Tries to create a proxy by reading values from <see cref="_configuration"/>. /// </summary> private IWebProxy GetWebProxyFromConfiguration() { string host; bool isEnabled; string isEnabledString = GetValueFromProxyConfiguration <string>(_configuration, ProxyConstants.IsEnabled); if (!bool.TryParse(isEnabledString, out isEnabled) || !isEnabled) { return(null); } // Try to get Proxy from config.json host = GetValueFromProxyConfiguration <string>(_configuration, ProxyConstants.Address); string username = GetValueFromProxyConfiguration <string>(_configuration, ProxyConstants.Username); string password = GetValueFromProxyConfiguration <string>(_configuration, ProxyConstants.Password); if (!string.IsNullOrEmpty(host)) { var proxy = new ApiPort.Proxy.WebProxy(host); if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)) { proxy.Credentials = new NetworkCredential(username, password); } return(proxy); } else { return(null); } }
/// <summary> /// Gets the proxy from environment variables. /// </summary> private IWebProxy GetWebProxyFromEnvironment() { // Try reading from the environment variable http_proxy. This would be specified as http://<username>:<password>@proxy.com var host = Environment.GetEnvironmentVariable(ProxyConstants.HttpProxy); Uri uri; if (!string.IsNullOrEmpty(host) && Uri.TryCreate(host, UriKind.Absolute, out uri)) { var webProxy = new ApiPort.Proxy.WebProxy(uri.GetComponents(UriComponents.HttpRequestUrl, UriFormat.SafeUnescaped)); if (!string.IsNullOrEmpty(uri.UserInfo)) { var credentials = uri.UserInfo.Split(':'); if (credentials.Length > 1) { webProxy.Credentials = new NetworkCredential( userName: credentials[0], password: credentials[1] ); } } var noProxy = Environment.GetEnvironmentVariable(ProxyConstants.NoProxy); if (!string.IsNullOrEmpty(noProxy)) { // split comma-separated list of domains webProxy.BypassList = noProxy.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); } return(webProxy); } else { return(null); } }