dev add method link
Test Workflow / test (push) Has been cancelled

This commit is contained in:
2026-05-05 13:44:49 +07:00
parent 6793bbfc5c
commit bb43b6ef4f
8 changed files with 328 additions and 4 deletions
@@ -2,6 +2,7 @@ package ru.soune.nocopy.service.geo;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.model.CountryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
@@ -15,11 +16,17 @@ import java.net.InetAddress;
@Slf4j
public class GeoCountryService {
private final DatabaseReader dbReader;
private final DatabaseReader countryDbReader;
private final DatabaseReader cityDbReader;
public GeoCountryService() throws IOException {
Resource resource = new ClassPathResource("geo/GeoLite2-Country.mmdb");
this.dbReader = new DatabaseReader.Builder(resource.getInputStream()).build();
this.countryDbReader = new DatabaseReader.Builder(resource.getInputStream()).build();
Resource cityResource = new ClassPathResource("geo/GeoLite2-City.mmdb");
this.cityDbReader = new DatabaseReader.Builder(cityResource.getInputStream()).build();
}
public String getCountryName(String input) {
@@ -44,12 +51,48 @@ public class GeoCountryService {
}
}
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";
}
}
private CountryResponse extractCountry(String input) throws IOException, GeoIp2Exception {
String domain = extractDomain(input);
InetAddress ip = InetAddress.getByName(domain);
return dbReader.country(ip);
return countryDbReader.country(ip);
}
private String extractDomain(String input) {