using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
namespace FastGithub.Configuration
{
///
/// dns配置
///
public class DnsConfig
{
///
/// IP地址
///
[AllowNull]
public string IPAddress { get; set; }
///
/// 端口
///
public int Port { get; set; } = 53;
///
/// 转换为IPEndPoint
///
///
///
public IPEndPoint ToIPEndPoint()
{
if (System.Net.IPAddress.TryParse(this.IPAddress, out var address) == false)
{
throw new FastGithubException($"无效的ip:{this.IPAddress}");
}
if (this.Port == 53 && IsLocalIPAddress(address))
{
throw new FastGithubException($"配置的dns值不能指向{nameof(FastGithub)}自身:{this.IPAddress}:{this.Port}");
}
return new IPEndPoint(address, this.Port);
}
public override string ToString()
{
return $"{this.IPAddress}:{this.Port}";
}
///
/// 是否为本机ip
///
///
///
private static bool IsLocalIPAddress(IPAddress address)
{
if (address.Equals(System.Net.IPAddress.Loopback))
{
return true;
}
if (address.Equals(System.Net.IPAddress.IPv6Loopback))
{
return true;
}
var addresses = Dns.GetHostAddresses(Dns.GetHostName());
return addresses.Contains(address);
}
}
}