KestrelServerOptionsExtensions.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 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. kestrel.Limits.MinResponseDataRate = null;
  25. kestrel.Limits.MinRequestBodyDataRate = null;
  26. }
  27. /// <summary>
  28. /// 监听http代理
  29. /// </summary>
  30. /// <param name="kestrel"></param>
  31. public static void ListenHttpProxy(this KestrelServerOptions kestrel)
  32. {
  33. var options = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubOptions>>().Value;
  34. var httpProxyPort = options.HttpProxyPort;
  35. if (GlobalListener.CanListenTcp(httpProxyPort) == false)
  36. {
  37. throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
  38. }
  39. var logger = kestrel.GetLogger();
  40. kestrel.ListenLocalhost(httpProxyPort);
  41. logger.LogInformation($"已监听http://localhost:{httpProxyPort},http代理服务启动完成");
  42. }
  43. /// <summary>
  44. /// 监听ssh反向代理
  45. /// </summary>
  46. /// <param name="kestrel"></param>
  47. public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
  48. {
  49. var sshPort = GlobalListener.SshPort;
  50. kestrel.ListenLocalhost(sshPort, listen =>
  51. {
  52. listen.UseFlowAnalyze();
  53. listen.UseConnectionHandler<GithubSshReverseProxyHandler>();
  54. });
  55. kestrel.GetLogger().LogInformation($"已监听ssh://localhost:{sshPort},github的ssh反向代理服务启动完成");
  56. }
  57. /// <summary>
  58. /// 监听git反向代理
  59. /// </summary>
  60. /// <param name="kestrel"></param>
  61. public static void ListenGitReverseProxy(this KestrelServerOptions kestrel)
  62. {
  63. var gitPort = GlobalListener.GitPort;
  64. kestrel.ListenLocalhost(gitPort, listen =>
  65. {
  66. listen.UseFlowAnalyze();
  67. listen.UseConnectionHandler<GithubGitReverseProxyHandler>();
  68. });
  69. kestrel.GetLogger().LogInformation($"已监听git://localhost:{gitPort},github的git反向代理服务启动完成");
  70. }
  71. /// <summary>
  72. /// 监听http反向代理
  73. /// </summary>
  74. /// <param name="kestrel"></param>
  75. public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
  76. {
  77. var httpPort = GlobalListener.HttpPort;
  78. kestrel.ListenLocalhost(httpPort);
  79. if (OperatingSystem.IsWindows())
  80. {
  81. kestrel.GetLogger().LogInformation($"已监听http://localhost:{httpPort},http反向代理服务启动完成");
  82. }
  83. }
  84. /// <summary>
  85. /// 监听https反向代理
  86. /// </summary>
  87. /// <param name="kestrel"></param>
  88. /// <exception cref="FastGithubException"></exception>
  89. public static void ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
  90. {
  91. var certService = kestrel.ApplicationServices.GetRequiredService<CertService>();
  92. certService.CreateCaCertIfNotExists();
  93. certService.InstallAndTrustCaCert();
  94. var httpsPort = GlobalListener.HttpsPort;
  95. kestrel.ListenLocalhost(httpsPort, listen =>
  96. {
  97. if (OperatingSystem.IsWindows())
  98. {
  99. listen.UseFlowAnalyze();
  100. }
  101. listen.UseHttps(https =>
  102. {
  103. https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain);
  104. });
  105. });
  106. if (OperatingSystem.IsWindows())
  107. {
  108. var logger = kestrel.GetLogger();
  109. logger.LogInformation($"已监听https://localhost:{httpsPort},https反向代理服务启动完成");
  110. }
  111. }
  112. /// <summary>
  113. /// 获取日志
  114. /// </summary>
  115. /// <param name="kestrel"></param>
  116. /// <returns></returns>
  117. private static ILogger GetLogger(this KestrelServerOptions kestrel)
  118. {
  119. var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
  120. return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(HttpServer)}");
  121. }
  122. }
  123. }