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;
|
|
|
|
|
|
|
|
|
|
@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-04-15 10:17:47 +07:00
|
|
|
return 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-04-15 10:25:25 +07:00
|
|
|
public String getCountryCode(String input) {
|
|
|
|
|
try {
|
|
|
|
|
CountryResponse response = extractCountry(input);
|
|
|
|
|
|
|
|
|
|
return response.getCountry().getIsoCode();
|
|
|
|
|
} 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);
|
|
|
|
|
|
|
|
|
|
return response.getCity().getName();
|
|
|
|
|
} 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);
|
|
|
|
|
|
|
|
|
|
return response.getCountry().getName();
|
|
|
|
|
} 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
|
|
|
}
|