Ejemplo n.º 1
0
        private static IWebProxy TryCreateWebProxy(WebProxyOptions webProxyOptions)
        {
            if (webProxyOptions == null || webProxyOptions.Address == null)
            {
                return(null);
            }

            var webProxy = new WebProxy(webProxyOptions.Address);

            webProxy.UseDefaultCredentials = webProxyOptions.UseDefaultCredentials.GetValueOrDefault(false);
            webProxy.BypassProxyOnLocal    = webProxyOptions.BypassOnLocal.GetValueOrDefault(false);

            return(webProxy);
        }
        private ProxyHttpClientOptions CreateProxyHttpClientOptions(IConfigurationSection section)
        {
            if (!section.Exists())
            {
                return(null);
            }

            var certSection = section.GetSection(nameof(ProxyHttpClientOptions.ClientCertificate));

            X509Certificate2 clientCertificate = null;

            if (certSection.Exists())
            {
                clientCertificate = _certificateConfigLoader.LoadCertificate(certSection);
            }

            if (clientCertificate != null)
            {
                Certificates.AddLast(new WeakReference <X509Certificate2>(clientCertificate));
            }

            SslProtocols?sslProtocols = null;

            if (section.GetSection(nameof(ProxyHttpClientOptions.SslProtocols)) is IConfigurationSection sslProtocolsSection)
            {
                foreach (var protocolConfig in sslProtocolsSection.GetChildren().Select(s => Enum.Parse <SslProtocols>(s.Value, ignoreCase: true)))
                {
                    sslProtocols = sslProtocols == null ? protocolConfig : sslProtocols | protocolConfig;
                }
            }

            WebProxyOptions webProxy;
            var             webProxySection = section.GetSection(nameof(ProxyHttpClientOptions.WebProxy));

            if (webProxySection.Exists())
            {
                webProxy = new WebProxyOptions()
                {
                    Address               = webProxySection.ReadUri(nameof(WebProxyOptions.Address)),
                    BypassOnLocal         = webProxySection.ReadBool(nameof(WebProxyOptions.BypassOnLocal)),
                    UseDefaultCredentials = webProxySection.ReadBool(nameof(WebProxyOptions.UseDefaultCredentials))
                };
            }
            else
            {
                webProxy = null;
            }

            return(new ProxyHttpClientOptions
            {
                SslProtocols = sslProtocols,
                DangerousAcceptAnyServerCertificate = section.ReadBool(nameof(ProxyHttpClientOptions.DangerousAcceptAnyServerCertificate)),
                ClientCertificate = clientCertificate,
                MaxConnectionsPerServer = section.ReadInt32(nameof(ProxyHttpClientOptions.MaxConnectionsPerServer)),
#if NET
                EnableMultipleHttp2Connections = section.ReadBool(nameof(ProxyHttpClientOptions.EnableMultipleHttp2Connections)),
                RequestHeaderEncoding = section[nameof(ProxyHttpClientOptions.RequestHeaderEncoding)] is string encoding?Encoding.GetEncoding(encoding) : null,
#endif
                ActivityContextHeaders = section.ReadEnum <ActivityContextHeaders>(nameof(ProxyHttpClientOptions.ActivityContextHeaders)),
                WebProxy = webProxy
            });