PortService.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using FastGithub.Configuration;
  2. using Microsoft.Extensions.Options;
  3. using System;
  4. using System.Net.Sockets;
  5. namespace FastGithub.ReverseProxy
  6. {
  7. /// <summary>
  8. /// 端口管理服务
  9. /// </summary>
  10. public class PortService
  11. {
  12. private int httpsReverseProxyPort = -1;
  13. /// <summary>
  14. /// http代理端口
  15. /// </summary>
  16. public int HttpProxyPort { get; }
  17. /// <summary>
  18. /// 获取https反向代理端口
  19. /// </summary>
  20. public int HttpsReverseProxyPort
  21. {
  22. get
  23. {
  24. if (OperatingSystem.IsWindows())
  25. {
  26. return 443;
  27. }
  28. if (this.httpsReverseProxyPort < 0)
  29. {
  30. this.httpsReverseProxyPort = LocalMachine.GetAvailablePort(AddressFamily.InterNetwork);
  31. }
  32. return this.httpsReverseProxyPort;
  33. }
  34. }
  35. /// <summary>
  36. /// 端口管理服务
  37. /// </summary>
  38. /// <param name="options"></param>
  39. public PortService(IOptions<FastGithubOptions> options)
  40. {
  41. this.HttpProxyPort = options.Value.HttpProxyPort;
  42. }
  43. }
  44. }