Skip to content

Commit

Permalink
精确判断主DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Aug 27, 2021
1 parent 8e8ad9f commit 2324634
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions FastGithub.Dns/DnsOverUdpHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override async Task StartAsync(CancellationToken cancellationToken)
{
try
{
SystemDnsUtil.SetPrimitiveDns(IPAddress.Loopback);
SystemDnsUtil.SetAsPrimitiveDns();
SystemDnsUtil.FlushResolverCache();
}
catch (Exception ex)
Expand Down Expand Up @@ -100,7 +100,7 @@ public override Task StopAsync(CancellationToken cancellationToken)

try
{
SystemDnsUtil.RemovePrimitiveDns(IPAddress.Loopback);
SystemDnsUtil.RemoveFromPrimitiveDns();
}
catch (Exception ex)
{
Expand Down
40 changes: 22 additions & 18 deletions FastGithub.Dns/SystemDnsUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ namespace FastGithub.Dns
/// </summary>
static class SystemDnsUtil
{
/// <summary>
/// www.baidu.com的ip
/// </summary>
private static readonly IPAddress www_baidu_com = IPAddress.Parse("183.232.231.172");

/// <summary>
/// 刷新DNS缓存
/// </summary>
Expand All @@ -39,21 +34,22 @@ public static void FlushResolverCache()
}

/// <summary>
/// 设置主dns
/// </summary>
/// <param name="primitive"></param>
/// 设置为主dns
/// </summary>
/// <exception cref="FastGithubException"></exception>
public static void SetPrimitiveDns(IPAddress primitive)
public static void SetAsPrimitiveDns()
{
var @interface = GetOutboundNetworkInterface();
if (@interface == null)
{
throw new FastGithubException($"找不到匹配的网络适配器来设置主DNS值:{primitive}");
throw new FastGithubException($"找不到匹配的网络适配器来设置主DNS");
}

var dnsAddresses = @interface.GetIPProperties().DnsAddresses;
if (primitive.Equals(dnsAddresses.FirstOrDefault()) == false)
var firstRecord = dnsAddresses.FirstOrDefault();
if (firstRecord == null || LocalMachine.ContainsIPAddress(firstRecord) == false)
{
var primitive = IPAddress.Loopback;
var nameServers = dnsAddresses.Prepend(primitive);
if (OperatingSystem.IsWindows())
{
Expand All @@ -71,26 +67,34 @@ public static void SetPrimitiveDns(IPAddress primitive)
}

/// <summary>
/// 移除主dns
/// </summary>
/// <param name="primitive"></param>
/// 从主dns移除
/// </summary>
/// <exception cref="FastGithubException"></exception>
public static void RemovePrimitiveDns(IPAddress primitive)
public static void RemoveFromPrimitiveDns()
{
var @interface = GetOutboundNetworkInterface();
if (@interface == null)
{
throw new FastGithubException($"找不到匹配的网络适配器来移除主DNS值:{primitive}");
throw new FastGithubException($"找不到匹配的网络适配器来移除主DNS");
}

var dnsAddresses = @interface.GetIPProperties().DnsAddresses;
if (primitive.Equals(dnsAddresses.FirstOrDefault()))
var firstRecord = dnsAddresses.FirstOrDefault();
if (firstRecord != null && LocalMachine.ContainsIPAddress(firstRecord))
{
var nameServers = dnsAddresses.Skip(1);
if (OperatingSystem.IsWindows())
{
SetNameServers(@interface, nameServers);
}
else if (OperatingSystem.IsLinux())
{
throw new FastGithubException($"不支持自动移除本机主DNS,请手工移除/etc/resolv.conf的第一条记录");
}
else if (OperatingSystem.IsMacOS())
{
throw new FastGithubException($"不支持自动移除本机主DNS,请手工移除连接网络的DNS的第一条记录");
}
}
}

Expand All @@ -101,7 +105,7 @@ public static void RemovePrimitiveDns(IPAddress primitive)
/// <returns></returns>
private static NetworkInterface? GetOutboundNetworkInterface()
{
var remoteEndPoint = new IPEndPoint(www_baidu_com, 443);
var remoteEndPoint = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 53);
var address = LocalMachine.GetLocalIPAddress(remoteEndPoint);
if (address == null)
{
Expand Down

0 comments on commit 2324634

Please sign in to comment.