KestrelServerOptionsExtensions.cs 3.6 KB

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