From 1bc7630adff226b32f4c70f97e7430330fa87786 Mon Sep 17 00:00:00 2001 From: EinfacheSache Date: Thu, 27 Jul 2023 17:32:31 +0200 Subject: [PATCH] Changes -> Added /np whitelist/blacklist + some code changes --- .../command/NeoProtectTabCompleter.java | 18 ++++-- .../de/cubeattack/neoprotect/core/Config.java | 6 +- .../de/cubeattack/neoprotect/core/Core.java | 3 + .../core/executor/NeoProtectExecutor.java | 60 ++++++++++++++++++- .../neoprotect/core/model/Firewall.java | 19 ++++++ .../core/request/RestAPIRequests.java | 38 +++++++++++- .../command/NeoProtectTabCompleter.java | 13 +++- .../velocity/command/NeoProtectCommand.java | 50 +++++++++------- src/main/resources/language_de.properties | 4 ++ src/main/resources/language_en.properties | 4 ++ 10 files changed, 176 insertions(+), 39 deletions(-) create mode 100644 src/main/java/de/cubeattack/neoprotect/core/model/Firewall.java diff --git a/src/main/java/de/cubeattack/neoprotect/bungee/command/NeoProtectTabCompleter.java b/src/main/java/de/cubeattack/neoprotect/bungee/command/NeoProtectTabCompleter.java index 4b417b9..0ee05b6 100644 --- a/src/main/java/de/cubeattack/neoprotect/bungee/command/NeoProtectTabCompleter.java +++ b/src/main/java/de/cubeattack/neoprotect/bungee/command/NeoProtectTabCompleter.java @@ -26,18 +26,21 @@ public void onTabComplete(TabCompleteEvent event) { ArrayList commands = new ArrayList<>(); ArrayList tabListOne = new ArrayList<>(); ArrayList tabListTwo = new ArrayList<>(); + ArrayList tabListThree = new ArrayList<>(); commands.add("/np"); commands.add("/neoprotect"); tabListOne.add("setup"); if (instance.getCore().isSetup()) { - tabListOne.add("ipanic"); - tabListOne.add("toggle"); - tabListOne.add("debugtool"); - tabListOne.add("analytics"); tabListOne.add("setgameshield"); tabListOne.add("setbackend"); + tabListOne.add("analytics"); + tabListOne.add("debugTool"); + tabListOne.add("whitelist"); + tabListOne.add("blacklist"); + tabListOne.add("ipanic"); + tabListOne.add("toggle"); if (cursorSplit.length >= 2 && cursorSplit[1].equalsIgnoreCase("debugTool")) { for (int i = 10; i <= 90; i = i + 10) { @@ -46,6 +49,11 @@ public void onTabComplete(TabCompleteEvent event) { tabListTwo.add("cancel"); } + if (cursorSplit.length >= 2 && (cursorSplit[1].equalsIgnoreCase("whitelist") || cursorSplit[1].equalsIgnoreCase("blacklist"))) { + tabListTwo.add("add"); + tabListTwo.add("remove"); + } + if (cursorSplit.length >= 2 && cursorSplit[1].equalsIgnoreCase("toggle")) { tabListTwo.add("antiVPN"); tabListTwo.add("anycast"); @@ -58,7 +66,7 @@ public void onTabComplete(TabCompleteEvent event) { } } - event.getSuggestions().addAll(completer(true, cursor, commands, tabListOne, tabListTwo)); + event.getSuggestions().addAll(completer(true, cursor, commands, tabListOne, tabListTwo, tabListThree)); } diff --git a/src/main/java/de/cubeattack/neoprotect/core/Config.java b/src/main/java/de/cubeattack/neoprotect/core/Config.java index fec3c58..118a708 100644 --- a/src/main/java/de/cubeattack/neoprotect/core/Config.java +++ b/src/main/java/de/cubeattack/neoprotect/core/Config.java @@ -38,11 +38,11 @@ public static void loadConfig(Core core, FileUtils config) { core.severe("Failed to load API-Key. Key is null or not valid"); return; } - if (GameShieldID.equals("")) { + if (GameShieldID.isEmpty()) { core.severe("Failed to load GameshieldID. ID is null"); return; } - if (BackendID.equals("")) { + if (BackendID.isEmpty()) { core.severe("Failed to load BackendID. ID is null"); return; } @@ -80,7 +80,7 @@ public static boolean isDebugMode() { return debugMode; } - public static VersionUtils.UpdateSetting isAutoUpdater() { + public static VersionUtils.UpdateSetting getAutoUpdaterSettings() { return VersionUtils.UpdateSetting.getByNameOrDefault(updateSetting); } diff --git a/src/main/java/de/cubeattack/neoprotect/core/Core.java b/src/main/java/de/cubeattack/neoprotect/core/Core.java index ac97d11..552b6cf 100644 --- a/src/main/java/de/cubeattack/neoprotect/core/Core.java +++ b/src/main/java/de/cubeattack/neoprotect/core/Core.java @@ -67,6 +67,9 @@ public void warn(String output) { public void severe(String output) { LogManager.getLogger().error(output); } + public void severe(String output, Throwable t) { + LogManager.getLogger().error(output, t); + } public String getPrefix() { return prefix; diff --git a/src/main/java/de/cubeattack/neoprotect/core/executor/NeoProtectExecutor.java b/src/main/java/de/cubeattack/neoprotect/core/executor/NeoProtectExecutor.java index 6194c69..cd20bf0 100644 --- a/src/main/java/de/cubeattack/neoprotect/core/executor/NeoProtectExecutor.java +++ b/src/main/java/de/cubeattack/neoprotect/core/executor/NeoProtectExecutor.java @@ -84,6 +84,11 @@ private void command(ExecutorBuilder executorBuilder) { break; } + case "whitelist": case "blacklist": { + firewall(args); + break; + } + case "debugtool": { debugTool(args); break; @@ -154,13 +159,60 @@ private void toggle(String[] args) { return; } + if (response == -1) { + instance.sendMessage(sender, "§cCan not found setting '" + args[1] + "'"); + return; + } + instance.sendMessage(sender, localization.get("command.toggle", args[1], localization.get(response == 1 ? "utils.activated" : "utils.deactivated"))); } } + private void firewall(String[] args) { + if (args.length == 1) { + instance.sendMessage(sender, "§7§l----- §bFirewall (" + args[0].toUpperCase() + ")§7§l -----"); + instance.getCore().getRestAPI().getFirewall(args[0]).forEach((firewall -> + instance.sendMessage(sender, "IP: " + firewall.getIp() + " ID(" + firewall.getId() + ")"))); + } else if (args.length == 3) { + String ip = args[2]; + String action = args[1]; + String mode = args[0].toUpperCase(); + int response = instance.getCore().getRestAPI().updateFirewall(ip, action, mode); + + if (response == -1) { + instance.sendMessage(sender, localization.get("usage.firewall")); + return; + } + + if (response == 0) { + instance.sendMessage(sender, localization.get("command.firewall.notfound", ip, mode)); + return; + } + + if (response == 400) { + instance.sendMessage(sender, localization.get("command.firewall.ip-invalid", ip)); + return; + } + + if (response == 403) { + instance.sendMessage(sender, localization.get("err.upgrade-plan")); + return; + } + + if (response == 429) { + instance.sendMessage(sender, localization.get("err.rate-limit")); + return; + } + + instance.sendMessage(sender, (action.equalsIgnoreCase("add") ? "Added '" : "Removed '") + ip + "' to firewall (" + mode + ")"); + } else { + instance.sendMessage(sender, localization.get("usage.firewall")); + } + } + private void analytics() { - instance.sendMessage(sender, "§7§l-------- §bAnalytics §7§l--------"); + instance.sendMessage(sender, "§7§l--------- §bAnalytics §7§l---------"); JSONObject analytics = instance.getCore().getRestAPI().getAnalytics(); instance.getCore().getRestAPI().getAnalytics().keySet().forEach(ak -> { if (ak.equals("bandwidth")) { @@ -330,8 +382,8 @@ public void run() { instance.sendMessage(sender, localization.get("debug.finished.first") + " (took " + (System.currentTimeMillis() - startTime) + "ms)"); instance.sendMessage(sender, localization.get("debug.finished.second") + file.getAbsolutePath() + " " + localization.get("utils.copy"), "COPY_TO_CLIPBOARD", file.getAbsolutePath(), null, null); instance.getCore().setDebugRunning(false); - } catch (Exception e) { - e.printStackTrace(); + } catch (Exception ex) { + instance.getCore().severe(ex.getMessage(), ex); } }); } @@ -399,6 +451,8 @@ private void showHelp() { instance.sendMessage(sender, " - /np ipanic"); instance.sendMessage(sender, " - /np analytics"); instance.sendMessage(sender, " - /np toggle (option)"); + instance.sendMessage(sender, " - /np whitelist (add/remove) (ip)"); + instance.sendMessage(sender, " - /np blacklist (add/remove) (ip)"); instance.sendMessage(sender, " - /np debugTool (cancel / amount)"); instance.sendMessage(sender, " - /np setgameshield"); instance.sendMessage(sender, " - /np setbackend"); diff --git a/src/main/java/de/cubeattack/neoprotect/core/model/Firewall.java b/src/main/java/de/cubeattack/neoprotect/core/model/Firewall.java new file mode 100644 index 0000000..07d4f29 --- /dev/null +++ b/src/main/java/de/cubeattack/neoprotect/core/model/Firewall.java @@ -0,0 +1,19 @@ +package de.cubeattack.neoprotect.core.model; + +public class Firewall { + String ip; + String id; + + public Firewall(String ip, String id) { + this.ip = ip; + this.id = id; + } + + public String getIp() { + return ip; + } + + public String getId() { + return id; + } +} diff --git a/src/main/java/de/cubeattack/neoprotect/core/request/RestAPIRequests.java b/src/main/java/de/cubeattack/neoprotect/core/request/RestAPIRequests.java index 5b01a39..3971327 100644 --- a/src/main/java/de/cubeattack/neoprotect/core/request/RestAPIRequests.java +++ b/src/main/java/de/cubeattack/neoprotect/core/request/RestAPIRequests.java @@ -11,6 +11,7 @@ import de.cubeattack.neoprotect.core.JsonBuilder; import de.cubeattack.neoprotect.core.Permission; import de.cubeattack.neoprotect.core.model.Backend; +import de.cubeattack.neoprotect.core.model.Firewall; import de.cubeattack.neoprotect.core.model.Gameshield; import java.util.ArrayList; @@ -126,6 +127,9 @@ public boolean togglePanicMode() { public int toggle(String mode) { JSONObject settings = rest.request(RequestType.GET_GAMESHIELD_INFO, null, Config.getGameShieldID()).getResponseBodyObject().getJSONObject("gameShieldSettings"); + + if(!settings.has(mode)) return -1; + boolean mitigationSensitivity = settings.getBoolean(mode); if (mitigationSensitivity) { @@ -168,6 +172,34 @@ public List getBackends() { return list; } + public List getFirewall(String mode) { + List list = new ArrayList<>(); + JSONArray firewalls = rest.request(RequestType.GET_FIREWALLS, null, Config.getGameShieldID(), mode.toUpperCase()).getResponseBodyArray(); + + for (Object object : firewalls) { + JSONObject firewallJSON = (JSONObject) object; + list.add(new Firewall(firewallJSON.getString("ip"), firewallJSON.get("id").toString())); + } + + return list; + } + + public int updateFirewall(String ip, String action, String mode) { + if(action.equalsIgnoreCase("REMOVE")){ + Firewall firewall = getFirewall(mode).stream().filter(f -> f.getIp().equals(ip)).findFirst().orElse(null); + + if(firewall == null){ + return 0; + } + + return rest.request(RequestType.DELETE_FIREWALL, null, Config.getGameShieldID(), firewall.getId()).getCode(); + }else if(action.equalsIgnoreCase("ADD")){ + RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new JsonBuilder().appendField("entry", ip).build().toString()); + return rest.request(RequestType.POST_FIREWALL_CREATE, requestBody, Config.getGameShieldID(), mode).getCode(); + } + return -1; + } + private void neoServerIPsUpdateSchedule() { core.info("NeoServerIPsUpdate scheduler started"); @@ -187,7 +219,7 @@ private void versionCheckSchedule() { new Timer().schedule(new TimerTask() { @Override public void run() { - core.setVersionResult(VersionUtils.checkVersion("NeoProtect", "NeoPlugin", "v" + core.getPlugin().getVersion(), Config.isAutoUpdater())); + core.setVersionResult(VersionUtils.checkVersion("NeoProtect", "NeoPlugin", "v" + core.getPlugin().getVersion(), Config.getAutoUpdaterSettings())); } }, 1000 * 10, 1000 * 60 * 3); } @@ -236,9 +268,9 @@ public void run() { if (ip == null) return; if (ip.equals(backend.getIp())) return; - RequestBody formBody = RequestBody.create(MediaType.parse("application/json"), new JsonBuilder().appendField("ipv4", ip).build().toString()); + RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new JsonBuilder().appendField("ipv4", ip).build().toString()); - if (!updateBackend(formBody)) { + if (!updateBackend(requestBody)) { core.warn("Update backendserver ID '" + Config.getBackendID() + "' to IP '" + ip + "' failed"); } else { core.info("Update backendserver ID '" + Config.getBackendID() + "' to IP '" + ip + "' success"); diff --git a/src/main/java/de/cubeattack/neoprotect/spigot/command/NeoProtectTabCompleter.java b/src/main/java/de/cubeattack/neoprotect/spigot/command/NeoProtectTabCompleter.java index 6089fd6..371ca49 100644 --- a/src/main/java/de/cubeattack/neoprotect/spigot/command/NeoProtectTabCompleter.java +++ b/src/main/java/de/cubeattack/neoprotect/spigot/command/NeoProtectTabCompleter.java @@ -34,6 +34,11 @@ public List onTabComplete(@NotNull CommandSender sender, @NotNull Comman return completorList; } + if (args.length >= 2 && (args[0].equalsIgnoreCase("whitelist") || args[0].equalsIgnoreCase("blacklist"))) { + completorList.add("add"); + completorList.add("remove"); + } + if (args.length != 1) { return completorList; } @@ -41,11 +46,13 @@ public List onTabComplete(@NotNull CommandSender sender, @NotNull Comman list.add("setup"); if (instance.getCore().isSetup()) { - list.add("ipanic"); - list.add("toggle"); - list.add("analytics"); list.add("setgameshield"); list.add("setbackend"); + list.add("analytics"); + list.add("whitelist"); + list.add("blacklist"); + list.add("ipanic"); + list.add("toggle"); } for (String tab : list) { diff --git a/src/main/java/de/cubeattack/neoprotect/velocity/command/NeoProtectCommand.java b/src/main/java/de/cubeattack/neoprotect/velocity/command/NeoProtectCommand.java index 1dbae43..de48c50 100644 --- a/src/main/java/de/cubeattack/neoprotect/velocity/command/NeoProtectCommand.java +++ b/src/main/java/de/cubeattack/neoprotect/velocity/command/NeoProtectCommand.java @@ -50,36 +50,42 @@ public CompletableFuture> suggestAsync(Invocation invocation) { if (args.length == 2) { if (args[0].equalsIgnoreCase("debugtool")) { for (int i = 10; i <= 100; i = i + 10) { - completorList.add(String.valueOf(i)); + list.add(String.valueOf(i)); } - completorList.add("cancel"); + list.add("cancel"); } + + if ((args[0].equalsIgnoreCase("whitelist") || args[0].equalsIgnoreCase("blacklist"))) { + list.add("add"); + list.add("remove"); + } + if (args[0].equalsIgnoreCase("toggle")) { - completorList.add("antiVPN"); - completorList.add("anycast"); - completorList.add("motdCache"); - completorList.add("blockForge"); - completorList.add("ipWhitelist"); - completorList.add("ipBlacklist"); - completorList.add("secureProfiles"); - completorList.add("advancedAntiBot"); + list.add("antiVPN"); + list.add("anycast"); + list.add("motdCache"); + list.add("blockForge"); + list.add("ipWhitelist"); + list.add("ipBlacklist"); + list.add("secureProfiles"); + list.add("advancedAntiBot"); } - return completorList; } - if (args.length > 1) { - return completorList; - } + if (args.length <= 1) { - list.add("setup"); + list.add("setup"); - if (instance.getCore().isSetup()) { - list.add("setgameshield"); - list.add("setbackend"); - list.add("analytics"); - list.add("debugTool"); - list.add("ipanic"); - list.add("toggle"); + if (instance.getCore().isSetup()) { + list.add("setgameshield"); + list.add("setbackend"); + list.add("analytics"); + list.add("debugTool"); + list.add("whitelist"); + list.add("blacklist"); + list.add("ipanic"); + list.add("toggle"); + } } for (String tab : list) { diff --git a/src/main/resources/language_de.properties b/src/main/resources/language_de.properties index 2bb9e6e..f238be2 100644 --- a/src/main/resources/language_de.properties +++ b/src/main/resources/language_de.properties @@ -9,13 +9,17 @@ setup.finished=§6Setup wurde erfolgreich abgeschlossen available.commands=Verfügbare Befehle: console.command=Sie können diesen Befehl nicht über die Konsole ausführen. Bitte führe diesen Befehl in-game aus. + command.setup=Geben Sie nun den API-KEY im Chat ein command.ipanic=Der Panikmodus wurde {0} command.toggle={0} wurde {1} +command.firewall.notfound=§cIP '{0}' ist nicht in der Firewall ({1}) +command.firewall.ip-invalid=§cIP '{0}' ist nicht gültig usage.debug=Bitte geben Sie /np debugTool (cancel / zahl) ein usage.ipanic=Bitte geben Sie /np ipanic ein usage.toggle=Bitte geben Sie /np toggle (Option) ein +usage.firewall=Bitte geben Sie /np (whitelist/blacklist) (add/remove) (ip) usage.setbackend=Bitte geben Sie /np setbackend ein usage.setgameshield=Bitte geben Sie /np setgameshield ein diff --git a/src/main/resources/language_en.properties b/src/main/resources/language_en.properties index 25bb68e..242d8ef 100644 --- a/src/main/resources/language_en.properties +++ b/src/main/resources/language_en.properties @@ -9,13 +9,17 @@ setup.finished=§6Setup has been successfully completed available.commands=Available commands: console.command=You cannot execute this command via the console. Please run this command in-game. + command.setup=Now enter the API-KEY in the Chat command.ipanic=Panic mode has been {0} command.toggle={0} has been {1} +command.firewall.ip-invalid=§cIP '{0}' is not valid +command.firewall.notfound=§cIP '{0}' is not on the firewall ({1}) usage.debug=Please type /np debugTool (cancel / amount) usage.ipanic=Please type /np ipanic usage.toggle=Please type /np toggle (option) +usage.firewall=Please type /np (whitelist/blacklist) (add/remove) (ip) usage.setbackend=Please type /np setbackend usage.setgameshield=Please enter /np setgameshield