Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

feat: add Tls options required

fatDuck4df6da2bc31c
diff --git a/optimizerDuck/Common/Helpers/GitHubSourceHelper.cs b/optimizerDuck/Common/Helpers/GitHubSourceHelper.cs
index 75741cb..007285e 100644
--- a/optimizerDuck/Common/Helpers/GitHubSourceHelper.cs
+++ b/optimizerDuck/Common/Helpers/GitHubSourceHelper.cs
@@ -15,7 +15,7 @@ namespace optimizerDuck.Common.Helpers;
/// </summary>
public static class GitHubSourceHelper
{
- private static readonly HttpClient HttpClient = new() { Timeout = TimeSpan.FromSeconds(5) };
+ private static readonly HttpClient HttpClient = HttpClientFactory.CreateClient(timeout: TimeSpan.FromSeconds(5));
private static readonly ConcurrentDictionary<
string,
Lazy<Task<(string Content, DateTime FetchedAt)>>
diff --git a/optimizerDuck/Common/Helpers/HttpClientFactory.cs b/optimizerDuck/Common/Helpers/HttpClientFactory.cs
new file mode 100644
index 0000000..7d01b64
--- /dev/null
+++ b/optimizerDuck/Common/Helpers/HttpClientFactory.cs
@@ -0,0 +1,85 @@
+using System.Diagnostics;
+using System.Net.Http;
+using System.Net.Security;
+using System.Security.Authentication;
+using System.Security.Cryptography.X509Certificates;
+using Microsoft.Extensions.Logging;
+
+namespace optimizerDuck.Common.Helpers;
+
+public static class HttpClientFactory
+{
+ /// <summary>
+ /// Creates an <see cref="HttpClient"/> with explicit TLS 1.2+ enforcement and
+ /// a <see cref="RemoteCertificateValidationCallback"/> that validates server
+ /// certificates via the system certificate store while logging any failures.
+ /// </summary>
+ /// <param name="timeout">Optional per-request timeout.</param>
+ /// <param name="logger">Optional logger for TLS diagnostic output.</param>
+ /// <returns>A configured <see cref="HttpClient"/> instance.</returns>
+ public static HttpClient CreateClient(TimeSpan? timeout = null, ILogger? logger = null)
+ {
+ var handler = new SocketsHttpHandler
+ {
+ PooledConnectionLifetime = TimeSpan.FromMinutes(5),
+ PooledConnectionIdleTimeout = TimeSpan.FromMinutes(2),
+ MaxConnectionsPerServer = 10,
+ SslOptions = new SslClientAuthenticationOptions
+ {
+ EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13,
+ RemoteCertificateValidationCallback = (sender, certificate, chain, errors) =>
+ {
+ if (errors == SslPolicyErrors.None)
+ return true;
+
+ var certInfo = certificate != null
+ ? $"Subject: {certificate.Subject}, Issuer: {certificate.Issuer}, "
+ + $"Expires: {certificate.GetExpirationDateString()}"
+ : "No certificate presented";
+
+ var remoteEndpoint = GetRemoteEndpoint(sender);
+
+ if (logger != null)
+ {
+ logger.LogWarning(
+ "TLS certificate validation failed for {Endpoint}. {CertInfo}. Errors: {Errors}",
+ remoteEndpoint,
+ certInfo,
+ errors
+ );
+ }
+ else
+ {
+ Trace.TraceWarning(
+ "TLS certificate validation failed for {0}. {1}. Errors: {2}",
+ remoteEndpoint,
+ certInfo,
+ errors
+ );
+ }
+
+ return false;
+ }
+ }
+ };
+
+ var client = new HttpClient(handler, disposeHandler: true);
+
+ if (timeout.HasValue)
+ client.Timeout = timeout.Value;
+
+ return client;
+ }
+
+ private static string GetRemoteEndpoint(object? sender)
+ {
+ if (sender is not HttpRequestMessage request)
+ return "Unknown";
+
+ var uri = request.RequestUri;
+ if (uri == null)
+ return "Unknown";
+
+ return uri.IsDefaultPort ? uri.Host : $"{uri.Host}:{uri.Port}";
+ }
+}
diff --git a/optimizerDuck/Services/System/StreamService.cs b/optimizerDuck/Services/System/StreamService.cs
index 18b6efa..a54b7ea 100644
--- a/optimizerDuck/Services/System/StreamService.cs
+++ b/optimizerDuck/Services/System/StreamService.cs
@@ -30,7 +30,7 @@ public class StreamService(ILogger<StreamService> logger) : IDisposable
try
{
Directory.CreateDirectory(Shared.DownloadsDirectory);
- _client ??= new HttpClient();
+ _client ??= HttpClientFactory.CreateClient(logger: logger);
using var response = await _client.GetAsync(url).ConfigureAwait(false);
logger.LogDebug("Received HTTP {StatusCode} from {Url}", response.StatusCode, url);
diff --git a/optimizerDuck/Services/System/UpdaterService.cs b/optimizerDuck/Services/System/UpdaterService.cs
index c23a2c3..d8f960d 100644
--- a/optimizerDuck/Services/System/UpdaterService.cs
+++ b/optimizerDuck/Services/System/UpdaterService.cs
@@ -21,7 +21,7 @@ public class UpdaterService : IDisposable
/// <param name="logger">The logger for update diagnostics.</param>
public UpdaterService(ILogger<UpdaterService> logger)
{
- _httpClient = new HttpClient();
+ _httpClient = HttpClientFactory.CreateClient(logger: logger);
_httpClient.DefaultRequestHeaders.UserAgent.Add(
new ProductInfoHeaderValue("optimizerDuck", "1.0")
);