Config option to disable GeoIP database #2720

This commit is contained in:
3meraldK 2023-09-24 17:26:16 +02:00 committed by ljacqu
parent 2de4a60f3a
commit 65a272d6cb
3 changed files with 21 additions and 0 deletions

View File

@ -403,6 +403,8 @@ Protection:
# Apply the protection also to registered usernames # Apply the protection also to registered usernames
enableProtectionRegistered: true enableProtectionRegistered: true
geoIpDatabase: geoIpDatabase:
# Enable GeoIp database
enabled: true
# The MaxMind clientId used to download the GeoIp database, # The MaxMind clientId used to download the GeoIp database,
# get one at https://www.maxmind.com/en/accounts/current/license-key # 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 EssentialsX project has a very useful tutorial on how to generate

View File

@ -64,6 +64,8 @@ public class GeoIpService {
private GeoIp2Provider databaseReader; private GeoIp2Provider databaseReader;
private volatile boolean downloading; private volatile boolean downloading;
private boolean enabled = true;
@Inject @Inject
GeoIpService(@DataFolder File dataFolder, BukkitService bukkitService, Settings settings) { GeoIpService(@DataFolder File dataFolder, BukkitService bukkitService, Settings settings) {
this.bukkitService = bukkitService; this.bukkitService = bukkitService;
@ -89,6 +91,13 @@ public class GeoIpService {
* @return True if the data is available, false otherwise. * @return True if the data is available, false otherwise.
*/ */
private synchronized boolean isDataAvailable() { private synchronized boolean isDataAvailable() {
// If this feature is disabled, just stop
if (!settings.getProperty(ProtectionSettings.ENABLE_GEOIP)) {
enabled = false;
return false;
}
if (downloading) { if (downloading) {
// we are currently downloading the database // we are currently downloading the database
return false; return false;
@ -280,6 +289,9 @@ public class GeoIpService {
* or "--" if it cannot be fetched. * or "--" if it cannot be fetched.
*/ */
public String getCountryCode(String ip) { public String getCountryCode(String ip) {
if (!enabled) {
return "--";
}
if (InternetProtocolUtils.isLocalAddress(ip)) { if (InternetProtocolUtils.isLocalAddress(ip)) {
return "LOCALHOST"; return "LOCALHOST";
} }
@ -293,6 +305,9 @@ public class GeoIpService {
* @return The name of the country, "LocalHost" for local addresses, or "N/A" if it cannot be fetched. * @return The name of the country, "LocalHost" for local addresses, or "N/A" if it cannot be fetched.
*/ */
public String getCountryName(String ip) { public String getCountryName(String ip) {
if (!enabled) {
return "N/A";
}
if (InternetProtocolUtils.isLocalAddress(ip)) { if (InternetProtocolUtils.isLocalAddress(ip)) {
return "LocalHost"; return "LocalHost";
} }

View File

@ -20,6 +20,10 @@ 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("Enable GeoIp database")
public static final Property<Boolean> ENABLE_GEOIP =
newProperty("Protection.geoIpDatabase.enabled", true);
@Comment({"The MaxMind clientId used to download the GeoIp database,", @Comment({"The MaxMind clientId used to download the GeoIp database,",
"get one at https://www.maxmind.com/en/accounts/current/license-key", "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 EssentialsX project has a very useful tutorial on how to generate",