Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

Commit

Permalink
Changes -> Added /np whitelist/blacklist + some code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
EinfacheSache committed Jul 27, 2023
1 parent c9f9fc1 commit 1bc7630
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ public void onTabComplete(TabCompleteEvent event) {
ArrayList<String> commands = new ArrayList<>();
ArrayList<String> tabListOne = new ArrayList<>();
ArrayList<String> tabListTwo = new ArrayList<>();
ArrayList<String> 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) {
Expand All @@ -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");
Expand All @@ -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));
}


Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/cubeattack/neoprotect/core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/de/cubeattack/neoprotect/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ private void command(ExecutorBuilder executorBuilder) {
break;
}

case "whitelist": case "blacklist": {
firewall(args);
break;
}

case "debugtool": {
debugTool(args);
break;
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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);
}
});
}
Expand Down Expand Up @@ -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");
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/de/cubeattack/neoprotect/core/model/Firewall.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -168,6 +172,34 @@ public List<Backend> getBackends() {
return list;
}

public List<Firewall> getFirewall(String mode) {
List<Firewall> 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");
Expand All @@ -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);
}
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,25 @@ public List<String> 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;
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,42 @@ public CompletableFuture<List<String>> 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) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/language_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/language_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1bc7630

Please sign in to comment.