KestrelServerOptionsExtensions.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using FastGithub.Configuration;
  2. using FastGithub.ReverseProxy;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Server.Kestrel.Core;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging;
  7. using System;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.NetworkInformation;
  11. using System.Security.Authentication;
  12. namespace FastGithub
  13. {
  14. /// <summary>
  15. /// Kestrel扩展
  16. /// </summary>
  17. public static class KestrelServerOptionsExtensions
  18. {
  19. /// <summary>
  20. /// 监听http的反向代理
  21. /// </summary>
  22. /// <param name="kestrel"></param>
  23. public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
  24. {
  25. const int HTTP_PORT = 80;
  26. if (OperatingSystem.IsWindows())
  27. {
  28. TcpTable.KillPortOwner(HTTP_PORT);
  29. }
  30. if (CanTcpListen(HTTP_PORT) == false)
  31. {
  32. var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
  33. var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
  34. logger.LogWarning($"由于tcp端口{HTTP_PORT}已经被其它进程占用,http反向代理功能将受限");
  35. }
  36. else
  37. {
  38. kestrel.Listen(IPAddress.Any, HTTP_PORT);
  39. }
  40. }
  41. /// <summary>
  42. /// 监听https的反向代理
  43. /// </summary>
  44. /// <param name="kestrel"></param>
  45. public static void ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
  46. {
  47. const int HTTPS_PORT = 443;
  48. if (OperatingSystem.IsWindows())
  49. {
  50. TcpTable.KillPortOwner(HTTPS_PORT);
  51. }
  52. if (CanTcpListen(HTTPS_PORT) == false)
  53. {
  54. throw new FastGithubException($"由于tcp端口{HTTPS_PORT}已经被其它进程占用,{nameof(FastGithub)}无法进行必须的https反向代理");
  55. }
  56. var certService = kestrel.ApplicationServices.GetRequiredService<CertService>();
  57. certService.CreateCaCertIfNotExists();
  58. certService.InstallAndTrustCaCert();
  59. kestrel.Listen(IPAddress.Any, HTTPS_PORT, listen => listen.UseHttps(https =>
  60. {
  61. https.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
  62. https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain);
  63. }));
  64. }
  65. /// <summary>
  66. /// 是否可以监听指定端口
  67. /// </summary>
  68. /// <param name="port"></param>
  69. /// <returns></returns>
  70. private static bool CanTcpListen(int port)
  71. {
  72. var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
  73. return tcpListeners.Any(item => item.Port == port) == false;
  74. }
  75. }
  76. }