DnsConfig.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Net;
  3. namespace FastGithub.Configuration
  4. {
  5. /// <summary>
  6. /// dns配置
  7. /// </summary>
  8. public record DnsConfig
  9. {
  10. /// <summary>
  11. /// IP地址
  12. /// </summary>
  13. [AllowNull]
  14. public string IPAddress { get; init; }
  15. /// <summary>
  16. /// 端口
  17. /// </summary>
  18. public int Port { get; init; } = 53;
  19. /// <summary>
  20. /// 转换为IPEndPoint
  21. /// </summary>
  22. /// <returns></returns>
  23. /// <exception cref="FastGithubException"></exception>
  24. public IPEndPoint ToIPEndPoint()
  25. {
  26. if (System.Net.IPAddress.TryParse(this.IPAddress, out var address) == false)
  27. {
  28. throw new FastGithubException($"无效的ip:{this.IPAddress}");
  29. }
  30. if (this.Port == 53 && LocalMachine.ContainsIPAddress(address))
  31. {
  32. throw new FastGithubException($"配置的dns值不能指向{nameof(FastGithub)}自身:{this.IPAddress}:{this.Port}");
  33. }
  34. return new IPEndPoint(address, this.Port);
  35. }
  36. }
  37. }