KestrelServerOptionsExtensions.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. using System.Security.Authentication;
  11. namespace FastGithub
  12. {
  13. /// <summary>
  14. /// Kestrel扩展
  15. /// </summary>
  16. public static class KestrelServerOptionsExtensions
  17. {
  18. /// <summary>
  19. /// 监听http的反向代理
  20. /// </summary>
  21. /// <param name="kestrel"></param>
  22. public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
  23. {
  24. const int HTTP_PORT = 80;
  25. if (LocalMachine.CanListenTcp(HTTP_PORT) == false)
  26. {
  27. var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
  28. var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
  29. logger.LogWarning($"由于tcp端口{HTTP_PORT}已经被其它进程占用,http反向代理功能将受限");
  30. }
  31. else
  32. {
  33. kestrel.Listen(IPAddress.Any, HTTP_PORT);
  34. }
  35. }
  36. /// <summary>
  37. /// 监听https的反向代理
  38. /// </summary>
  39. /// <param name="kestrel"></param>
  40. public static void ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
  41. {
  42. const int HTTPS_PORT = 443;
  43. if (OperatingSystem.IsWindows())
  44. {
  45. TcpTable.KillPortOwner(HTTPS_PORT);
  46. }
  47. if (LocalMachine.CanListenTcp(HTTPS_PORT) == false)
  48. {
  49. throw new FastGithubException($"由于tcp端口{HTTPS_PORT}已经被其它进程占用,{nameof(FastGithub)}无法进行必须的https反向代理");
  50. }
  51. var certService = kestrel.ApplicationServices.GetRequiredService<CertService>();
  52. certService.CreateCaCertIfNotExists();
  53. certService.InstallAndTrustCaCert();
  54. kestrel.Listen(IPAddress.Any, HTTPS_PORT, listen => listen.UseHttps(https =>
  55. {
  56. if (OperatingSystem.IsWindows() && Environment.OSVersion.Version < new Version(6, 2))
  57. {
  58. https.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13;
  59. }
  60. https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain);
  61. }));
  62. }
  63. /// <summary>
  64. /// 监听github的ssh的代理
  65. /// </summary>
  66. /// <param name="kestrel"></param>
  67. public static void ListenGithubSshProxy(this KestrelServerOptions kestrel)
  68. {
  69. const int SSH_PORT = 22;
  70. if (LocalMachine.CanListenTcp(SSH_PORT) == false)
  71. {
  72. var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
  73. var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
  74. logger.LogWarning($"由于tcp端口{SSH_PORT}已经被其它进程占用,github的ssh代理功能将受限");
  75. }
  76. else
  77. {
  78. kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler<GithubSshHandler>());
  79. }
  80. }
  81. }
  82. }