Files
src
build.gradle
manifest.kod
README.md
settings.gradle
Code
12 977
src/main/java/com/apexsmp/ipguard/ApexGuardPlugin.java
package com.apexsmp.ipguard;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.logging.Level;
public final class ApexGuardPlugin extends JavaPlugin {
private volatile IpDatabase database;
private IpHasher ipHasher;
private AuthMeBridge authMeBridge;
private Set<String> registrationCommands = Set.of("register", "reg");
private boolean verificationEnabled;
private boolean strictIpMatching;
private boolean allowMultipleTrustedIps;
private boolean logRejections;
private String unauthorizedMessage;
private String verificationErrorMessage;
@Override
public void onEnable() {
saveDefaultConfig();
try {
ipHasher = IpHasher.loadOrCreate(getDataFolder().toPath().resolve("ip-hash.key"));
} catch (IOException exception) {
getLogger().log(Level.SEVERE, "Could not load or create the IP-hashing key; disabling the plugin.", exception);
getServer().getPluginManager().disablePlugin(this);
return;
}
reloadGuardSettings();
getServer().getPluginManager().registerEvents(new IpGuardListener(this), this);
PluginCommand command = getCommand("apexguard");
if (command == null) {
getLogger().severe("The apexguard command is missing from plugin.yml.");
getServer().getPluginManager().disablePlugin(this);
return;
}
ApexGuardCommand executor = new ApexGuardCommand(this);
command.setExecutor(executor);
command.setTabCompleter(executor);
}
@Override
public void onDisable() {
IpDatabase current = database;
database = null;
if (current != null) {
current.close();
}
}
void reloadGuardSettings() {
reloadConfig();
verificationEnabled = getConfig().getBoolean("ip-verification.enabled", true);
strictIpMatching = getConfig().getBoolean("ip-verification.strict-ip-matching", true);
allowMultipleTrustedIps = getConfig().getBoolean("ip-verification.allow-multiple-trusted-ips", false);
unauthorizedMessage = getConfig().getString("ip-verification.unauthorized-message",
"You Can Only Join From Your Registered IP Address!");
verificationErrorMessage = getConfig().getString("ip-verification.verification-error-message",
"IP verification is unavailable. Please contact an administrator.");
logRejections = getConfig().getBoolean("security.log-rejections", true);
Set<String> configuredCommands = new HashSet<>();
for (String value : getConfig().getStringList("authme.registration-commands")) {
if (value != null && !value.isBlank()) {
configuredCommands.add(value.toLowerCase(Locale.ROOT));
}
}
registrationCommands = Set.copyOf(configuredCommands);
IpDatabase oldDatabase = database;
database = null;
if (oldDatabase != null) {
oldDatabase.close();
}
try {
database = new IpDatabase(this, getConfig());
getLogger().info("IP registration database is ready.");
} catch (Exception exception) {
getLogger().log(Level.SEVERE, "Could not initialize the registration database. Connections will fail closed.", exception);
}
authMeBridge = new AuthMeBridge(this, getConfig().getBoolean("authme.enabled", true));
if (getConfig().getBoolean("authme.enabled", true) && !authMeBridge.isAvailable()) {
getLogger().severe("AuthMe's registration API is unavailable. New accounts will be denied until it is available.");
}
}
IpDatabase getDatabase() {
return database;
}
IpHasher getIpHasher() {
return ipHasher;
}
Boolean isAuthMeRegistered(String username) {
return authMeBridge == null ? null : authMeBridge.isRegistered(username);
}
boolean isAuthMeAvailable() {
return authMeBridge != null && authMeBridge.isAvailable();
}
boolean isRegistrationCommand(String command) {
return registrationCommands.contains(command.toLowerCase(Locale.ROOT));
}
boolean isVerificationEnabled() {
return verificationEnabled;
}
boolean isStrictIpMatching() {
return strictIpMatching;
}
boolean allowsMultipleTrustedIps() {
return allowMultipleTrustedIps;
}
String getUnauthorizedMessage() {
return unauthorizedMessage;
}
String getVerificationErrorMessage() {
return verificationErrorMessage;
}
boolean logsRejections() {
return logRejections;
}
static boolean isValidUsername(String username) {
return username != null && username.matches("[A-Za-z0-9_]{3,16}");
}
}