diff --git a/src/main/java/ecb/ajneb97/core/managers/ConfigManager.java b/src/main/java/ecb/ajneb97/core/managers/ConfigManager.java index 7e7f28b..9e89501 100644 --- a/src/main/java/ecb/ajneb97/core/managers/ConfigManager.java +++ b/src/main/java/ecb/ajneb97/core/managers/ConfigManager.java @@ -6,7 +6,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; import java.util.ArrayList; +import java.util.Base64; import java.util.List; public class ConfigManager { @@ -17,14 +20,14 @@ public class ConfigManager { private String originalFileName; private String newFileName; - public ConfigManager(Path dataDirectoryPath,String originalFileName,String newFileName){ + public ConfigManager(Path dataDirectoryPath, String originalFileName, String newFileName) { this.dataDirectoryPath = dataDirectoryPath; this.originalFileName = originalFileName; this.newFileName = newFileName; } public void registerConfig() { - String path = dataDirectoryPath+File.separator+newFileName; + String path = dataDirectoryPath + File.separator + newFileName; createDefaultConfigFile(); yamlFile = new YamlFile(path); @@ -39,14 +42,14 @@ public void registerConfig() { } } - public void createDefaultConfigFile(){ + public void createDefaultConfigFile() { File dataDirectoryFile = new File(dataDirectoryPath.toString()); - if (!dataDirectoryFile.exists()){ + if (!dataDirectoryFile.exists()) { dataDirectoryFile.mkdir(); } File configFile = new File(dataDirectoryFile, newFileName); try { - if (!configFile.exists()){ + if (!configFile.exists()) { Files.copy(this.getClass().getClassLoader().getResourceAsStream(originalFileName), configFile.toPath()); } } catch (IOException e) { @@ -59,21 +62,33 @@ public YamlFile getConfig() { } public void checkMessagesUpdate() { - Path configFile = Paths.get(dataDirectoryPath+File.separator+newFileName); + Path configFile = Paths.get(dataDirectoryPath + File.separator + newFileName); try { String configText = new String(Files.readAllBytes(configFile)); - if(!configText.contains("custom_commands_actions:")){ - List list = new ArrayList(); + if (!yamlFile.contains("actions.enabled")) { + yamlFile.set("actions.enabled", true); + yamlFile.save(); + yamlFile.load(); + } + + if (!yamlFile.contains("actions.key")) { + yamlFile.set("actions.key", generateKey(16)); + yamlFile.save(); + yamlFile.load(); + } + + if (!configText.contains("custom_commands_actions:")) { + List list = new ArrayList<>(); list.add("/bungee"); yamlFile.set("custom_commands_actions.example1.commands", list); - list = new ArrayList(); + list = new ArrayList<>(); list.add("message: &8[&b&lECB&8] &cYou can't see the BungeeCord version!"); yamlFile.set("custom_commands_actions.example1.actions", list); yamlFile.save(); yamlFile.load(); } - if(!configText.contains("blocked_command_default_actions:")){ + if (!configText.contains("blocked_command_default_actions:")) { List list = new ArrayList(); list.add("message: &8[&b&lECB&8] &cYou don't have permissions to use that command."); list.add("playsound: BLOCK_NOTE_BLOCK_PLING;10;0.1"); @@ -82,8 +97,19 @@ public void checkMessagesUpdate() { yamlFile.save(); yamlFile.load(); } - }catch(IOException e){ - e.printStackTrace(); + } catch (IOException | NoSuchAlgorithmException e) { + throw new RuntimeException(e); } } + + public String generateKey(int length) throws NoSuchAlgorithmException { + SecureRandom secureRandom = new SecureRandom(); + byte[] key = new byte[length]; + secureRandom.nextBytes(key); + return Base64.getEncoder().encodeToString(key); + } + + public YamlFile getYamlFile() { + return yamlFile; + } } diff --git a/src/main/java/ecb/ajneb97/spigot/EasyCommandBlocker.java b/src/main/java/ecb/ajneb97/spigot/EasyCommandBlocker.java index c58ada1..c980439 100644 --- a/src/main/java/ecb/ajneb97/spigot/EasyCommandBlocker.java +++ b/src/main/java/ecb/ajneb97/spigot/EasyCommandBlocker.java @@ -38,7 +38,9 @@ public void onEnable(){ commandsManager = new CommandsManager(configManager.getConfig()); registerCommands(); registerEvents(); - bungeeMessagingManager = new BungeeMessagingManager(this); + if (configManager.getYamlFile().get("settings.bungeecord") != null && configManager.getYamlFile().getBoolean("settings.bungeecord")) { + bungeeMessagingManager = new BungeeMessagingManager(this); + } protocolLibManager = new ProtocolLibManager(this); viaVersionManager = new ViaVersionManager(this); @@ -79,6 +81,10 @@ public void registerCommands(){ this.getCommand("ecb").setExecutor(new MainCommand(this)); } + public ConfigManager getConfigManager() { + return configManager; + } + public void registerEvents() { PluginManager pm = getServer().getPluginManager(); pm.registerEvents(new PlayerListener(this), this); diff --git a/src/main/java/ecb/ajneb97/spigot/managers/BungeeMessagingManager.java b/src/main/java/ecb/ajneb97/spigot/managers/BungeeMessagingManager.java index cfb6ceb..97ce345 100644 --- a/src/main/java/ecb/ajneb97/spigot/managers/BungeeMessagingManager.java +++ b/src/main/java/ecb/ajneb97/spigot/managers/BungeeMessagingManager.java @@ -12,8 +12,11 @@ public class BungeeMessagingManager implements PluginMessageListener { private EasyCommandBlocker plugin; - public BungeeMessagingManager(EasyCommandBlocker plugin){ - if (!Bukkit.getServer().spigot().getConfig().getBoolean( "settings.bungeecord" ) ) { + + public BungeeMessagingManager(EasyCommandBlocker plugin) { + this.plugin = plugin; + + if (!Bukkit.getServer().spigot().getConfig().getBoolean("settings.bungeecord")) { return; } @@ -22,16 +25,25 @@ public BungeeMessagingManager(EasyCommandBlocker plugin){ @Override public void onPluginMessageReceived(String channel, Player player, byte[] bytes) { - if(!channel.equalsIgnoreCase(GlobalVariables.bungeeMainChannel)) { + if (!channel.equalsIgnoreCase(GlobalVariables.bungeeMainChannel)) { return; } ByteArrayDataInput in = ByteStreams.newDataInput(bytes); String subChannel = in.readUTF(); - if(subChannel.equalsIgnoreCase( GlobalVariables.bungeeActionsSubChannel)) { - String data = in.readUTF(); - ActionsUtils.executeAction(data,player); + String key = in.readUTF(); + if (!plugin.getConfigManager().getYamlFile().contains("actions.key")) { + plugin.getLogger().warning("No key found in config.yml, but continue.."); + } + if (!key.equals(plugin.getConfigManager().getYamlFile().getString("actions.key"))) { + plugin.getLogger().warning("Player attempting password bypass: " + player.getName() + ":" + player.getUniqueId()); + return; + } + + if (subChannel.equalsIgnoreCase(GlobalVariables.bungeeActionsSubChannel)) { + String data = in.readUTF(); + ActionsUtils.executeAction(data, player); } } }