KestrelServerOptionsExtensions.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using FastGithub.Configuration;
  2. using FastGithub.HttpServer;
  3. using Microsoft.AspNetCore.Connections;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.AspNetCore.Server.Kestrel.Core;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using System;
  10. using System.Net;
  11. namespace FastGithub
  12. {
  13. /// <summary>
  14. /// Kestrel扩展
  15. /// </summary>
  16. public static class KestrelServerOptionsExtensions
  17. {
  18. /// <summary>
  19. /// 无限制
  20. /// </summary>
  21. /// <param name="kestrel"></param>
  22. public static void NoLimit(this KestrelServerOptions kestrel)
  23. {
  24. kestrel.Limits.MaxRequestBodySize = null;
  25. kestrel.Limits.MinResponseDataRate = null;
  26. kestrel.Limits.MinRequestBodyDataRate = null;
  27. }
  28. /// <summary>
  29. /// 监听http代理
  30. /// </summary>
  31. /// <param name="kestrel"></param>
  32. public static void ListenHttpProxy(this KestrelServerOptions kestrel)
  33. {
  34. var options = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubOptions>>().Value;
  35. var httpProxyPort = options.HttpProxyPort;
  36. if (LocalMachine.CanListenTcp(httpProxyPort) == false)
  37. {
  38. throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
  39. }
  40. var logger = kestrel.GetLogger();
  41. kestrel.Listen(IPAddress.Loopback, httpProxyPort);
  42. logger.LogInformation($"已监听http://{IPAddress.Loopback}:{httpProxyPort},http代理服务启动完成");
  43. }
  44. /// <summary>
  45. /// 尝试监听ssh反向代理
  46. /// </summary>
  47. /// <param name="kestrel"></param>
  48. public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
  49. {
  50. const int SSH_PORT = 22;
  51. if (LocalMachine.CanListenTcp(SSH_PORT) == true)
  52. {
  53. kestrel.Listen(IPAddress.Loopback, SSH_PORT, listen => listen.UseConnectionHandler<SshReverseProxyHandler>());
  54. kestrel.GetLogger().LogInformation($"已监听ssh://{IPAddress.Loopback}:{SSH_PORT},github的ssh反向代理服务启动完成");
  55. }
  56. }
  57. /// <summary>
  58. /// 尝试监听http反向代理
  59. /// </summary>
  60. /// <param name="kestrel"></param>
  61. public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
  62. {
  63. const int HTTP_PORT = 80;
  64. if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
  65. {
  66. kestrel.Listen(IPAddress.Loopback, HTTP_PORT);
  67. kestrel.GetLogger().LogInformation($"已监听http://{IPAddress.Loopback}:{HTTP_PORT},http反向代理服务启动完成");
  68. }
  69. }
  70. /// <summary>
  71. /// 监听https反向代理
  72. /// </summary>
  73. /// <param name="kestrel"></param>
  74. /// <exception cref="FastGithubException"></exception>
  75. /// <returns></returns>
  76. public static int ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
  77. {
  78. var httpsPort = HttpsReverseProxyPort.Value;
  79. if (OperatingSystem.IsWindows())
  80. {
  81. TcpTable.KillPortOwner(httpsPort);
  82. }
  83. if (LocalMachine.CanListenTcp(httpsPort) == false)
  84. {
  85. throw new FastGithubException($"tcp端口{httpsPort}已经被其它进程占用");
  86. }
  87. var certService = kestrel.ApplicationServices.GetRequiredService<CertService>();
  88. certService.CreateCaCertIfNotExists();
  89. certService.InstallAndTrustCaCert();
  90. kestrel.Listen(IPAddress.Loopback, httpsPort,
  91. listen => listen.UseHttps(https =>
  92. https.ServerCertificateSelector = (ctx, domain) =>
  93. certService.GetOrCreateServerCert(domain)));
  94. if (httpsPort == 443)
  95. {
  96. var logger = kestrel.GetLogger();
  97. logger.LogInformation($"已监听https://{IPAddress.Loopback}:{httpsPort},https反向代理服务启动完成");
  98. }
  99. return httpsPort;
  100. }
  101. /// <summary>
  102. /// 获取日志
  103. /// </summary>
  104. /// <param name="kestrel"></param>
  105. /// <returns></returns>
  106. private static ILogger GetLogger(this KestrelServerOptions kestrel)
  107. {
  108. var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
  109. return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(HttpServer)}");
  110. }
  111. }
  112. }