コード例 #1
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _resolver.ResolveCountryCodeAsync(address).ConfigureAwait(true);

                if (resolved == CountryCode.World)
                {
                    var uri = new Uri($"https://ipinfo.io/{address}?token={_token}");

                    var obj = await LoadJson(uri).ConfigureAwait(true);

                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country"].ToString());

                    await _resolver.StoreGeoIpRangeAsync(address, address, resolved).ConfigureAwait(false);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
            }
            return(CountryCode.World);
        }
コード例 #2
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _store.ResolveCountryCodeAsync(address);

                if (resolved == CountryCode.World)
                {
                    var ipstr    = address.ToString();
                    var response = await(new HttpClient()).GetStringAsync($"http://api.ipstack.com/{ipstr}?access_key={_accessKey}&format=1");

                    var obj = JsonConvert.DeserializeObject(response) as JObject;
                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country_code"].ToString());

                    await _store.StoreGeoIpRangeAsync(address, address, resolved);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
            }
            return(CountryCode.World);
        }
コード例 #3
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _resolver.ResolveCountryCodeAsync(address);

                if (resolved == CountryCode.World)
                {
                    var ipstr    = address.ToString();
                    var response = await(new HttpClient()).GetStringAsync($"https://ipinfo.io/{ipstr}??token={_token}");

                    var obj = JsonConvert.DeserializeObject(response) as JObject;
                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country"].ToString());

                    await _resolver.StoreGeoIpRangeAsync(address, address, resolved);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
            }
            return(CountryCode.World);
        }
コード例 #4
0
        public static async Task TestGeoResolve(IGeoIpResolver store)
        {
            await store.StoreGeoIpRangeAsync(IPAddress.Parse("86.44.0.0"), IPAddress.Parse("86.49.47.255"),
                                             CountryCode.Cz);

            await store.StoreGeoIpRangeAsync(IPAddress.Parse("85.44.0.0"), IPAddress.Parse("86.43.255.255"),
                                             CountryCode.Sk);

            await store.StoreGeoIpRangeAsync(IPAddress.Parse("86.49.48.0"), IPAddress.Parse("86.86.255.255"),
                                             CountryCode.It);

            Assert.Equal(CountryCode.Cz, await store.ResolveCountryCodeAsync(IPAddress.Parse("86.49.47.89")));
        }
コード例 #5
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _resolver.ResolveCountryCodeAsync(address).ConfigureAwait(true);

                if (resolved == CountryCode.World)
                {
                    var obj = await LoadJson(new Uri($"http://ip-api.com/json/{address}")).ConfigureAwait(true);

                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country_code"].ToString());

                    await _resolver.StoreGeoIpRangeAsync(address, address, resolved).ConfigureAwait(false);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
                return(CountryCode.World);
            }
        }
コード例 #6
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _store.ResolveCountryCodeAsync(address).ConfigureAwait(true);

                if (resolved == CountryCode.World)
                {
                    var obj = await LoadJson(new Uri($"http://api.ipstack.com/{address}?access_key={_accessKey}&format=1")).ConfigureAwait(true);

                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country_code"].ToString());

                    await _store.StoreGeoIpRangeAsync(address, address, resolved).ConfigureAwait(false);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
            }
            return(CountryCode.World);
        }