KestrelServerOptionsExtensions.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 System;
  9. using System.Net;
  10. namespace FastGithub
  11. {
  12. /// <summary>
  13. /// Kestrel扩展
  14. /// </summary>
  15. public static class KestrelServerOptionsExtensions
  16. {
  17. /// <summary>
  18. /// 无限制
  19. /// </summary>
  20. /// <param name="kestrel"></param>
  21. public static void NoLimit(this KestrelServerOptions kestrel)
  22. {
  23. kestrel.Limits.MaxRequestBodySize = null;
  24. }
  25. /// <summary>
  26. /// 监听http代理
  27. /// </summary>
  28. /// <param name="kestrel"></param>
  29. public static void ListenHttpProxy(this KestrelServerOptions kestrel)
  30. {
  31. var httpPort = kestrel.ApplicationServices.GetRequiredService<PortService>().HttpProxyPort;
  32. if (LocalMachine.CanListenTcp(httpPort) == false)
  33. {
  34. throw new FastGithubException("tcp端口{httpsPort}已经被其它进程占用,请在配置文件更换一个端口");
  35. }
  36. kestrel.Listen(IPAddress.Any, httpPort);
  37. kestrel.GetLogger().LogInformation($"已监听tcp端口{httpPort},http代理启动完成");
  38. }
  39. /// <summary>
  40. /// 监听ssh反向代理
  41. /// </summary>
  42. /// <param name="kestrel"></param>
  43. public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
  44. {
  45. const int SSH_PORT = 22;
  46. if (LocalMachine.CanListenTcp(SSH_PORT) == true)
  47. {
  48. kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler<SshReverseProxyHandler>());
  49. kestrel.GetLogger().LogInformation($"已监听tcp端口{SSH_PORT},github的ssh代理启动完成");
  50. }
  51. }
  52. /// <summary>
  53. /// 监听http反向代理
  54. /// </summary>
  55. /// <param name="kestrel"></param>
  56. public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
  57. {
  58. const int HTTP_PORT = 80;
  59. if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
  60. {
  61. kestrel.Listen(IPAddress.Any, HTTP_PORT);
  62. kestrel.GetLogger().LogInformation($"已监听tcp端口{HTTP_PORT},http反向代理启动完成");
  63. }
  64. }
  65. /// <summary>
  66. /// 监听https反向代理
  67. /// </summary>
  68. /// <param name="kestrel"></param>
  69. /// <exception cref="FastGithubException"></exception>
  70. public static void ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
  71. {
  72. var httpsPort = kestrel.ApplicationServices.GetRequiredService<PortService>().HttpsReverseProxyPort;
  73. if (OperatingSystem.IsWindows())
  74. {
  75. TcpTable.KillPortOwner(httpsPort);
  76. }
  77. if (LocalMachine.CanListenTcp(httpsPort) == false)
  78. {
  79. throw new FastGithubException($"tcp端口{httpsPort}已经被其它进程占用");
  80. }
  81. var certService = kestrel.ApplicationServices.GetRequiredService<CertService>();
  82. certService.CreateCaCertIfNotExists();
  83. certService.InstallAndTrustCaCert();
  84. kestrel.Listen(IPAddress.Any, httpsPort,
  85. listen => listen.UseHttps(https =>
  86. https.ServerCertificateSelector = (ctx, domain) =>
  87. certService.GetOrCreateServerCert(domain)));
  88. var logger = kestrel.GetLogger();
  89. logger.LogInformation($"已监听tcp端口{httpsPort},https反向代理启动完成");
  90. }
  91. /// <summary>
  92. /// 获取日志
  93. /// </summary>
  94. /// <param name="kestrel"></param>
  95. /// <returns></returns>
  96. private static ILogger GetLogger(this KestrelServerOptions kestrel)
  97. {
  98. var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
  99. return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
  100. }
  101. }
  102. }