Files
backend
proxy
build.gradle
settings.gradle
Code
13 329
backend/src/main/java/de/derblondekoenig/allcommand/backend/AllCommandBackendPlugin.java
package de.derblondekoenig.allcommand.backend;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;
public final class AllCommandBackendPlugin extends JavaPlugin implements PluginMessageListener {
private static final String CHANNEL = "kodari:allcommand";
private static final long MAX_AGE_MILLIS = 30_000L;
private final Map<String, Long> receivedNonces = new HashMap<>();
private String secret;
@Override
public void onEnable() {
saveDefaultConfig();
FileConfiguration config = getConfig();
secret = config.getString("secret", "");
if (secret == null || secret.length() < 32 || secret.startsWith("REPLACE-")) {
getLogger().severe("Set the same secret as the proxy (at least 32 characters) in config.yml.");
getServer().getPluginManager().disablePlugin(this);
return;
}
getServer().getMessenger().registerIncomingPluginChannel(this, CHANNEL, this);
}
@Override
public void onDisable() {
getServer().getMessenger().unregisterIncomingPluginChannel(this, CHANNEL, this);
}
@Override
public void onPluginMessageReceived(String channel, org.bukkit.entity.Player player, byte[] message) {
if (!CHANNEL.equals(channel)) {
return;
}
try {
CommandProtocol.Packet packet = CommandProtocol.decode(secret, message);
if (packet == null) {
return;
}
long now = System.currentTimeMillis();
if (packet.timestamp < now - MAX_AGE_MILLIS || packet.timestamp > now + MAX_AGE_MILLIS) {
return;
}
receivedNonces.entrySet().removeIf(entry -> entry.getValue() < now - MAX_AGE_MILLIS);
if (receivedNonces.containsKey(packet.nonce)) {
return;
}
receivedNonces.put(packet.nonce, packet.timestamp);
String command = packet.command;
if (command.isEmpty() || command.contains("\n") || command.contains("\r")) {
return;
}
while (command.startsWith("/")) {
command = command.substring(1);
}
if (!command.isEmpty()) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
}
} catch (Exception exception) {
getLogger().warning("Rejected an invalid all-command message: " + exception.getMessage());
}
}
}