diff --git a/src/main/java/ru/soune/nocopy/service/geo/GeoCountryService.java b/src/main/java/ru/soune/nocopy/service/geo/GeoCountryService.java index a5148b4..f381573 100644 --- a/src/main/java/ru/soune/nocopy/service/geo/GeoCountryService.java +++ b/src/main/java/ru/soune/nocopy/service/geo/GeoCountryService.java @@ -11,6 +11,7 @@ import org.springframework.stereotype.Service; import java.io.IOException; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -21,7 +22,6 @@ import java.util.Set; public class GeoCountryService { private final DatabaseReader countryDbReader; - private final DatabaseReader cityDbReader; public GeoCountryService() throws IOException { @@ -30,18 +30,25 @@ public class GeoCountryService { Resource cityResource = new ClassPathResource("geo/GeoLite2-City.mmdb"); this.cityDbReader = new DatabaseReader.Builder(cityResource.getInputStream()).build(); - } public String getCountryName(String input) { try { CountryResponse response = extractCountry(input); - return response.getCountry().getName() == null ? "Not defined": - response.getCountry().getName(); + if (response == null || response.getCountry() == null) { + return "Not defined"; + } + + String countryName = response.getCountry().getName(); + return countryName != null ? countryName : "Not defined"; + + } catch (UnknownHostException e) { + log.warn("Cannot resolve host for input: {}", input); + return "Not defined"; } catch (Exception e) { - log.error("Failed for input: {}", input, e); - return "unknow"; + log.error("Failed to get country for input: {}", input, e); + return "unknown"; } } @@ -54,17 +61,17 @@ public class GeoCountryService { for (String ip : ipList) { try { String country = getCountryByIp(ip, locale); - if (!"unknow".equals(country)) { + if (!"unknown".equals(country) && !"Not defined".equals(country)) { uniqueCountries.add(country); } String countryCode = getCountryCodeByIp(ip); - if (!"unknow".equals(countryCode)) { + if (!"unknown".equals(countryCode) && countryCode != null) { uniqueCountryCodes.add(countryCode); } String city = getCityByIp(ip, locale); - if (!"unknow".equals(city)) { + if (!"unknown".equals(city) && !"Not defined".equals(city)) { uniqueCities.add(city); } } catch (Exception e) { @@ -84,11 +91,19 @@ public class GeoCountryService { try { CountryResponse response = extractCountry(input); - return response.getCountry().getIsoCode() == null ? "Not defined": - response.getCountry().getIsoCode(); + if (response == null || response.getCountry() == null) { + return "Not defined"; + } + + String isoCode = response.getCountry().getIsoCode(); + return isoCode != null ? isoCode : "Not defined"; + + } catch (UnknownHostException e) { + log.warn("Cannot resolve host for input: {}", input); + return "Not defined"; } catch (Exception e) { - log.error("Failed for input: {}", input, e); - return "unknow"; + log.error("Failed to get country code for input: {}", input, e); + return "unknown"; } } @@ -96,13 +111,22 @@ public class GeoCountryService { try { InetAddress ipAddress = InetAddress.getByName(ip); CityResponse response = cityDbReader.city(ipAddress); + + if (response.getCity() == null) { + return "Not defined"; + } + String cityName = response.getCity().getNames().getOrDefault(locale, response.getCity().getName()); - return cityName == null ? "unknow": cityName; + return cityName != null ? cityName : "Not defined"; + + } catch (UnknownHostException e) { + log.warn("Cannot resolve IP: {}", ip); + return "Not defined"; } catch (Exception e) { log.error("Failed to get city for IP: {}", ip, e); - return "unknow"; + return "unknown"; } } @@ -110,13 +134,22 @@ public class GeoCountryService { try { InetAddress ipAddress = InetAddress.getByName(ip); CountryResponse response = countryDbReader.country(ipAddress); + + if (response.getCountry() == null) { + return "Not defined"; + } + String countryName = response.getCountry().getNames().getOrDefault(locale, response.getCountry().getName()); - return countryName == null ? "unknow": countryName; + return countryName != null ? countryName : "Not defined"; + + } catch (UnknownHostException e) { + log.warn("Cannot resolve IP: {}", ip); + return "Not defined"; } catch (Exception e) { log.error("Failed to get country for IP: {}", ip, e); - return "unknow"; + return "unknown"; } } @@ -125,17 +158,34 @@ public class GeoCountryService { InetAddress ipAddress = InetAddress.getByName(ip); CountryResponse response = countryDbReader.country(ipAddress); - return response.getCountry().getIsoCode(); + return response.getCountry() != null ? response.getCountry().getIsoCode() : null; + + } catch (UnknownHostException e) { + log.warn("Cannot resolve IP: {}", ip); + return null; } catch (Exception e) { log.error("Failed to get country code for IP: {}", ip, e); - return "unknow"; + return "unknown"; } } private CountryResponse extractCountry(String input) throws IOException, GeoIp2Exception { String domain = extractDomain(input); - InetAddress ip = InetAddress.getByName(domain); + InetAddress ip; + try { + ip = InetAddress.getByName(domain); + } catch (UnknownHostException e) { + if (!domain.startsWith("www.")) { + try { + ip = InetAddress.getByName("www." + domain); + } catch (UnknownHostException ex) { + throw new UnknownHostException("Cannot resolve domain: " + domain); + } + } else { + throw e; + } + } return countryDbReader.country(ip); } @@ -145,10 +195,32 @@ public class GeoCountryService { throw new IllegalArgumentException("Input is empty"); } - String cleaned = input.replaceAll("^https?://", ""); - cleaned = cleaned.split("/")[0]; - cleaned = cleaned.split(":")[0]; + try { + String cleaned = input.trim().toLowerCase(); - return cleaned; + cleaned = cleaned.replaceAll("^https?://", ""); + + cleaned = cleaned.replaceAll("^www\\.", ""); + + cleaned = cleaned.split("/")[0]; + + cleaned = cleaned.split(":")[0]; + + if (cleaned.contains("?")) { + cleaned = cleaned.split("\\?")[0]; + } + + cleaned = cleaned.trim(); + + if (!cleaned.contains(".") || cleaned.length() < 4) { + throw new IllegalArgumentException("Invalid domain format: " + cleaned); + } + + return cleaned; + + } catch (Exception e) { + log.error("Failed to extract domain from: {}", input, e); + throw new IllegalArgumentException("Cannot extract domain from: " + input, e); + } } } \ No newline at end of file