Esempio n. 1
0
        /// <summary>
        ///     获取延迟
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int Fetch(Models.Server.Server s)
        {
            /*
             * -1  : Not Test
             * -2  : DNS Exception
             * -3  : Exception
             * 999 : Timeout
             */

            try
            {
                var addr = DNS.Fetch(s.Host);
                if (addr == IPAddress.Any)
                {
                    return(-2);
                }

                if (Cache.Contains(addr))
                {
                    var rule = Cache[addr] as CacheEntry;
                    if (DateTimeOffset.Now.ToUnixTimeSeconds() - rule.Unix < 30)
                    {
                        return(rule.Time);
                    }
                    else
                    {
                        Cache.Remove(addr);
                    }
                }

                var time = 0;
                if (Global.Config.Generic.ICMPing)
                {
                    time = ICMPing(addr);
                }
                else
                {
                    return(TCPPing(addr, s.Port));
                }

                Cache.Add(addr, new CacheEntry()
                {
                    Unix = DateTimeOffset.Now.ToUnixTimeSeconds(), Time = time
                });
                return(time);
            }
            catch (Exception e)
            {
                Global.Logger.Warning(e.ToString());
                return(-3);
            }
        }
Esempio n. 2
0
        public static string GetCityCode(string Hostname)
        {
            if (Hostname.Contains(":"))
            {
                Hostname = Hostname.Split(':')[0];
            }

            string Country;

            try
            {
                var databaseReader = new DatabaseReader("bin\\GeoLite2-Country.mmdb");

                if (IPAddress.TryParse(Hostname, out _))
                {
                    Country = databaseReader.Country(Hostname).Country.IsoCode;
                }
                else
                {
                    var DnsResult = DNS.Lookup(Hostname);

                    if (DnsResult != null)
                    {
                        Country = databaseReader.Country(DnsResult).Country.IsoCode;
                    }
                    else
                    {
                        Country = "Unknown";
                    }
                }
            }
            catch (Exception)
            {
                Country = "Unknown";
            }

            return(Country == null ? "Unknown" : Country);
        }