精确判断主DNS
This commit is contained in:
		
							parent
							
								
									8e8ad9f69e
								
							
						
					
					
						commit
						2324634a86
					
				@ -57,7 +57,7 @@ namespace FastGithub.Dns
 | 
				
			|||||||
            {
 | 
					            {
 | 
				
			||||||
                try
 | 
					                try
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    SystemDnsUtil.SetPrimitiveDns(IPAddress.Loopback);
 | 
					                    SystemDnsUtil.SetAsPrimitiveDns();
 | 
				
			||||||
                    SystemDnsUtil.FlushResolverCache();
 | 
					                    SystemDnsUtil.FlushResolverCache();
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                catch (Exception ex)
 | 
					                catch (Exception ex)
 | 
				
			||||||
@ -100,7 +100,7 @@ namespace FastGithub.Dns
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            try
 | 
					            try
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                SystemDnsUtil.RemovePrimitiveDns(IPAddress.Loopback);
 | 
					                SystemDnsUtil.RemoveFromPrimitiveDns();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            catch (Exception ex)
 | 
					            catch (Exception ex)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
 | 
				
			|||||||
@ -15,11 +15,6 @@ namespace FastGithub.Dns
 | 
				
			|||||||
    /// </summary> 
 | 
					    /// </summary> 
 | 
				
			||||||
    static class SystemDnsUtil
 | 
					    static class SystemDnsUtil
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        /// <summary>
 | 
					 | 
				
			||||||
        /// www.baidu.com的ip
 | 
					 | 
				
			||||||
        /// </summary>
 | 
					 | 
				
			||||||
        private static readonly IPAddress www_baidu_com = IPAddress.Parse("183.232.231.172");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 刷新DNS缓存
 | 
					        /// 刷新DNS缓存
 | 
				
			||||||
        /// </summary>
 | 
					        /// </summary>
 | 
				
			||||||
@ -39,21 +34,22 @@ namespace FastGithub.Dns
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 设置主dns
 | 
					        /// 设置为主dns
 | 
				
			||||||
        /// </summary> 
 | 
					        /// </summary> 
 | 
				
			||||||
        /// <param name="primitive"></param>
 | 
					 | 
				
			||||||
        /// <exception cref="FastGithubException"></exception> 
 | 
					        /// <exception cref="FastGithubException"></exception> 
 | 
				
			||||||
        public static void SetPrimitiveDns(IPAddress primitive)
 | 
					        public static void SetAsPrimitiveDns()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            var @interface = GetOutboundNetworkInterface();
 | 
					            var @interface = GetOutboundNetworkInterface();
 | 
				
			||||||
            if (@interface == null)
 | 
					            if (@interface == null)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                throw new FastGithubException($"找不到匹配的网络适配器来设置主DNS值:{primitive}");
 | 
					                throw new FastGithubException($"找不到匹配的网络适配器来设置主DNS");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var dnsAddresses = @interface.GetIPProperties().DnsAddresses;
 | 
					            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);
 | 
					                var nameServers = dnsAddresses.Prepend(primitive);
 | 
				
			||||||
                if (OperatingSystem.IsWindows())
 | 
					                if (OperatingSystem.IsWindows())
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
@ -71,26 +67,34 @@ namespace FastGithub.Dns
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// <summary>
 | 
					        /// <summary>
 | 
				
			||||||
        /// 移除主dns
 | 
					        /// 从主dns移除
 | 
				
			||||||
        /// </summary> 
 | 
					        /// </summary> 
 | 
				
			||||||
        /// <param name="primitive"></param>
 | 
					 | 
				
			||||||
        /// <exception cref="FastGithubException"></exception> 
 | 
					        /// <exception cref="FastGithubException"></exception> 
 | 
				
			||||||
        public static void RemovePrimitiveDns(IPAddress primitive)
 | 
					        public static void RemoveFromPrimitiveDns()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            var @interface = GetOutboundNetworkInterface();
 | 
					            var @interface = GetOutboundNetworkInterface();
 | 
				
			||||||
            if (@interface == null)
 | 
					            if (@interface == null)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                throw new FastGithubException($"找不到匹配的网络适配器来移除主DNS值:{primitive}");
 | 
					                throw new FastGithubException($"找不到匹配的网络适配器来移除主DNS");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var dnsAddresses = @interface.GetIPProperties().DnsAddresses;
 | 
					            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);
 | 
					                var nameServers = dnsAddresses.Skip(1);
 | 
				
			||||||
                if (OperatingSystem.IsWindows())
 | 
					                if (OperatingSystem.IsWindows())
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    SetNameServers(@interface, nameServers);
 | 
					                    SetNameServers(@interface, nameServers);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					                else if (OperatingSystem.IsLinux())
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    throw new FastGithubException($"不支持自动移除本机主DNS,请手工移除/etc/resolv.conf的第一条记录");
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                else if (OperatingSystem.IsMacOS())
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    throw new FastGithubException($"不支持自动移除本机主DNS,请手工移除连接网络的DNS的第一条记录");
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -101,7 +105,7 @@ namespace FastGithub.Dns
 | 
				
			|||||||
        /// <returns></returns> 
 | 
					        /// <returns></returns> 
 | 
				
			||||||
        private static NetworkInterface? GetOutboundNetworkInterface()
 | 
					        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);
 | 
					            var address = LocalMachine.GetLocalIPAddress(remoteEndPoint);
 | 
				
			||||||
            if (address == null)
 | 
					            if (address == null)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user