Browse Source

geoip files

master
gsd 3 weeks ago
parent
commit
6dea2b1973
  1. 10
      src/main/java/app/services/StatsService.java
  2. 14
      src/main/java/app/services/db/GraphService.java
  3. 47
      src/main/java/app/services/io/readers/GeoIPCity.java
  4. 14
      src/main/java/app/services/io/readers/GeoIPCountry.java
  5. 17
      src/main/java/app/updates/CountriesUpdater.java

10
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.Server;
import app.entities.server.players.RCONPlayer; import app.entities.server.players.RCONPlayer;
import app.entities.server.request.PlayerOnServer; import app.entities.server.request.PlayerOnServer;
import app.services.io.readers.GeoIP; import app.services.io.readers.GeoIPCountry;
import app.utils.CryptedCookie; import app.utils.CryptedCookie;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -20,13 +20,13 @@ import java.util.Map;
public class StatsService { public class StatsService {
Stats stats; Stats stats;
private CryptedCookie cryptedCookie; private CryptedCookie cryptedCookie;
private GeoIP geoIP; private GeoIPCountry geoIPCountry;
@Autowired @Autowired
public StatsService (Stats stats, CryptedCookie cryptedCookie, GeoIP geoIP) { public StatsService (Stats stats, CryptedCookie cryptedCookie, GeoIPCountry geoIPCountry) {
this.stats = stats; this.stats = stats;
this.cryptedCookie = cryptedCookie; this.cryptedCookie = cryptedCookie;
this.geoIP = geoIP; this.geoIPCountry = geoIPCountry;
} }
public PlayOn searchPlayer(SteamID steamID){ public PlayOn searchPlayer(SteamID steamID){
@ -37,7 +37,7 @@ public class StatsService {
player.getId(), player.getId(),
cryptedCookie.Hashed(player.getIp()), cryptedCookie.Hashed(player.getIp()),
player.getName(), player.getName(),
geoIP.GetCountry(player.getIp().split(":", 2)[0])); geoIPCountry.GetCountry(player.getIp().split(":", 2)[0]));
} }
return null; return null;
} }

14
src/main/java/app/services/db/GraphService.java

@ -3,7 +3,8 @@ package app.services.db;
import app.entities.SearchFilter; import app.entities.SearchFilter;
import app.entities.db.period.PerPeriodStatistic; import app.entities.db.period.PerPeriodStatistic;
import app.services.ProfileService; 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 com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -29,7 +30,10 @@ public class GraphService {
private NamedParameterJdbcTemplate namedParameterJdbcTemplate; private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Autowired @Autowired
private GeoIP geoIP; private GeoIPCountry geoIPCountry;
@Autowired
private GeoIPCity geoIPCity;
private final static Logger logger = LoggerFactory.getLogger(GraphService.class); private final static Logger logger = LoggerFactory.getLogger(GraphService.class);
@ -122,8 +126,8 @@ public class GraphService {
return connect_ip; return connect_ip;
} }
public void fillCountryAndCity(GeoIP geoIP) { public void fillCountryAndCity(GeoIPCountry geoIPCountry, GeoIPCity geoIPCity) {
this.countryAndCity = geoIP.GetCountry(connect_ip) + "/" + geoIP.GetCity(countryAndCity); 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)) return namedParameterJdbcTemplate.query(sql, map, new BeanPropertyRowMapper<>(CountryCityPerPeriodStatistic.class))
.stream() .stream()
.peek(d -> d.fillCountryAndCity(geoIP)) .peek(d -> d.fillCountryAndCity(geoIPCountry, geoIPCity))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
} }

47
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<IFileLoader> 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;
}
}
);
}
}

14
src/main/java/app/services/io/readers/GeoIP.java → 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 app.services.io.fileloader.IFileLoader;
import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List; import java.util.List;
@Component @Component
public class GeoIP extends BaseReader { public class GeoIPCountry extends BaseReader {
private final DatabaseReader databaseReader; private final DatabaseReader databaseReader;
@Autowired @Autowired
public GeoIP(List<IFileLoader> fileLoaders) { public GeoIPCountry(List<IFileLoader> fileLoaders) {
super(fileLoaders); super(fileLoaders);
databaseReader = loadConfiguration(); databaseReader = loadConfiguration();
if (databaseReader == null) { 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() { public DatabaseReader loadConfiguration() {
return (DatabaseReader) super.loadConfiguration( return (DatabaseReader) super.loadConfiguration(
"GEOIP_FILE", "GEOIP_FILE",

17
src/main/java/app/updates/CountriesUpdater.java

@ -1,10 +1,7 @@
package app.updates; package app.updates;
import app.entities.server.Server; import app.services.io.readers.GeoIPCountry;
import app.services.io.readers.GeoIP;
import app.entities.Stats; import app.entities.Stats;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; 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.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Map;
@Component @Component
public class CountriesUpdater extends BaseUpdater{ public class CountriesUpdater extends BaseUpdater{
@ -26,7 +19,7 @@ public class CountriesUpdater extends BaseUpdater{
@Qualifier("jt_ro") @Qualifier("jt_ro")
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
private Stats stats; private Stats stats;
private GeoIP geoIP; private GeoIPCountry geoIPCountry;
private final Logger logger = LoggerFactory.getLogger(CountriesUpdater.class); private final Logger logger = LoggerFactory.getLogger(CountriesUpdater.class);
@ -37,9 +30,9 @@ public class CountriesUpdater extends BaseUpdater{
@Autowired @Autowired
public CountriesUpdater(Stats stats, public CountriesUpdater(Stats stats,
GeoIP geoIP) { GeoIPCountry geoIPCountry) {
this.stats = stats; this.stats = stats;
this.geoIP = geoIP; this.geoIPCountry = geoIPCountry;
} }
@EventListener(ApplicationReadyEvent.class) @EventListener(ApplicationReadyEvent.class)
@ -55,7 +48,7 @@ public class CountriesUpdater extends BaseUpdater{
stats.getCountriesWriter().clear(); stats.getCountriesWriter().clear();
jdbcTemplate.query(countries_in_current_year, (rs, n) -> rs.getString("connect_ip")).forEach(ip -> { 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; return true;
} }

Loading…
Cancel
Save