This commit is contained in:
@@ -2,63 +2,49 @@ package ru.soune.nocopy.service.geo;
|
|||||||
|
|
||||||
import com.maxmind.geoip2.DatabaseReader;
|
import com.maxmind.geoip2.DatabaseReader;
|
||||||
import com.maxmind.geoip2.model.CountryResponse;
|
import com.maxmind.geoip2.model.CountryResponse;
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class GeoCountryService {
|
public class GeoCountryService {
|
||||||
|
|
||||||
private DatabaseReader dbReader;
|
private final DatabaseReader dbReader;
|
||||||
private final Map<String, String> cache = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
@PostConstruct
|
public GeoCountryService() throws IOException {
|
||||||
public void init() {
|
Resource resource = new ClassPathResource("geo/GeoLite2-Country.mmdb");
|
||||||
try {
|
this.dbReader = new DatabaseReader.Builder(resource.getInputStream()).build();
|
||||||
var resource = new ClassPathResource("geo/GeoLite2-Country.mmdb");
|
|
||||||
this.dbReader = new DatabaseReader.Builder(resource.getInputStream()).build();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Failed to load Geo DB", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCountry(String input) {
|
public String getCountry(String input) {
|
||||||
if (input == null || input.isBlank()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
String host = extractHost(input);
|
|
||||||
|
|
||||||
if (cache.containsKey(host)) {
|
|
||||||
return cache.get(host);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
InetAddress ip = InetAddress.getByName(host);
|
String domain = extractDomain(input);
|
||||||
|
|
||||||
|
InetAddress ip = InetAddress.getByName(domain);
|
||||||
|
|
||||||
CountryResponse response = dbReader.country(ip);
|
CountryResponse response = dbReader.country(ip);
|
||||||
String country = response.getCountry().getIsoCode();
|
|
||||||
|
|
||||||
cache.put(host, country != null ? country : "");
|
|
||||||
log.debug("{} → {}", host, country);
|
|
||||||
return country;
|
|
||||||
|
|
||||||
|
return response.getCountry().getName();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
cache.put(host, "");
|
log.error("Failed for input: {}", input, e);
|
||||||
return null;
|
return "UNKNOWN";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String extractHost(String input) {
|
private String extractDomain(String input) {
|
||||||
String s = input.replaceFirst("^https?://", "");
|
if (input == null || input.isEmpty()) {
|
||||||
s = s.split("/")[0];
|
throw new IllegalArgumentException("Input is empty");
|
||||||
s = s.split(":")[0];
|
}
|
||||||
|
|
||||||
return s;
|
String cleaned = input.replaceAll("^https?://", "");
|
||||||
|
cleaned = cleaned.split("/")[0];
|
||||||
|
cleaned = cleaned.split(":")[0];
|
||||||
|
|
||||||
|
return cleaned;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user