ListenOptionsHttpsExtensions.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using FastGithub.ReverseProxy;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Server.Kestrel.Core;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Security.Cryptography.X509Certificates;
  7. namespace FastGithub
  8. {
  9. public static class ListenOptionsHttpsExtensions
  10. {
  11. /// <summary>
  12. /// 应用fastGihub的https
  13. /// </summary>
  14. /// <param name="listenOptions"></param>
  15. /// <param name="caPublicCerPath"></param>
  16. /// <param name="caPrivateKeyPath"></param>
  17. /// <returns></returns>
  18. public static ListenOptions UseGithubHttps(this ListenOptions listenOptions, string caPublicCerPath, string caPrivateKeyPath)
  19. {
  20. return listenOptions.UseHttps(https =>
  21. {
  22. var certs = new ConcurrentDictionary<string, X509Certificate2>();
  23. https.ServerCertificateSelector = (ctx, domain) =>
  24. certs.GetOrAdd(domain, d =>
  25. CertGenerator.Generate(
  26. new[] { d },
  27. 2048,
  28. DateTime.Today.AddYears(-1),
  29. DateTime.Today.AddYears(1),
  30. caPublicCerPath,
  31. caPrivateKeyPath));
  32. });
  33. }
  34. }
  35. }