From 53f04610478ef519748dd930c62ce8060944079b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 29 Jul 2021 09:14:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AB=99=E7=82=B9=E8=AF=81=E4=B9=A6=E4=B8=80?= =?UTF-8?q?=E5=B9=B4=E6=9C=89=E6=95=88=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../KestrelServerOptionsExtensions.cs | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs index 313e394..8be4fe0 100644 --- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs +++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs @@ -1,10 +1,11 @@ using FastGithub.ReverseProxy; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -12,7 +13,6 @@ using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Security.Cryptography.X509Certificates; -using System.Threading; namespace FastGithub { @@ -22,9 +22,9 @@ namespace FastGithub public static class KestrelServerOptionsExtensions { /// - /// 域名与证书 + /// 域名证书缓存 /// - private static readonly ConcurrentDictionary> domainCerts = new(); + private static readonly IMemoryCache domainCertCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); /// /// 监听https的反向代理 @@ -51,6 +51,7 @@ namespace FastGithub /// /// 生成根证书 + /// 10年 /// /// /// @@ -64,8 +65,8 @@ namespace FastGithub File.Delete(caPublicCerPath); File.Delete(caPrivateKeyPath); - var validFrom = DateTime.Today.AddYears(-10); - var validTo = DateTime.Today.AddYears(50); + var validFrom = DateTime.Today.AddDays(-1); + var validTo = DateTime.Today.AddYears(10); CertGenerator.GenerateBySelf(new[] { nameof(FastGithub) }, 2048, validFrom, validTo, caPublicCerPath, caPrivateKeyPath); } @@ -79,7 +80,7 @@ namespace FastGithub { if (OperatingSystem.IsWindows() == false) { - logger.LogWarning($"不支持自动安装根证书{caPublicCerPath}:请根据你的系统平台情况安装和信任根证书"); + logger.LogWarning($"不支持自动安装证书{caPublicCerPath}:请手动安装证书到根证书颁发机构"); } else { @@ -96,7 +97,7 @@ namespace FastGithub } catch (Exception) { - logger.LogWarning($"安装根证书{caPublicCerPath}失败:请手动安装到“将所有的证书都放入下载存储”\\“受信任的根证书颁发机构”"); + logger.LogWarning($"安装证书{caPublicCerPath}失败:请手动安装到“将所有的证书都放入下载存储”\\“受信任的根证书颁发机构”"); } } } @@ -110,17 +111,18 @@ namespace FastGithub /// private static X509Certificate2 GetDomainCert(string? domain, string caPublicCerPath, string caPrivateKeyPath) { - return domainCerts.GetOrAdd(domain ?? string.Empty, GetOrCreateCert).Value; + return domainCertCache.GetOrCreate(domain ?? string.Empty, GetOrCreateCert); - Lazy GetOrCreateCert(string host) + // 生成域名的1年证书 + X509Certificate2 GetOrCreateCert(ICacheEntry entry) { - return new Lazy(() => - { - var domains = GetDomains(host).Distinct(); - var validFrom = DateTime.Today.AddYears(-1); - var validTo = DateTime.Today.AddYears(10); - return CertGenerator.GenerateByCa(domains, 2048, validFrom, validTo, caPublicCerPath, caPrivateKeyPath); - }, LazyThreadSafetyMode.ExecutionAndPublication); + var host = (string)entry.Key; + var domains = GetDomains(host).Distinct(); + var validFrom = DateTime.Today.AddDays(-1); + var validTo = DateTime.Today.AddYears(1); + + entry.SetAbsoluteExpiration(validTo); + return CertGenerator.GenerateByCa(domains, 2048, validFrom, validTo, caPublicCerPath, caPrivateKeyPath); } }