Files
no-copy/src/main/java/ru/soune/nocopy/service/geo/GeoCountryService.java
T

152 lines
5.0 KiB
Java
Raw Normal View History

2026-04-15 09:34:32 +07:00
package ru.soune.nocopy.service.geo;
import com.maxmind.geoip2.DatabaseReader;
2026-04-15 10:25:25 +07:00
import com.maxmind.geoip2.exception.GeoIp2Exception;
2026-05-05 13:44:49 +07:00
import com.maxmind.geoip2.model.CityResponse;
2026-04-15 09:34:32 +07:00
import com.maxmind.geoip2.model.CountryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
2026-04-15 10:17:47 +07:00
import org.springframework.core.io.Resource;
2026-04-15 09:34:32 +07:00
import org.springframework.stereotype.Service;
2026-04-15 10:17:47 +07:00
import java.io.IOException;
2026-04-15 09:34:32 +07:00
import java.net.InetAddress;
2026-05-05 15:31:49 +07:00
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
2026-04-15 09:34:32 +07:00
@Service
@Slf4j
public class GeoCountryService {
2026-05-05 13:44:49 +07:00
private final DatabaseReader countryDbReader;
private final DatabaseReader cityDbReader;
2026-04-15 09:34:32 +07:00
2026-04-15 10:17:47 +07:00
public GeoCountryService() throws IOException {
Resource resource = new ClassPathResource("geo/GeoLite2-Country.mmdb");
2026-05-05 13:44:49 +07:00
this.countryDbReader = new DatabaseReader.Builder(resource.getInputStream()).build();
Resource cityResource = new ClassPathResource("geo/GeoLite2-City.mmdb");
this.cityDbReader = new DatabaseReader.Builder(cityResource.getInputStream()).build();
2026-04-15 09:34:32 +07:00
}
2026-04-15 10:25:25 +07:00
public String getCountryName(String input) {
2026-04-15 09:34:32 +07:00
try {
2026-04-15 10:25:25 +07:00
CountryResponse response = extractCountry(input);
2026-04-15 09:34:32 +07:00
2026-05-13 07:44:24 +07:00
return response.getCountry().getName() == null ? "Not defined":
response.getCountry().getName();
2026-04-15 09:34:32 +07:00
} catch (Exception e) {
2026-04-15 10:17:47 +07:00
log.error("Failed for input: {}", input, e);
return "UNKNOWN";
2026-04-15 09:34:32 +07:00
}
}
2026-05-05 15:31:49 +07:00
public Map<String, Integer> getUniqueGeoCount(List<String> ipList) {
Set<String> uniqueCountries = new HashSet<>();
Set<String> uniqueCountryCodes = new HashSet<>();
Set<String> uniqueCities = new HashSet<>();
if (ipList != null) {
for (String ip : ipList) {
try {
String country = getCountryByIp(ip);
if (!"UNKNOWN".equals(country)) {
uniqueCountries.add(country);
}
String countryCode = getCountryCodeByIp(ip);
if (!"UNKNOWN".equals(countryCode)) {
uniqueCountryCodes.add(countryCode);
}
String city = getCityByIp(ip);
if (!"UNKNOWN".equals(city)) {
uniqueCities.add(city);
}
} catch (Exception e) {
log.error("Error processing IP: {}", ip, e);
}
}
}
return Map.of(
"uniqueCountries", uniqueCountries.size(),
"uniqueCountryCodes", uniqueCountryCodes.size(),
"uniqueCities", uniqueCities.size()
);
}
2026-04-15 10:25:25 +07:00
public String getCountryCode(String input) {
try {
CountryResponse response = extractCountry(input);
2026-05-13 07:44:24 +07:00
return response.getCountry().getIsoCode() == null ? "Not defined":
response.getCountry().getIsoCode();
2026-04-15 10:25:25 +07:00
} catch (Exception e) {
log.error("Failed for input: {}", input, e);
return "UNKNOWN";
}
}
2026-05-05 13:44:49 +07:00
public String getCityByIp(String ip) {
try {
InetAddress ipAddress = InetAddress.getByName(ip);
CityResponse response = cityDbReader.city(ipAddress);
2026-05-14 12:28:52 +07:00
return response.getCity().getNames().getOrDefault("ru",
response.getCity().getName());
2026-05-05 13:44:49 +07:00
} catch (Exception e) {
log.error("Failed to get city for IP: {}", ip, e);
return "UNKNOWN";
}
}
public String getCountryByIp(String ip) {
try {
InetAddress ipAddress = InetAddress.getByName(ip);
CountryResponse response = countryDbReader.country(ipAddress);
2026-05-14 12:28:52 +07:00
return response.getCountry().getNames().getOrDefault("ru",
response.getCountry().getName());
2026-05-05 13:44:49 +07:00
} catch (Exception e) {
log.error("Failed to get country for IP: {}", ip, e);
return "UNKNOWN";
}
}
public String getCountryCodeByIp(String ip) {
try {
InetAddress ipAddress = InetAddress.getByName(ip);
CountryResponse response = countryDbReader.country(ipAddress);
return response.getCountry().getIsoCode();
} catch (Exception e) {
log.error("Failed to get country code for IP: {}", ip, e);
return "UNKNOWN";
}
}
2026-04-15 10:25:25 +07:00
private CountryResponse extractCountry(String input) throws IOException, GeoIp2Exception {
String domain = extractDomain(input);
InetAddress ip = InetAddress.getByName(domain);
2026-05-05 13:44:49 +07:00
return countryDbReader.country(ip);
2026-04-15 10:25:25 +07:00
}
2026-04-15 10:17:47 +07:00
private String extractDomain(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("Input is empty");
}
2026-04-15 10:10:46 +07:00
2026-04-15 10:17:47 +07:00
String cleaned = input.replaceAll("^https?://", "");
cleaned = cleaned.split("/")[0];
cleaned = cleaned.split(":")[0];
return cleaned;
2026-04-15 09:34:32 +07:00
}
2026-04-15 10:10:46 +07:00
}