/// <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); }
/// <summary> /// Informs user that we are going to enter configuration phase and asks several questions regarding general configuration. /// </summary> /// <returns>true if the function succeeded, false otherwise.</returns> public static bool GeneralConfigurationPhase() { log.Trace("()"); bool res = false; CUI.WriteRich("\nWe are going to configure all installed components now.\n"); CUI.WriteRich("\nTrying to find out what is your external IP address... "); IPAddress externalIp = GeneralConfiguration.FindExternalIpAddress(); string ipDefault = ""; string externalIpStr = ""; if (externalIp != null) { CUI.WriteOk(); externalIpStr = externalIp.ToString(); ipDefault = string.Format("[{0}] ", externalIpStr); } else { CUI.WriteFailed(); } bool done = false; while (!done) { CUI.WriteRich("\nEnter external IP address of this machine: {0}", ipDefault); string answer = CUI.ReadStringAnswer(externalIpStr); IPAddress selectedIp; if (IPAddress.TryParse(answer, out selectedIp)) { GeneralConfiguration.ExternalIpAddress = selectedIp; done = true; } else { CUI.WriteRich("<red>ERROR:</red> <white>'{0}'</white> is not a valid IP address. Please try again.\n", answer); } } CUI.WriteLine(); CUI.WriteRich("Trying to find out location of your IP address... "); GpsLocation location = GeneralConfiguration.FindIpLocation(GeneralConfiguration.ExternalIpAddress); string latitudeStr = ""; string longitudeStr = ""; string latitudeDefault = ""; string longitudeDefault = ""; if (location != null) { CUI.WriteOk(); CultureInfo enUs = new CultureInfo("en-US"); latitudeStr = location.Latitude.ToString("0.######", enUs); longitudeStr = location.Longitude.ToString("0.######", enUs); latitudeDefault = string.Format("[{0}] ", latitudeStr); longitudeDefault = string.Format("[{0}] ", longitudeStr); } else { CUI.WriteFailed(); } decimal latitude = 0; decimal longitude = 0; done = false; while (!done) { CUI.WriteRich("\nEnter latitude of GPS location of this machine: {0}", latitudeDefault); string answer = CUI.ReadStringAnswer(latitudeStr); if (decimal.TryParse(answer, NumberStyles.Any, CultureInfo.InvariantCulture, out latitude) && new GpsLocation(latitude, 0).IsValid()) { done = true; } else { CUI.WriteRich("<red>ERROR:</red> <white>'{0}'</white> is not a valid GPS latitude. Please try again.\n", answer); } } done = false; while (!done) { CUI.WriteRich("Enter longitude of GPS location of this machine: {0}", longitudeDefault); string answer = CUI.ReadStringAnswer(longitudeStr); if (decimal.TryParse(answer, NumberStyles.Any, CultureInfo.InvariantCulture, out longitude) && new GpsLocation(latitude, longitude).IsValid()) { done = true; } else { CUI.WriteRich("<red>ERROR:</red> <white>'{0}'</white> is not a valid GPS longitude. Please try again.\n", answer); } } GeneralConfiguration.Location = new GpsLocation(latitude, longitude); CUI.WriteLine(); res = true; log.Trace("(-):{0}", res); return(res); }