KestrelServerExtensions.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using FastGithub.Configuration;
  2. using FastGithub.HttpServer.Certs;
  3. using FastGithub.HttpServer.TcpMiddlewares;
  4. using FastGithub.HttpServer.TlsMiddlewares;
  5. using Microsoft.AspNetCore.Connections;
  6. using Microsoft.AspNetCore.Hosting;
  7. using Microsoft.AspNetCore.Server.Kestrel.Core;
  8. using Microsoft.AspNetCore.Server.Kestrel.Https;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.Extensions.Logging;
  11. using Microsoft.Extensions.Options;
  12. using System;
  13. namespace FastGithub
  14. {
  15. /// <summary>
  16. /// Kestrel扩展
  17. /// </summary>
  18. public static class KestrelServerExtensions
  19. {
  20. /// <summary>
  21. /// 无限制
  22. /// </summary>
  23. /// <param name="kestrel"></param>
  24. public static void NoLimit(this KestrelServerOptions kestrel)
  25. {
  26. kestrel.Limits.MaxRequestBodySize = null;
  27. kestrel.Limits.MinResponseDataRate = null;
  28. kestrel.Limits.MinRequestBodyDataRate = null;
  29. }
  30. /// <summary>
  31. /// 监听http代理
  32. /// </summary>
  33. /// <param name="kestrel"></param>
  34. public static void ListenHttpProxy(this KestrelServerOptions kestrel)
  35. {
  36. var options = kestrel.ApplicationServices.GetRequiredService<IOptions<FastGithubOptions>>().Value;
  37. var httpProxyPort = options.HttpProxyPort;
  38. if (GlobalListener.CanListenTcp(httpProxyPort) == false)
  39. {
  40. throw new FastGithubException($"tcp端口{httpProxyPort}已经被其它进程占用,请在配置文件更换{nameof(FastGithubOptions.HttpProxyPort)}为其它端口");
  41. }
  42. kestrel.ListenLocalhost(httpProxyPort, listen =>
  43. {
  44. var proxyMiddleware = kestrel.ApplicationServices.GetRequiredService<HttpProxyMiddleware>();
  45. var tunnelMiddleware = kestrel.ApplicationServices.GetRequiredService<TunnelMiddleware>();
  46. listen.Use(next => context => proxyMiddleware.InvokeAsync(next, context));
  47. listen.UseTls();
  48. listen.Use(next => context => tunnelMiddleware.InvokeAsync(next, context));
  49. });
  50. kestrel.GetLogger().LogInformation($"已监听http://localhost:{httpProxyPort},http代理服务启动完成");
  51. }
  52. /// <summary>
  53. /// 监听ssh协议代理
  54. /// </summary>
  55. /// <param name="kestrel"></param>
  56. public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
  57. {
  58. var sshPort = GlobalListener.SshPort;
  59. kestrel.ListenLocalhost(sshPort, listen =>
  60. {
  61. listen.UseFlowAnalyze();
  62. listen.UseConnectionHandler<GithubSshReverseProxyHandler>();
  63. });
  64. kestrel.GetLogger().LogInformation($"已监听ssh://localhost:{sshPort},github的ssh反向代理服务启动完成");
  65. }
  66. /// <summary>
  67. /// 监听git协议代理代理
  68. /// </summary>
  69. /// <param name="kestrel"></param>
  70. public static void ListenGitReverseProxy(this KestrelServerOptions kestrel)
  71. {
  72. var gitPort = GlobalListener.GitPort;
  73. kestrel.ListenLocalhost(gitPort, listen =>
  74. {
  75. listen.UseFlowAnalyze();
  76. listen.UseConnectionHandler<GithubGitReverseProxyHandler>();
  77. });
  78. kestrel.GetLogger().LogInformation($"已监听git://localhost:{gitPort},github的git反向代理服务启动完成");
  79. }
  80. /// <summary>
  81. /// 监听http反向代理
  82. /// </summary>
  83. /// <param name="kestrel"></param>
  84. public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
  85. {
  86. var httpPort = GlobalListener.HttpPort;
  87. kestrel.ListenLocalhost(httpPort);
  88. if (OperatingSystem.IsWindows())
  89. {
  90. kestrel.GetLogger().LogInformation($"已监听http://localhost:{httpPort},http反向代理服务启动完成");
  91. }
  92. }
  93. /// <summary>
  94. /// 监听https反向代理
  95. /// </summary>
  96. /// <param name="kestrel"></param>
  97. /// <exception cref="FastGithubException"></exception>
  98. public static void ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
  99. {
  100. var httpsPort = GlobalListener.HttpsPort;
  101. kestrel.ListenLocalhost(httpsPort, listen =>
  102. {
  103. if (OperatingSystem.IsWindows())
  104. {
  105. listen.UseFlowAnalyze();
  106. }
  107. listen.UseTls();
  108. });
  109. if (OperatingSystem.IsWindows())
  110. {
  111. var logger = kestrel.GetLogger();
  112. logger.LogInformation($"已监听https://localhost:{httpsPort},https反向代理服务启动完成");
  113. }
  114. }
  115. /// <summary>
  116. /// 获取日志
  117. /// </summary>
  118. /// <param name="kestrel"></param>
  119. /// <returns></returns>
  120. private static ILogger GetLogger(this KestrelServerOptions kestrel)
  121. {
  122. var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
  123. return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(HttpServer)}");
  124. }
  125. /// <summary>
  126. /// 使用Tls中间件
  127. /// </summary>
  128. /// <param name="listen"></param>
  129. /// <param name="configureOptions">https配置</param>
  130. /// <returns></returns>
  131. public static ListenOptions UseTls(this ListenOptions listen)
  132. {
  133. var certService = listen.ApplicationServices.GetRequiredService<CertService>();
  134. certService.CreateCaCertIfNotExists();
  135. certService.InstallAndTrustCaCert();
  136. return listen.UseTls(https => https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain));
  137. }
  138. /// <summary>
  139. /// 使用Tls中间件
  140. /// </summary>
  141. /// <param name="listen"></param>
  142. /// <param name="configureOptions">https配置</param>
  143. /// <returns></returns>
  144. private static ListenOptions UseTls(this ListenOptions listen, Action<HttpsConnectionAdapterOptions> configureOptions)
  145. {
  146. var invadeMiddleware = listen.ApplicationServices.GetRequiredService<TlsInvadeMiddleware>();
  147. var restoreMiddleware = listen.ApplicationServices.GetRequiredService<TlsRestoreMiddleware>();
  148. listen.Use(next => context => invadeMiddleware.InvokeAsync(next, context));
  149. listen.UseHttps(configureOptions);
  150. listen.Use(next => context => restoreMiddleware.InvokeAsync(next, context));
  151. return listen;
  152. }
  153. }
  154. }