diff --git a/src/main/java/app/services/StatsService.java b/src/main/java/app/services/StatsService.java index 037d7f8..ab10765 100644 --- a/src/main/java/app/services/StatsService.java +++ b/src/main/java/app/services/StatsService.java @@ -7,7 +7,7 @@ import app.entities.server.PlayOn; import app.entities.server.Server; import app.entities.server.players.RCONPlayer; import app.entities.server.request.PlayerOnServer; -import app.services.io.readers.GeoIP; +import app.services.io.readers.GeoIPCountry; import app.utils.CryptedCookie; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -20,13 +20,13 @@ import java.util.Map; public class StatsService { Stats stats; private CryptedCookie cryptedCookie; - private GeoIP geoIP; + private GeoIPCountry geoIPCountry; @Autowired - public StatsService (Stats stats, CryptedCookie cryptedCookie, GeoIP geoIP) { + public StatsService (Stats stats, CryptedCookie cryptedCookie, GeoIPCountry geoIPCountry) { this.stats = stats; this.cryptedCookie = cryptedCookie; - this.geoIP = geoIP; + this.geoIPCountry = geoIPCountry; } public PlayOn searchPlayer(SteamID steamID){ @@ -37,7 +37,7 @@ public class StatsService { player.getId(), cryptedCookie.Hashed(player.getIp()), player.getName(), - geoIP.GetCountry(player.getIp().split(":", 2)[0])); + geoIPCountry.GetCountry(player.getIp().split(":", 2)[0])); } return null; } diff --git a/src/main/java/app/services/db/GraphService.java b/src/main/java/app/services/db/GraphService.java index 8d3313b..1fa0332 100644 --- a/src/main/java/app/services/db/GraphService.java +++ b/src/main/java/app/services/db/GraphService.java @@ -3,7 +3,8 @@ package app.services.db; import app.entities.SearchFilter; import app.entities.db.period.PerPeriodStatistic; import app.services.ProfileService; -import app.services.io.readers.GeoIP; +import app.services.io.readers.GeoIPCity; +import app.services.io.readers.GeoIPCountry; import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import org.slf4j.Logger; @@ -29,7 +30,10 @@ public class GraphService { private NamedParameterJdbcTemplate namedParameterJdbcTemplate; @Autowired - private GeoIP geoIP; + private GeoIPCountry geoIPCountry; + + @Autowired + private GeoIPCity geoIPCity; private final static Logger logger = LoggerFactory.getLogger(GraphService.class); @@ -122,8 +126,8 @@ public class GraphService { return connect_ip; } - public void fillCountryAndCity(GeoIP geoIP) { - this.countryAndCity = geoIP.GetCountry(connect_ip) + "/" + geoIP.GetCity(countryAndCity); + public void fillCountryAndCity(GeoIPCountry geoIPCountry, GeoIPCity geoIPCity) { + this.countryAndCity = geoIPCountry.GetCountry(connect_ip) + "/" + geoIPCity.GetCity(countryAndCity); } } @@ -163,7 +167,7 @@ public class GraphService { return namedParameterJdbcTemplate.query(sql, map, new BeanPropertyRowMapper<>(CountryCityPerPeriodStatistic.class)) .stream() - .peek(d -> d.fillCountryAndCity(geoIP)) + .peek(d -> d.fillCountryAndCity(geoIPCountry, geoIPCity)) .collect(Collectors.toList()); } } diff --git a/src/main/java/app/services/io/readers/GeoIPCity.java b/src/main/java/app/services/io/readers/GeoIPCity.java new file mode 100644 index 0000000..5362cfe --- /dev/null +++ b/src/main/java/app/services/io/readers/GeoIPCity.java @@ -0,0 +1,47 @@ +package app.services.io.readers; + +import app.services.io.fileloader.IFileLoader; +import com.maxmind.geoip2.DatabaseReader; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.InetAddress; +import java.util.List; + +@Component +public class GeoIPCity extends BaseReader { + private final DatabaseReader databaseReader; + + @Autowired + public GeoIPCity(List fileLoaders) { + super(fileLoaders); + databaseReader = loadConfiguration(); + if (databaseReader == null) { + logger.error("GEOIP_CITY file is missing"); + System.exit(1); + } + } + + public String GetCity(String ip) { + try { + return databaseReader.city(InetAddress.getByName(ip)).getCity().getName(); + } catch (Exception e) { + return "Unknown"; + } + } + + public DatabaseReader loadConfiguration() { + return (DatabaseReader) super.loadConfiguration( + "GEOIP_CITY_FILE", + b -> { + try { + return new DatabaseReader.Builder(new ByteArrayInputStream(b)).build(); + } catch (IOException e) { + return null; + } + } + ); + } +} diff --git a/src/main/java/app/services/io/readers/GeoIP.java b/src/main/java/app/services/io/readers/GeoIPCountry.java similarity index 76% rename from src/main/java/app/services/io/readers/GeoIP.java rename to src/main/java/app/services/io/readers/GeoIPCountry.java index 1568f16..6d7c731 100644 --- a/src/main/java/app/services/io/readers/GeoIP.java +++ b/src/main/java/app/services/io/readers/GeoIPCountry.java @@ -2,22 +2,20 @@ package app.services.io.readers; import app.services.io.fileloader.IFileLoader; import com.maxmind.geoip2.DatabaseReader; -import com.maxmind.geoip2.exception.GeoIp2Exception; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.List; @Component -public class GeoIP extends BaseReader { +public class GeoIPCountry extends BaseReader { private final DatabaseReader databaseReader; @Autowired - public GeoIP(List fileLoaders) { + public GeoIPCountry(List fileLoaders) { super(fileLoaders); databaseReader = loadConfiguration(); if (databaseReader == null) { @@ -34,14 +32,6 @@ public class GeoIP extends BaseReader { } } - public String GetCity(String ip) { - try { - return databaseReader.city(InetAddress.getByName(ip)).getCity().getName(); - } catch (Exception e) { - return "Unknown"; - } - } - public DatabaseReader loadConfiguration() { return (DatabaseReader) super.loadConfiguration( "GEOIP_FILE", diff --git a/src/main/java/app/updates/CountriesUpdater.java b/src/main/java/app/updates/CountriesUpdater.java index 14cc88b..0475dd7 100644 --- a/src/main/java/app/updates/CountriesUpdater.java +++ b/src/main/java/app/updates/CountriesUpdater.java @@ -1,10 +1,7 @@ package app.updates; -import app.entities.server.Server; -import app.services.io.readers.GeoIP; +import app.services.io.readers.GeoIPCountry; import app.entities.Stats; -import com.maxmind.geoip2.exception.GeoIp2Exception; -import jakarta.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -15,10 +12,6 @@ import org.springframework.context.event.EventListener; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; -import java.io.IOException; -import java.net.UnknownHostException; -import java.util.Map; - @Component public class CountriesUpdater extends BaseUpdater{ @@ -26,7 +19,7 @@ public class CountriesUpdater extends BaseUpdater{ @Qualifier("jt_ro") private JdbcTemplate jdbcTemplate; private Stats stats; - private GeoIP geoIP; + private GeoIPCountry geoIPCountry; private final Logger logger = LoggerFactory.getLogger(CountriesUpdater.class); @@ -37,9 +30,9 @@ public class CountriesUpdater extends BaseUpdater{ @Autowired public CountriesUpdater(Stats stats, - GeoIP geoIP) { + GeoIPCountry geoIPCountry) { this.stats = stats; - this.geoIP = geoIP; + this.geoIPCountry = geoIPCountry; } @EventListener(ApplicationReadyEvent.class) @@ -55,7 +48,7 @@ public class CountriesUpdater extends BaseUpdater{ stats.getCountriesWriter().clear(); jdbcTemplate.query(countries_in_current_year, (rs, n) -> rs.getString("connect_ip")).forEach(ip -> { - stats.getCountriesWriter().merge(geoIP.GetCountry(ip), 1, Integer::sum); + stats.getCountriesWriter().merge(geoIPCountry.GetCountry(ip), 1, Integer::sum); }); return true; }