Merge branch 'Stabrinai:master' into master
This commit is contained in:
commit
6a91130aa8
@ -52,7 +52,6 @@ import org.bukkit.event.player.PlayerRespawnEvent;
|
|||||||
import org.bukkit.event.player.PlayerShearEntityEvent;
|
import org.bukkit.event.player.PlayerShearEntityEvent;
|
||||||
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
|
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
|
||||||
import org.bukkit.inventory.InventoryView;
|
import org.bukkit.inventory.InventoryView;
|
||||||
import org.geysermc.floodgate.api.FloodgateApi;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
@ -94,7 +93,6 @@ public class PlayerListener implements Listener {
|
|||||||
private PermissionsManager permissionsManager;
|
private PermissionsManager permissionsManager;
|
||||||
@Inject
|
@Inject
|
||||||
private QuickCommandsProtectionManager quickCommandsProtectionManager;
|
private QuickCommandsProtectionManager quickCommandsProtectionManager;
|
||||||
FloodgateApi Floodgate = org.geysermc.floodgate.api.FloodgateApi.getInstance();
|
|
||||||
// Lowest priority to apply fast protection checks
|
// Lowest priority to apply fast protection checks
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
public void onAsyncPlayerPreLoginEventLowest(AsyncPlayerPreLoginEvent event) {
|
public void onAsyncPlayerPreLoginEventLowest(AsyncPlayerPreLoginEvent event) {
|
||||||
@ -113,7 +111,8 @@ public class PlayerListener implements Listener {
|
|||||||
if (validationService.isUnrestricted(name)) {
|
if (validationService.isUnrestricted(name)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (settings.getProperty(RestrictionSettings.HOOK_FLOODGATE_PLAYER) && Floodgate.isFloodgateId(event.getUniqueId())){
|
|
||||||
|
if (settings.getProperty(RestrictionSettings.HOOK_FLOODGATE_PLAYER) && org.geysermc.floodgate.api.FloodgateApi.getInstance().isFloodgateId(event.getUniqueId())){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Non-blocking checks
|
// Non-blocking checks
|
||||||
|
|||||||
@ -47,37 +47,21 @@ public class GeoIpService {
|
|||||||
|
|
||||||
private static final String DATABASE_NAME = "GeoLite2-Country";
|
private static final String DATABASE_NAME = "GeoLite2-Country";
|
||||||
private static final String DATABASE_FILE = DATABASE_NAME + ".mmdb";
|
private static final String DATABASE_FILE = DATABASE_NAME + ".mmdb";
|
||||||
private static final String DATABASE_TMP_FILE = DATABASE_NAME + ".mmdb.tmp";
|
|
||||||
|
|
||||||
private static final String ARCHIVE_FILE = DATABASE_NAME + ".mmdb.gz";
|
|
||||||
|
|
||||||
private static final String ARCHIVE_URL =
|
|
||||||
"https://updates.maxmind.com/geoip/databases/" + DATABASE_NAME + "/update";
|
|
||||||
|
|
||||||
private static final int UPDATE_INTERVAL_DAYS = 30;
|
|
||||||
|
|
||||||
private final ConsoleLogger logger = ConsoleLoggerFactory.get(GeoIpService.class);
|
private final ConsoleLogger logger = ConsoleLoggerFactory.get(GeoIpService.class);
|
||||||
private final Path dataFile;
|
private final Path dataFile;
|
||||||
private final BukkitService bukkitService;
|
|
||||||
private final Settings settings;
|
|
||||||
|
|
||||||
private GeoIp2Provider databaseReader;
|
private GeoIp2Provider databaseReader;
|
||||||
private volatile boolean downloading;
|
private volatile boolean downloading;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
GeoIpService(@DataFolder File dataFolder, BukkitService bukkitService, Settings settings) {
|
GeoIpService(@DataFolder File dataFolder) {
|
||||||
this.bukkitService = bukkitService;
|
|
||||||
this.dataFile = dataFolder.toPath().resolve(DATABASE_FILE);
|
this.dataFile = dataFolder.toPath().resolve(DATABASE_FILE);
|
||||||
this.settings = settings;
|
|
||||||
|
|
||||||
// Fires download of recent data or the initialization of the look up service
|
// Fires download of recent data or the initialization of the look up service
|
||||||
isDataAvailable();
|
isDataAvailable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
GeoIpService(@DataFolder File dataFolder, BukkitService bukkitService, Settings settings, GeoIp2Provider reader) {
|
GeoIpService(@DataFolder File dataFolder, GeoIp2Provider reader) {
|
||||||
this.bukkitService = bukkitService;
|
|
||||||
this.settings = settings;
|
|
||||||
this.dataFile = dataFolder.toPath().resolve(DATABASE_FILE);
|
this.dataFile = dataFolder.toPath().resolve(DATABASE_FILE);
|
||||||
|
|
||||||
this.databaseReader = reader;
|
this.databaseReader = reader;
|
||||||
@ -101,73 +85,19 @@ public class GeoIpService {
|
|||||||
|
|
||||||
if (Files.exists(dataFile)) {
|
if (Files.exists(dataFile)) {
|
||||||
try {
|
try {
|
||||||
FileTime lastModifiedTime = Files.getLastModifiedTime(dataFile);
|
|
||||||
if (Duration.between(lastModifiedTime.toInstant(), Instant.now()).toDays() <= UPDATE_INTERVAL_DAYS) {
|
|
||||||
startReading();
|
startReading();
|
||||||
|
|
||||||
// don't fire the update task - we are up to date
|
// don't fire the update task - we are up to date
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
logger.debug("GEO IP database is older than " + UPDATE_INTERVAL_DAYS + " Days");
|
|
||||||
}
|
|
||||||
} catch (IOException ioEx) {
|
} catch (IOException ioEx) {
|
||||||
logger.logException("Failed to load GeoLiteAPI database", ioEx);
|
logger.logException("Failed to load GeoLiteAPI database", ioEx);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//set the downloading flag in order to fix race conditions outside
|
|
||||||
downloading = true;
|
|
||||||
|
|
||||||
// File is outdated or doesn't exist - let's try to download the data file!
|
// File is outdated or doesn't exist - let's try to download the data file!
|
||||||
// use bukkit's cached threads
|
// use bukkit's cached threads
|
||||||
bukkitService.runTaskAsynchronously(this::updateDatabase);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Tries to update the database by downloading a new version from the website.
|
|
||||||
*/
|
|
||||||
private void updateDatabase() {
|
|
||||||
logger.info("Downloading GEO IP database, because the old database is older than "
|
|
||||||
+ UPDATE_INTERVAL_DAYS + " days or doesn't exist");
|
|
||||||
|
|
||||||
Path downloadFile = null;
|
|
||||||
Path tempFile = null;
|
|
||||||
try {
|
|
||||||
// download database to temporarily location
|
|
||||||
downloadFile = Files.createTempFile(ARCHIVE_FILE, null);
|
|
||||||
tempFile = Files.createTempFile(DATABASE_TMP_FILE, null);
|
|
||||||
String expectedChecksum = downloadDatabaseArchive(downloadFile);
|
|
||||||
if (expectedChecksum == null) {
|
|
||||||
logger.info("There is no newer GEO IP database uploaded to MaxMind. Using the old one for now.");
|
|
||||||
startReading();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// tar extract database and copy to target destination
|
|
||||||
extractDatabase(downloadFile, tempFile);
|
|
||||||
|
|
||||||
// MD5 checksum verification
|
|
||||||
verifyChecksum(Hashing.md5(), tempFile, expectedChecksum);
|
|
||||||
|
|
||||||
Files.copy(tempFile, dataFile, StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
|
|
||||||
//only set this value to false on success otherwise errors could lead to endless download triggers
|
|
||||||
logger.info("Successfully downloaded new GEO IP database to " + dataFile);
|
|
||||||
startReading();
|
|
||||||
} catch (IOException ioEx) {
|
|
||||||
logger.logException("Could not download GeoLiteAPI database", ioEx);
|
|
||||||
} finally {
|
|
||||||
// clean up
|
|
||||||
if (downloadFile != null) {
|
|
||||||
FileUtils.delete(downloadFile.toFile());
|
|
||||||
}
|
|
||||||
if (tempFile != null) {
|
|
||||||
FileUtils.delete(tempFile.toFile());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startReading() throws IOException {
|
private void startReading() throws IOException {
|
||||||
databaseReader = new Reader(dataFile.toFile(), FileMode.MEMORY, new CHMCache());
|
databaseReader = new Reader(dataFile.toFile(), FileMode.MEMORY, new CHMCache());
|
||||||
@ -177,101 +107,6 @@ public class GeoIpService {
|
|||||||
downloading = false;
|
downloading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Downloads the archive to the destination file if it's newer than the locally version.
|
|
||||||
*
|
|
||||||
* @param lastModified modification timestamp of the already present file
|
|
||||||
* @param destination save file
|
|
||||||
* @return null if no updates were found, the MD5 hash of the downloaded archive if successful
|
|
||||||
* @throws IOException if failed during downloading and writing to destination file
|
|
||||||
*/
|
|
||||||
private String downloadDatabaseArchive(Instant lastModified, Path destination) throws IOException {
|
|
||||||
String clientId = settings.getProperty(ProtectionSettings.MAXMIND_API_CLIENT_ID);
|
|
||||||
String licenseKey = settings.getProperty(ProtectionSettings.MAXMIND_API_LICENSE_KEY);
|
|
||||||
if (clientId.isEmpty() || licenseKey.isEmpty()) {
|
|
||||||
logger.warning("No MaxMind credentials found in the configuration file!"
|
|
||||||
+ " GeoIp protections will be disabled.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) new URL(ARCHIVE_URL).openConnection();
|
|
||||||
String basicAuth = "Basic " + new String(Base64.getEncoder().encode((clientId + ":" + licenseKey).getBytes()));
|
|
||||||
connection.setRequestProperty("Authorization", basicAuth);
|
|
||||||
|
|
||||||
if (lastModified != null) {
|
|
||||||
// Only download if we actually need a newer version - this field is specified in GMT zone
|
|
||||||
ZonedDateTime zonedTime = lastModified.atZone(ZoneId.of("GMT"));
|
|
||||||
String timeFormat = DateTimeFormatter.RFC_1123_DATE_TIME.format(zonedTime);
|
|
||||||
connection.addRequestProperty("If-Modified-Since", timeFormat);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
|
|
||||||
//we already have the newest version
|
|
||||||
connection.getInputStream().close();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
String hash = connection.getHeaderField("X-Database-MD5");
|
|
||||||
String rawModifiedDate = connection.getHeaderField("Last-Modified");
|
|
||||||
Instant modifiedDate = Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(rawModifiedDate));
|
|
||||||
Files.copy(connection.getInputStream(), destination, StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
Files.setLastModifiedTime(destination, FileTime.from(modifiedDate));
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Downloads the archive to the destination file if it's newer than the locally version.
|
|
||||||
*
|
|
||||||
* @param destination save file
|
|
||||||
* @return null if no updates were found, the MD5 hash of the downloaded archive if successful
|
|
||||||
* @throws IOException if failed during downloading and writing to destination file
|
|
||||||
*/
|
|
||||||
private String downloadDatabaseArchive(Path destination) throws IOException {
|
|
||||||
Instant lastModified = null;
|
|
||||||
if (Files.exists(dataFile)) {
|
|
||||||
lastModified = Files.getLastModifiedTime(dataFile).toInstant();
|
|
||||||
}
|
|
||||||
|
|
||||||
return downloadDatabaseArchive(lastModified, destination);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Verify if the expected checksum is equal to the checksum of the given file.
|
|
||||||
*
|
|
||||||
* @param function the checksum function like MD5, SHA256 used to generate the checksum from the file
|
|
||||||
* @param file the file we want to calculate the checksum from
|
|
||||||
* @param expectedChecksum the expected checksum
|
|
||||||
* @throws IOException on I/O error reading the file or the checksum verification failed
|
|
||||||
*/
|
|
||||||
private void verifyChecksum(HashFunction function, Path file, String expectedChecksum) throws IOException {
|
|
||||||
HashCode actualHash = function.hashBytes(Files.readAllBytes(file));
|
|
||||||
HashCode expectedHash = HashCode.fromString(expectedChecksum);
|
|
||||||
if (!Objects.equals(actualHash, expectedHash)) {
|
|
||||||
throw new IOException("GEO IP Checksum verification failed. "
|
|
||||||
+ "Expected: " + expectedChecksum + "Actual:" + actualHash);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract the database from gzipped data. Existing outputFile will be replaced if it already exists.
|
|
||||||
*
|
|
||||||
* @param inputFile gzipped database input file
|
|
||||||
* @param outputFile destination file for the database
|
|
||||||
* @throws IOException on I/O error reading the archive, or writing the output
|
|
||||||
*/
|
|
||||||
private void extractDatabase(Path inputFile, Path outputFile) throws IOException {
|
|
||||||
// .gz -> gzipped file
|
|
||||||
try (BufferedInputStream in = new BufferedInputStream(Files.newInputStream(inputFile));
|
|
||||||
GZIPInputStream gzipIn = new GZIPInputStream(in)) {
|
|
||||||
|
|
||||||
// found the database file and copy file
|
|
||||||
Files.copy(gzipIn, outputFile, StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
|
|
||||||
// update the last modification date to be same as in the archive
|
|
||||||
Files.setLastModifiedTime(outputFile, Files.getLastModifiedTime(inputFile));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the country code of the given IP address.
|
* Get the country code of the given IP address.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -65,7 +65,7 @@ public final class EmailSettings implements SettingsHolder {
|
|||||||
newProperty("Email.emailOauth2Token", "");
|
newProperty("Email.emailOauth2Token", "");
|
||||||
@Comment("Email notifications when the server shuts down")
|
@Comment("Email notifications when the server shuts down")
|
||||||
public static final Property<Boolean> SHUTDOWN_MAIL =
|
public static final Property<Boolean> SHUTDOWN_MAIL =
|
||||||
newProperty("Email.shutDownEmail", true);
|
newProperty("Email.shutDownEmail", false);
|
||||||
@Comment("Email notification address when the server is shut down")
|
@Comment("Email notification address when the server is shut down")
|
||||||
public static final Property<String> SHUTDOWN_MAIL_ADDRESS =
|
public static final Property<String> SHUTDOWN_MAIL_ADDRESS =
|
||||||
newProperty("Email.shutDownEmailAddress", "your@mail.com");
|
newProperty("Email.shutDownEmailAddress", "your@mail.com");
|
||||||
|
|||||||
@ -20,17 +20,6 @@ public final class ProtectionSettings implements SettingsHolder {
|
|||||||
public static final Property<Boolean> ENABLE_PROTECTION_REGISTERED =
|
public static final Property<Boolean> ENABLE_PROTECTION_REGISTERED =
|
||||||
newProperty("Protection.enableProtectionRegistered", true);
|
newProperty("Protection.enableProtectionRegistered", true);
|
||||||
|
|
||||||
@Comment({"The MaxMind clientId used to download the GeoIp database,",
|
|
||||||
"get one at https://www.maxmind.com/en/accounts/current/license-key",
|
|
||||||
"The EssentialsX project has a very useful tutorial on how to generate",
|
|
||||||
"the license key: https://github.com/EssentialsX/Wiki/blob/master/GeoIP.md"})
|
|
||||||
public static final Property<String> MAXMIND_API_CLIENT_ID =
|
|
||||||
newProperty("Protection.geoIpDatabase.clientId", "");
|
|
||||||
|
|
||||||
@Comment("The MaxMind licenseKey used to download the GeoIp database.")
|
|
||||||
public static final Property<String> MAXMIND_API_LICENSE_KEY =
|
|
||||||
newProperty("Protection.geoIpDatabase.licenseKey", "");
|
|
||||||
|
|
||||||
@Comment({
|
@Comment({
|
||||||
"Countries allowed to join the server and register. For country codes, see",
|
"Countries allowed to join the server and register. For country codes, see",
|
||||||
"https://dev.maxmind.com/geoip/legacy/codes/iso3166/",
|
"https://dev.maxmind.com/geoip/legacy/codes/iso3166/",
|
||||||
|
|||||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user