コード例 #1
0
        public static void ResetDnsCmd()
        {
            var backupDnsAddr = Equals(DnsSettings.SecondDnsIp, IPAddress.Any)
                ? "" : DnsSettings.SecondDnsIp.ToString();
            var cmd = "";

            foreach (var network in GetAllNetworkInterfaces())
            {
                if (network.OperationalStatus != Up)
                {
                    continue;
                }
                cmd += $"netsh interface ipv6 delete dns \"{network.Name}\" all" + Environment.NewLine;
                cmd += $"netsh interface ip delete dns \"{network.Name}\" all" + Environment.NewLine;
                cmd += $"netsh interface ip set dns \"{network.Name}\" source=dhcp" + Environment.NewLine;
                try
                {
                    if (!network.GetIPProperties().GetIPv4Properties().IsDhcpEnabled)
                    {
                        cmd +=
                            $"netsh interface ip set dns \"{network.Name}\" source=static addr={backupDnsAddr} validate=no" +
                            Environment.NewLine;
                    }
                }
                catch (Exception e)
                {
                    MyTools.BackgroundLog(network.Name + e);
                }
            }

            var filename = Path.GetTempPath() + "setdns.cmd";

            File.Create(filename).Close();
            File.WriteAllText(filename, cmd, Encoding.Default);
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName    = filename,
                Verb        = "runas",
                WindowStyle = ProcessWindowStyle.Hidden
            };

            Process.Start(startInfo).Exited += (o, args) => { File.Delete(filename); };
        }
コード例 #2
0
        public static IPAddress ResolveNameIpAddress(string name)
        {
            if (IPAddress.TryParse(name.TrimEnd('.'), out _))
            {
                return(IPAddress.Parse(name.TrimEnd('.')));
            }
            while (true)
            {
                try
                {
                    DnsRecordBase ipMsg;
                    if (DnsSettings.StartupOverDoH)
                    {
                        ipMsg = QueryResolve.ResolveOverHttpsByDnsJson(IPAddress.Any.ToString(),
                                                                       name, "https://1.0.0.1/dns-query", DnsSettings.ProxyEnable, DnsSettings.WProxy).list[0];
                    }
                    else
                    {
                        ipMsg = new DnsClient(DnsSettings.SecondDnsIp, 5000).Resolve(DomainName.Parse(name))
                                .AnswerRecords[0];
                    }

                    if (ipMsg.RecordType == RecordType.A && ipMsg is ARecord msg1)
                    {
                        return(msg1.Address);
                    }
                    if (ipMsg.RecordType == RecordType.CName)
                    {
                        if (ipMsg is CNameRecord msg)
                        {
                            name = msg.CanonicalName.ToString();
                        }
                    }
                }
                catch (Exception e)
                {
                    MyTools.BackgroundLog(e.ToString());
                }
            }
        }
コード例 #3
0
ファイル: IpTools.cs プロジェクト: wenzijishu/AuroraDNS.GUI
 public static string GetIntIp()
 {
     try
     {
         return(new MyCurl.MWebClient().DownloadString(UrlSettings.WhatMyIpApi).Trim());
     }
     catch (Exception e)
     {
         MyTools.BackgroundLog("Try Connect:" + e);
         try
         {
             return(new MyCurl.MIpBkWebClient().DownloadString(UrlSettings.WhatMyIpApi).Trim());
         }
         catch (Exception exception)
         {
             MyTools.BackgroundLog("Try Connect:" + exception);
             return(MessageBox.Show($"Error: 尝试获取公网IP地址失败(WhatMyIP-API){Environment.NewLine}点击“确定”以重试连接,点击“取消”放弃连接使用预设地址。{Environment.NewLine}Original error: "
                                    + exception.Message, @"错误", MessageBoxButton.OKCancel) == MessageBoxResult.OK
                 ? GetIntIp() : IPAddress.Any.ToString());
         }
     }
 }
コード例 #4
0
ファイル: IpTools.cs プロジェクト: zxysm/AuroraDNS.GUI
        public static string GetLocIp()
        {
            var tcpClient = new TcpClient {
                ReceiveTimeout = 2500, SendTimeout = 2500
            };
            var addressUri = new Uri(DnsSettings.HttpsDnsUrl);

            try
            {
                tcpClient.Connect(addressUri.DnsSafeHost, addressUri.Port);
                return(((IPEndPoint)tcpClient.Client.LocalEndPoint).Address.ToString());
            }
            catch (Exception e)
            {
                MyTools.BackgroundLog("Try Connect:" + e);
                try
                {
                    try
                    {
                        tcpClient.Connect(ResolveNameIpAddress(addressUri.DnsSafeHost), addressUri.Port);
                        return(((IPEndPoint)tcpClient.Client.LocalEndPoint).Address.ToString());
                    }
                    catch
                    {
                        addressUri = new Uri(DnsSettings.SecondHttpsDnsUrl);
                        tcpClient.Connect(ResolveNameIpAddress(addressUri.DnsSafeHost), addressUri.Port);
                        return(((IPEndPoint)tcpClient.Client.LocalEndPoint).Address.ToString());
                    }
                }
                catch
                {
                    return(MessageBox.Show(
                               $"Error: 尝试连接远端 DNS over HTTPS 服务器发生错误{Environment.NewLine}点击“确定”以重试连接,点击“取消”放弃连接使用备用 DNS 服务器测试。" +
                               $"{Environment.NewLine}Original error: " + e.Message, @"错误", MessageBoxButton.OKCancel) == MessageBoxResult.OK
                        ? GetLocIp() : GetLocIpUdp());
                }
            }
        }
コード例 #5
0
ファイル: IpTools.cs プロジェクト: wenzijishu/AuroraDNS.GUI
        public static string GeoIpLocal(string ipStr, bool onlyCountryCode = false)
        {
            try
            {
                string locStr = new WebClient().DownloadString(IsIp(ipStr)
                    ? $"{UrlSettings.GeoIpApi}{ipStr}": $"{UrlSettings.GeoIpApi}{Dns.GetHostAddresses(ipStr)[0]}");
                JsonValue json = Json.Parse(locStr);

                string countryCode;
                string organization;

                if (locStr.Contains("\"country_code\""))
                {
                    countryCode = json.AsObjectGetString("country_code");
                }
                else if (locStr.Contains("\"countryCode\""))
                {
                    countryCode = json.AsObjectGetString("countryCode");
                }
                else
                {
                    countryCode = "";
                }

                if (locStr.Contains("\"organization\""))
                {
                    organization = json.AsObjectGetString("organization");
                }
                else if (locStr.Contains("\"as\""))
                {
                    organization = json.AsObjectGetString("as");
                }
                else if (locStr.Contains("\"org\""))
                {
                    organization = json.AsObjectGetString("org");
                }
                else
                {
                    organization = "";
                }

                if (!organization.ToUpper().Contains("AS") && locStr.Contains("\"asn\""))
                {
                    try
                    {
                        organization = $"AS{json.AsObjectGetInt("asn")} {organization}";
                    }
                    catch
                    {
                        organization = $"AS{json.AsObjectGetString("asn")} {organization}";
                    }
                }

                if (onlyCountryCode)
                {
                    return(countryCode);
                }
                return(countryCode + " " + organization);
            }
            catch (Exception e)
            {
                MyTools.BackgroundLog(@"| DownloadString failed : " + e);
                return(null);
            }
        }