51 lines
2.0 KiB
C#
51 lines
2.0 KiB
C#
namespace Tabletop.Core.Services
|
|
{
|
|
public class GeolocationService(HttpClient httpClient)
|
|
{
|
|
private readonly HttpClient _httpClient = httpClient;
|
|
|
|
public Task<string> GetGeolocationAsync(string ipAddress)
|
|
{
|
|
//// Überprüfen, ob die IP-Adresse lokal ist (127.0.0.1 oder localhost)
|
|
//if (await IsLocalIpAddressAsync(ipAddress))
|
|
//{
|
|
// // Wenn es eine lokale IP-Adresse ist, keine Geolokalisierung durchführen
|
|
// return "Lokale IP-Adresse";
|
|
//}
|
|
|
|
//try
|
|
//{
|
|
// // URL für Geolokalisierung
|
|
// var url = $"https://api.ipgeolocation.io/ipgeo?apiKey={AppdataService.ApiKey}&ip={ipAddress}";
|
|
|
|
// // HTTP-Anfrage senden
|
|
// var response = await _httpClient.GetAsync(url);
|
|
|
|
// // Fehlerbehandlung: Prüfen, ob der Statuscode erfolgreich war
|
|
// response.EnsureSuccessStatusCode(); // Wird eine Ausnahme werfen, wenn der Statuscode nicht erfolgreich ist
|
|
|
|
// // Rückgabe der Geolokalisierungsdaten
|
|
// return await response.Content.ReadAsStringAsync();
|
|
//}
|
|
//catch (HttpRequestException ex)
|
|
//{
|
|
// // Fehlerbehandlung: Loggen oder Weiterverarbeiten des Fehlers
|
|
// throw new Exception($"Fehler bei der Geolokalisierung: {ex.Message}", ex);
|
|
//}
|
|
return Task.FromResult("Lokale IP-Adresse");
|
|
}
|
|
|
|
// Hilfsmethode, um zu prüfen, ob die IP-Adresse lokal ist
|
|
private static Task<bool> IsLocalIpAddressAsync(string ipAddress)
|
|
{
|
|
// Überprüfen, ob die IP-Adresse eine der folgenden ist
|
|
bool isLocal = ipAddress == "127.0.0.1" || ipAddress == "localhost" ||
|
|
ipAddress.StartsWith("10.") || ipAddress.StartsWith("192.168.") ||
|
|
ipAddress.StartsWith("172.16.");
|
|
|
|
return Task.FromResult(isLocal); // Rückgabe des Ergebnisses als Task
|
|
}
|
|
|
|
}
|
|
|
|
} |