2
0

KestrelServerOptionsExtensions.cs 3.4 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. 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. /// 监听ssh
  27. /// </summary>
  28. /// <param name="kestrel"></param>
  29. public static void ListenSsh(this KestrelServerOptions kestrel)
  30. {
  31. const int SSH_PORT = 22;
  32. if (LocalMachine.CanListenTcp(SSH_PORT) == true)
  33. {
  34. kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler<GithubSshProxyHandler>());
  35. kestrel.GetLogger().LogInformation($"已监听tcp端口{SSH_PORT},github的ssh代理启动完成");
  36. }
  37. }
  38. /// <summary>
  39. /// 监听http
  40. /// </summary>
  41. /// <param name="kestrel"></param>
  42. public static void ListenHttp(this KestrelServerOptions kestrel)
  43. {
  44. const int HTTP_PORT = 80;
  45. if (LocalMachine.CanListenTcp(HTTP_PORT) == true)
  46. {
  47. kestrel.Listen(IPAddress.Any, HTTP_PORT);
  48. kestrel.GetLogger().LogInformation($"已监听tcp端口{HTTP_PORT},http反向代理启动完成");
  49. }
  50. }
  51. /// <summary>
  52. /// 监听https
  53. /// </summary>
  54. /// <param name="kestrel"></param>
  55. /// <exception cref="FastGithubException"></exception>
  56. public static void ListenHttps(this KestrelServerOptions kestrel)
  57. {
  58. const int HTTPS_PORT = 443;
  59. if (OperatingSystem.IsWindows())
  60. {
  61. TcpTable.KillPortOwner(HTTPS_PORT);
  62. }
  63. if (LocalMachine.CanListenTcp(HTTPS_PORT) == false)
  64. {
  65. throw new FastGithubException($"tcp端口{HTTPS_PORT}已经被其它进程占用");
  66. }
  67. var certService = kestrel.ApplicationServices.GetRequiredService<CertService>();
  68. certService.CreateCaCertIfNotExists();
  69. certService.InstallAndTrustCaCert();
  70. kestrel.Listen(IPAddress.Any, HTTPS_PORT,
  71. listen => listen.UseHttps(https =>
  72. https.ServerCertificateSelector = (ctx, domain) =>
  73. certService.GetOrCreateServerCert(domain)));
  74. var logger = kestrel.GetLogger();
  75. logger.LogInformation($"已监听tcp端口{HTTPS_PORT},https反向代理启动完成");
  76. }
  77. /// <summary>
  78. /// 获取日志
  79. /// </summary>
  80. /// <param name="kestrel"></param>
  81. /// <returns></returns>
  82. private static ILogger GetLogger(this KestrelServerOptions kestrel)
  83. {
  84. var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
  85. return loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
  86. }
  87. }
  88. }