Ejemplo n.º 1
0
        public static string GetLocIp()
        {
            try
            {
                using (TcpClient tcpClient = new TcpClient())
                {
                    if (DnsSettings.HttpsDnsUrl.Split('/')[2].Contains(":"))
                    {
                        tcpClient.Connect(DnsSettings.HttpsDnsUrl.Split('/', ':')[3],
                                          Convert.ToInt32(DnsSettings.HttpsDnsUrl.Split('/', ':')[4]));
                    }
                    else
                    {
                        tcpClient.Connect(DnsSettings.HttpsDnsUrl.Split('/')[2], 443);
                    }

                    return(((IPEndPoint)tcpClient.Client.LocalEndPoint).Address.ToString());
                }
            }
            catch (Exception e)
            {
                MyTools.BgwLog("Try Connect:" + e);
                return(MessageBox.Show(
                           $"Error: 尝试连接远端 DNS over HTTPS 服务器发生错误{Environment.NewLine}点击“确定”以重试连接,点击“取消”放弃连接使用预设地址。{Environment.NewLine}Original error: "
                           + e.Message, @"错误", MessageBoxButton.OKCancel) == MessageBoxResult.OK
                    ? GetLocIp() : "192.168.0.1");
            }
        }
Ejemplo n.º 2
0
        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 if (locStr.Contains("\"0\""))
                {
                    countryCode = json.AsObjectGetString("0");
                }
                else
                {
                    countryCode = "Unknown";
                }

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

                if (onlyCountryCode)
                {
                    return(countryCode);
                }
                return(countryCode + ", " + organization);
            }
            catch (Exception e)
            {
                MyTools.BgwLog(@"| DownloadString failed : " + e);
                return(null);
            }
        }
Ejemplo n.º 3
0
 public static string GetIntIp()
 {
     try
     {
         //Thread.CurrentThread.CurrentCulture.Name == "zh-CN"
         return(new WebClient().DownloadString(UrlSettings.WhatMyIpApi).Trim());
     }
     catch (Exception e)
     {
         MyTools.BgwLog("Try Connect:" + e);
         return(MessageBox.Show($"Error: 尝试获取公网IP地址失败{Environment.NewLine}点击“确定”以重试连接,点击“取消”放弃连接使用预设地址。{Environment.NewLine}Original error: "
                                + e.Message, @"错误", MessageBoxButton.OKCancel) == MessageBoxResult.OK
             ? GetIntIp() : IPAddress.Any.ToString());
     }
 }
Ejemplo n.º 4
0
 public static string GeoIpLocal(string ipStr)
 {
     try
     {
         string locStr = new WebClient().DownloadString(IsIp(ipStr) ? $"https://api.ip.sb/geoip/{ipStr}" :
                                                        $"https://api.ip.sb/geoip/{Dns.GetHostAddresses(ipStr)[0]}");
         JsonValue json = Json.Parse(locStr);
         return(json.AsObjectGetString("country_code") + ", " + json.AsObjectGetString("organization"));
     }
     catch (Exception e)
     {
         MyTools.BgwLog(@"| DownloadString failed : " + e);
         return(null);
     }
 }
Ejemplo n.º 5
0
 public static string GetIntIp()
 {
     try
     {
         //Thread.CurrentThread.CurrentCulture.Name == "zh-CN"
         return(TimeZoneInfo.Local.Id.Contains("China Standard Time")
             ? new WebClient().DownloadString("http://members.3322.org/dyndns/getip").Trim()
             : new WebClient().DownloadString("http://whatismyip.akamai.com/").Trim());
     }
     catch (Exception e)
     {
         MyTools.BgwLog("Try Connect:" + e);
         return(MessageBox.Show($"Error: 尝试获取公网IP地址失败{Environment.NewLine}点击“确定”以重试连接,点击“取消”放弃连接使用预设地址。{Environment.NewLine}Original error: "
                                + e.Message, @"错误", MessageBoxButton.OKCancel) == MessageBoxResult.OK
             ? GetIntIp() : IPAddress.Any.ToString());
     }
 }