/// <summary> /// Connects to a third party service ipinfo.io and obtains information about IP address location. /// </summary> /// <param name="IpAddress">IP address to find location for.</param> /// <returns>Estimated GPS location of the machine or null if the funtion fails.</returns> public static GpsLocation FindIpLocationIpinfoIo(IPAddress IpAddress) { log.Trace("(IpAddress:{0})", IpAddress); GpsLocation res = null; string uri = string.Format(LocationServiceUri, IpAddress); log.Trace("Sending request to '{0}'.", uri); string response = HttpGet(uri); if (response != null) { try { IpinfoIoResult ipInfo = JsonConvert.DeserializeObject <IpinfoIoResult>(response); if (ipInfo.loc != null) { string[] parts = ipInfo.loc.Split(new char[] { ',', ';' }); if (parts.Length == 2) { decimal lat; decimal lon; if (decimal.TryParse(parts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out lat) && decimal.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out lon)) { GpsLocation location = new GpsLocation(lat, lon); if (location.IsValid()) { res = location; } } } } if (res == null) { log.Error("Invalid location information '{0}' received.", ipInfo.loc); } } catch { log.Error("Received invalid response from '{0}':\n{1}", uri, response); } } else { log.Error("Request to '{0}' failed.", uri); } log.Trace("(-):{0}", res); return(res); }
/// <summary> /// Connects to a seed node and obtains IP address location information. /// </summary> /// <param name="IpAddress">IP address to find location for.</param> /// <returns>Estimated GPS location of the machine or null if the funtion fails.</returns> public static GpsLocation FindIpLocation(IPAddress IpAddress) { log.Trace("(IpAddress:{0})", IpAddress); GpsLocation res = null; foreach (string seedNode in SeedNodes) { string uri = string.Format(LocationUri, seedNode, IpAddress); log.Trace("Sending request to '{0}'.", uri); string response = HttpGet(uri); if (response != null) { response = response.Trim(); string[] parts = response.Split(new char[] { ';', ',' }); if (parts.Length == 2) { decimal lat; decimal lon; if (decimal.TryParse(parts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out lat) && decimal.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out lon)) { GpsLocation location = new GpsLocation(lat, lon); if (location.IsValid()) { res = location; } break; } } log.Error("Invalid response received from '{0}':\n{1}", uri, response); } else { log.Error("Request to '{0}' failed.", uri); } } log.Trace("(-):{0}", res); return(res); }