From 90fa59ae6b1a2422737932dde48b51a2553c6c2d Mon Sep 17 00:00:00 2001 From: Florian Bernd Date: Wed, 5 Jul 2023 11:03:46 +0200 Subject: [PATCH] NetFx: Add workaround for unsupported `HttpClientHandler.SslProtocols` (#58) --- .../Resolvers/ApiResolver.cs | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Elastic.Stack.ArtifactsApi/Resolvers/ApiResolver.cs b/src/Elastic.Stack.ArtifactsApi/Resolvers/ApiResolver.cs index cb6cea9..7d097df 100644 --- a/src/Elastic.Stack.ArtifactsApi/Resolvers/ApiResolver.cs +++ b/src/Elastic.Stack.ArtifactsApi/Resolvers/ApiResolver.cs @@ -7,8 +7,19 @@ using System.Collections.Generic; using System.IO; using System.Linq; + +#if NETFRAMEWORK +using System.Net; +#endif + using System.Net.Http; + +#if !NETFRAMEWORK + using System.Security.Authentication; + +#endif + using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; @@ -22,7 +33,12 @@ public static class ApiResolver private static readonly ConcurrentDictionary Releases = new ConcurrentDictionary(); private static HttpClient HttpClient { get; } = - new HttpClient(new HttpClientHandler {SslProtocols = SslProtocols.Tls12}) +#if NETFRAMEWORK + new HttpClient +#else + // SslProtocols is only available in .NET Framework 4.7.2 and above + new HttpClient(new HttpClientHandler { SslProtocols = SslProtocols.Tls12 }) +#endif { BaseAddress = new Uri(ArtifactsApiUrl) }; @@ -39,9 +55,10 @@ public static string FetchJson(string path) public static bool IsReleasedVersion(string version) { - if (Releases.TryGetValue(version, out var released)) return released; + if (Releases.TryGetValue(version, out var released)) + return released; var versionPath = "https://github.com/elastic/elasticsearch/releases/tag/v" + version; - var message = new HttpRequestMessage {Method = HttpMethod.Head, RequestUri = new Uri(versionPath)}; + var message = new HttpRequestMessage { Method = HttpMethod.Head, RequestUri = new Uri(versionPath) }; using (var response = HttpClient.SendAsync(message).GetAwaiter().GetResult()) { @@ -76,6 +93,14 @@ public static string GetBuildHash(string url) return tokens[1]; } + +#if NETFRAMEWORK + static ApiResolver() => + ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol + & ~SecurityProtocolType.Ssl3 + & ~SecurityProtocolType.Tls + & ~SecurityProtocolType.Tls11; +#endif } internal class ArtifactsVersionsResponse