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($"https://ipinfo.io/{ipstr}??token={token}"); var obj = JsonConvert.DeserializeObject(response) as JObject; resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country"].ToString()); await store.StoreGeoIpRangeAsync(address, address, resolved); return(resolved); } return(resolved); } catch (Exception) { } return(CountryCode.World); }
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); }
public static async Task TestGeoResolve(IAnalyticStore 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"))); }