KestrelServerOptionsExtensions.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using FastGithub.Configuration;
  2. using FastGithub.ReverseProxy;
  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. }
  26. /// <summary>
  27. /// 监听http代理
  28. /// </summary>
  29. /// <param name="kestrel"></param>
  30. public static void ListenHttpProxy(this KestrelServerOptions kestrel)
  31. {
  32. var options = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubOptions>>().Value;
  33. var httpProxyPort = options.HttpProxyPort;
  34. if (LocalMachine.CanListenTcp(httpProxyPort) == false)
  35. {
  36. throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
  37. }
  38. var logger = kestrel.GetLogger();
  39. kestrel.Listen(IPAddress.Loopback, httpProxyPort);
  40. logger.LogInformation($"已监听http://127.0.0.1:{httpProxyPort},http代理服务启动完成");
  41. }
  42. /// <summary>
  43. /// 尝试监听ssh反向代理
  44. /// </summary>
  45. /// <param name="kestrel"></param>
  46. public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
  47. {
  48. const int SSH_PORT = 22;
  49. if (LocalMachine.CanListenTcp(SSH_PORT) == true)
  50. {
  51. kestrel.Listen(IPAddress.Loopback, SSH_PORT, listen => listen.UseConnectionHandler<SshReverseProxyHandler>());
  52. kestrel.GetLogger().LogInformation($"已监听ssh://127.0.0.1:{SSH_PORT},github的ssh反向代理服务启动完成");
  53. }
  54. }
  55. /// <summary>
  56. /// 尝试监听http反向代理
  57. /// </summary>
  58. /// <param name="kestrel"></param>
  59. public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
  60. {
  61. const int HTTP_PORT = 80;
  62. if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
  63. {
  64. kestrel.Listen(IPAddress.Loopback, HTTP_PORT);
  65. kestrel.GetLogger().LogInformation($"已监听http://127.0.0.1:{HTTP_PORT},http反向代理服务启动完成");
  66. }
  67. }
  68. /// <summary>
  69. /// 监听https反向代理
  70. /// </summary>
  71. /// <param name="kestrel"></param>
  72. /// <exception cref="FastGithubException"></exception>
  73. /// <returns></returns>
  74. public static int ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
  75. {
  76. var httpsPort = HttpsReverseProxyPort.Value;
  77. if (OperatingSystem.IsWindows())
  78. {
  79. TcpTable.KillPortOwner(httpsPort);
  80. }
  81. if (LocalMachine.CanListenTcp(httpsPort) == false)
  82. {
  83. throw new FastGithubException($"tcp端口{httpsPort}已经被其它进程占用");
  84. }
  85. var certService = kestrel.ApplicationServices.GetRequiredService<CertService>();
  86. certService.CreateCaCertIfNotExists();
  87. certService.InstallAndTrustCaCert();
  88. kestrel.Listen(IPAddress.Loopback, httpsPort,
  89. listen => listen.UseHttps(https =>
  90. https.ServerCertificateSelector = (ctx, domain) =>
  91. certService.GetOrCreateServerCert(domain)));
  92. if (httpsPort == 443)
  93. {
  94. var logger = kestrel.GetLogger();
  95. logger.LogInformation($"已监听https://127.0.0.1:{httpsPort},https反向代理服务启动完成");
  96. }
  97. return httpsPort;
  98. }
  99. /// <summary>
  100. /// 获取日志
  101. /// </summary>
  102. /// <param name="kestrel"></param>
  103. /// <returns></returns>
  104. private static ILogger GetLogger(this KestrelServerOptions kestrel)
  105. {
  106. var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
  107. return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
  108. }
  109. }
  110. }