/// <summary> /// Gets the country code by the IP Address. /// </summary> /// <param name="ip">The ip.</param> /// <returns>The ISO Country Code</returns> public string GetCountryCode(string ip) { var resolvedIp = new IP(ip); var country = DataService.Instance.GeoLiteBlocks.FirstOrDefault(g => resolvedIp.ToV4NumericIP() >= g.FirstNumericIP && resolvedIp.ToV4NumericIP() <= g.LastNumericIP); if (country != null) { var location = DataService.Instance.GeoLiteLocations.FirstOrDefault(l => l.Id == country.Country); if (location != null) { return location.CountryName; } } //Log.Error($"found no country for ip {ip}"); return "unknown"; }
/// <summary> /// Initializes a new instance of the <see cref="GeoLiteBlock"/> class. /// </summary> /// <param name="csvLine">The CSV line.</param> public GeoLiteBlock(string csvLine) { var item = (csvLine.Split(',')).ToArray(); //network Network = item[0]; var ip = new IP(item[0]); LastIP = ip.GetEndSegmentBasedOnSubnet(); LastNumericIP = IP.ToV4NumericIP(LastIP); FirstIP = ip.ToIPv4String(); FirstNumericIP = ip.ToV4NumericIP(); //geoname_id int country; if (item[1].Length > 2 && int.TryParse(item[1].Substring(1, item[1].Length - 2), out country)) { Country = country; } //registered_country_geoname_id int regCountry; if (item[2].Length > 2 && int.TryParse(item[2].Substring(1, item[2].Length - 2), out regCountry)) { RegisteredCountry = regCountry; } //represented_country_geoname_id int repCountry; if (item[3].Length > 2 && int.TryParse(item[3].Substring(1, item[3].Length - 2), out repCountry)) { RepresentedCountry = repCountry; } //is_anonymous_proxy if (item[4] == "1") { IsAnonymousProxy = true; } //is_satellite_provider if (item[5] == "1") { IsSatelliteProvider = true; } //Extended Properties if (item.Count() == 9) { //postal_code PostalCode = item[6]; //latitude double lat; if (double.TryParse(item[7].Substring(1, item[7].Length - 2), out lat)) { Latitude = lat; } //longitude double lon; if (double.TryParse(item[8].Substring(1, item[8].Length - 2), out lon)) { Longitude = lon; } } }