KestrelServerOptionsExtensions.cs 4.5 KB

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