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

Commit

Permalink
Change -> Debug tool now collects more data
Browse files Browse the repository at this point in the history
  • Loading branch information
EinfacheSache committed Jul 13, 2023
1 parent 24cc11f commit 7ce302f
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import de.cubeattack.neoprotect.core.Core;
import de.cubeattack.neoprotect.core.NeoProtectPlugin;
import de.cubeattack.neoprotect.core.Permission;
import de.cubeattack.neoprotect.core.model.KeepAliveResponseKey;
import de.cubeattack.neoprotect.core.model.debugtool.KeepAliveResponseKey;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import de.cubeattack.api.util.JavaUtils;
import de.cubeattack.neoprotect.bungee.NeoProtectBungee;
import de.cubeattack.neoprotect.core.Config;
import de.cubeattack.neoprotect.core.model.DebugPingResponse;
import de.cubeattack.neoprotect.core.model.KeepAliveResponseKey;
import de.cubeattack.neoprotect.core.model.debugtool.DebugPingResponse;
import de.cubeattack.neoprotect.core.model.debugtool.KeepAliveResponseKey;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
Expand All @@ -26,6 +26,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
Expand Down Expand Up @@ -121,7 +122,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
backendRTT = tcpInfoBackend.rtt() / 1000;
}

instance.getCore().getDebugPingResponses().put(player.getName(), new DebugPingResponse(ping, neoRTT, backendRTT));
ConcurrentHashMap<String, ArrayList<DebugPingResponse>> map = instance.getCore().getDebugPingResponses();

if(!map.containsKey(player.getName())) {
instance.getCore().getDebugPingResponses().put(player.getName(), new ArrayList<>());
}

map.get(player.getName()).add(new DebugPingResponse(ping, neoRTT, backendRTT));

}
instance.getCore().getPingMap().remove(keepAliveResponseKey);
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/de/cubeattack/neoprotect/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import de.cubeattack.api.logger.LogManager;
import de.cubeattack.api.util.FileUtils;
import de.cubeattack.api.util.VersionUtils;
import de.cubeattack.neoprotect.core.model.DebugPingResponse;
import de.cubeattack.neoprotect.core.model.KeepAliveResponseKey;
import de.cubeattack.neoprotect.core.model.debugtool.DebugPingResponse;
import de.cubeattack.neoprotect.core.model.debugtool.KeepAliveResponseKey;
import de.cubeattack.neoprotect.core.request.RestAPIRequests;

import java.io.File;
Expand All @@ -27,14 +27,14 @@ public class Core {
private final RestAPIRequests restAPIRequests;
private final NeoProtectPlugin plugin;
private final Localization localization;

private final List<Object> PLAYER_IN_SETUP = new ArrayList<>();
private final ConcurrentHashMap<KeepAliveResponseKey, Long> pingMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, Timestamp> timestampsMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, DebugPingResponse> debugPingResponses = new ConcurrentHashMap<>();

private final ConcurrentHashMap<String, ArrayList<DebugPingResponse>> debugPingResponses = new ConcurrentHashMap<>();
private final ExecutorService executorService;

private VersionUtils.Result versionResult;
private boolean isDebugRunning = false;

public Core(NeoProtectPlugin plugin) {
LogManager.getLogger().setLogger(plugin.getLogger());
Expand Down Expand Up @@ -71,6 +71,14 @@ public String getPrefix() {
return prefix;
}

public boolean isDebugRunning() {
return isDebugRunning;
}

public void setDebugRunning(boolean debugRunning) {
isDebugRunning = debugRunning;
}

public NeoProtectPlugin getPlugin() {
return plugin;
}
Expand Down Expand Up @@ -107,7 +115,7 @@ public ConcurrentHashMap<Long, Timestamp> getTimestampsMap() {
return timestampsMap;
}

public ConcurrentHashMap<String, DebugPingResponse> getDebugPingResponses() {
public ConcurrentHashMap<String, ArrayList<DebugPingResponse>> getDebugPingResponses() {
return debugPingResponses;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.cubeattack.neoprotect.core.NeoProtectPlugin;
import de.cubeattack.neoprotect.core.model.Backend;
import de.cubeattack.neoprotect.core.model.Gameshield;
import de.cubeattack.neoprotect.core.model.debugtool.DebugPingResponse;

import java.io.File;
import java.sql.Timestamp;
Expand Down Expand Up @@ -77,7 +78,7 @@ private void command(ExecutorBuilder executorBuilder) {
}

case "debugping": {
debugPing();
debugTool();
break;
}

Expand Down Expand Up @@ -113,6 +114,23 @@ private void command(ExecutorBuilder executorBuilder) {
}
}


private void setup(Object sender) {
instance.getCore().getPlayerInSetup().add(sender);
instance.sendMessage(sender, localization.get("command.setup") + localization.get("utils.click"),
"OPEN_URL", "https://panel.neoprotect.net/profile",
"SHOW_TEXT", localization.get("apikey.find"));
}

private void iPanic(Object sender, String[] args) {
if (args.length != 1) {
instance.sendMessage(sender, localization.get("usage.ipanic"));
} else {
instance.sendMessage(sender, localization.get("command.ipanic",
localization.get(instance.getCore().getRestAPI().togglePanicMode() ? "utils.activated" : "utils.deactivated")));
}
}

private void analytics() {
instance.sendMessage(sender, "§7§l-------- §bAnalytics §7§l--------");
JSONObject analytics = instance.getCore().getRestAPI().getAnalytics();
Expand Down Expand Up @@ -147,27 +165,11 @@ private void analytics() {
});
}

private void setup(Object sender) {
instance.getCore().getPlayerInSetup().add(sender);
instance.sendMessage(sender, localization.get("command.setup") + localization.get("utils.click"),
"OPEN_URL", "https://panel.neoprotect.net/profile",
"SHOW_TEXT", localization.get("apikey.find"));
}

private void iPanic(Object sender, String[] args) {
if (args.length != 1) {
instance.sendMessage(sender, localization.get("usage.ipanic"));
} else {
instance.sendMessage(sender, localization.get("command.ipanic",
localization.get(instance.getCore().getRestAPI().togglePanicMode() ? "utils.activated" : "utils.deactivated")));
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private void debugPing() {
private void debugTool() {

if (instance.getPluginType() == NeoProtectPlugin.PluginType.SPIGOT) {
instance.sendMessage(sender, "This command is only available for proxy-server");
instance.sendMessage(sender, localization.get("debug.spigot"));
return;
}

Expand All @@ -176,34 +178,111 @@ private void debugPing() {
return;
}

instance.getCore().getTimestampsMap().put(instance.sendKeepAliveMessage(new Random().nextInt(90) * 10000 + 1337), new Timestamp(System.currentTimeMillis()));
instance.sendMessage(sender, "§aSending KeepAlivePackets");
if (instance.getCore().isDebugRunning()) {
instance.sendMessage(sender, localization.get("debug.running"));
return;
}

Timer time = new Timer();
time.schedule(new TimerTask() {
instance.getCore().setDebugRunning(true);
instance.sendMessage(sender, localization.get("debug.starting"));

new Timer().schedule(new TimerTask() {
int counter = 0;
@Override
public void run() {
try {
File file = new File("plugins/NeoProtect/debug" + "/" + new Timestamp(System.currentTimeMillis()) + ".yml");
YamlConfiguration configuration = new YamlConfiguration();

if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}

configuration.load(file);
counter++;
instance.getCore().getTimestampsMap().put(instance.sendKeepAliveMessage(new Random().nextInt(90) * 10000 + 1337), new Timestamp(System.currentTimeMillis()));
instance.sendMessage(sender, localization.get("debug.sendingPackets") + " (" + counter + "/5)");
if(counter >= 5)this.cancel();
}
},500, 2000);

instance.getCore().getDebugPingResponses().keySet().forEach((playerName -> configuration.set("players." + playerName, new JSONObject(instance.getCore().getDebugPingResponses().get(playerName)).toMap())));

configuration.save(file);
instance.sendMessage(sender, "§aDebug file has been created");
} catch (Exception e) {
e.printStackTrace();
}
new Timer().schedule(new TimerTask() {
@Override
public void run() {
instance.getCore().getExecutorService().submit(() -> {
try {
File file = new File("plugins/NeoProtect/debug" + "/" + new Timestamp(System.currentTimeMillis()) + ".yml");
YamlConfiguration configuration = new YamlConfiguration();

if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}

configuration.load(file);

instance.getCore().getDebugPingResponses().keySet().forEach((playerName -> {
List<DebugPingResponse> list = instance.getCore().getDebugPingResponses().get(playerName);

long maxPlayerToProxyLatenz = 0;
long maxNeoToProxyLatenz = 0;
long maxProxyToBackendLatenz = 0;
long maxPlayerToNeoLatenz = 0;

long avgPlayerToProxyLatenz = 0;
long avgNeoToProxyLatenz = 0;
long avgProxyToBackendLatenz = 0;
long avgPlayerToNeoLatenz = 0;

long minPlayerToProxyLatenz = Long.MAX_VALUE;
long minNeoToProxyLatenz = Long.MAX_VALUE;
long minProxyToBackendLatenz = Long.MAX_VALUE;
long minPlayerToNeoLatenz = Long.MAX_VALUE;

for (DebugPingResponse response : list) {
if (maxPlayerToProxyLatenz < response.getPlayerToProxyLatenz())
maxPlayerToProxyLatenz = response.getPlayerToProxyLatenz();
if (maxNeoToProxyLatenz < response.getNeoToProxyLatenz())
maxNeoToProxyLatenz = response.getNeoToProxyLatenz();
if (maxProxyToBackendLatenz < response.getProxyToBackendLatenz())
maxProxyToBackendLatenz = response.getProxyToBackendLatenz();
if (maxPlayerToNeoLatenz < response.getPlayerToNeoLatenz())
maxPlayerToNeoLatenz = response.getPlayerToNeoLatenz();

avgPlayerToProxyLatenz = avgPlayerToProxyLatenz + response.getPlayerToProxyLatenz();
avgNeoToProxyLatenz = avgNeoToProxyLatenz + response.getNeoToProxyLatenz();
avgProxyToBackendLatenz = avgProxyToBackendLatenz + response.getProxyToBackendLatenz();
avgPlayerToNeoLatenz = avgPlayerToNeoLatenz + response.getPlayerToNeoLatenz();

if (minPlayerToProxyLatenz > response.getPlayerToProxyLatenz())
minPlayerToProxyLatenz = response.getPlayerToProxyLatenz();
if (minNeoToProxyLatenz > response.getNeoToProxyLatenz())
minNeoToProxyLatenz = response.getNeoToProxyLatenz();
if (minProxyToBackendLatenz > response.getProxyToBackendLatenz())
minProxyToBackendLatenz = response.getProxyToBackendLatenz();
if (minPlayerToNeoLatenz > response.getPlayerToNeoLatenz())
minPlayerToNeoLatenz = response.getPlayerToNeoLatenz();
}

configuration.set("players." + playerName + ".max.PlayerToProxyLatenz", maxPlayerToProxyLatenz);
configuration.set("players." + playerName + ".max.NeoToProxyLatenz", maxNeoToProxyLatenz);
configuration.set("players." + playerName + ".max.ProxyToBackendLatenz", maxProxyToBackendLatenz);
configuration.set("players." + playerName + ".max.PlayerToNeoLatenz", maxPlayerToNeoLatenz);

configuration.set("players." + playerName + ".average.PlayerToProxyLatenz", avgPlayerToProxyLatenz / list.size());
configuration.set("players." + playerName + ".average.NeoToProxyLatenz", avgNeoToProxyLatenz / list.size());
configuration.set("players." + playerName + ".average.ProxyToBackendLatenz", avgProxyToBackendLatenz / list.size());
configuration.set("players." + playerName + ".average.PlayerToNeoLatenz", avgPlayerToNeoLatenz / list.size());

configuration.set("players." + playerName + ".min.PlayerToProxyLatenz", minPlayerToProxyLatenz);
configuration.set("players." + playerName + ".min.NeoToProxyLatenz", minNeoToProxyLatenz);
configuration.set("players." + playerName + ".min.ProxyToBackendLatenz", minProxyToBackendLatenz);
configuration.set("players." + playerName + ".min.PlayerToNeoLatenz", minPlayerToNeoLatenz);

}));

configuration.save(file);
instance.sendMessage(sender, localization.get("debug.finished.first"));
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();
}
});
}
}, 3000);

}, 2000 * 4 + 2500);
}

private void gameshieldSelector(Object sender) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.cubeattack.neoprotect.core.model;
package de.cubeattack.neoprotect.core.model.debugtool;

@SuppressWarnings("unused")
public class DebugPingResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.cubeattack.neoprotect.core.model;
package de.cubeattack.neoprotect.core.model.debugtool;

import java.net.SocketAddress;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import de.cubeattack.neoprotect.core.Core;
import de.cubeattack.neoprotect.core.NeoProtectPlugin;
import de.cubeattack.neoprotect.core.Permission;
import de.cubeattack.neoprotect.core.model.KeepAliveResponseKey;
import de.cubeattack.neoprotect.core.model.debugtool.KeepAliveResponseKey;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/language_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ setup.command.required=Bitte verwenden Sie zuerst /np setup, bevor Sie andere Be
setup.finished=§6Setup wurde erfolgreich abgeschlossen

available.commands=Verfügbare Befehle:
console.command=Sie können diesen Befehl nicht über die Konsole ausführen
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}

Expand All @@ -33,3 +33,10 @@ utils.activated=§aaktiviert
utils.deactivated=§cdeaktiviert

plugin.outdated.message=§cDas Plugin {0} ist veraltet, bitte downloade die aktuelle Version {1}
debug.spigot=Dieser Befehl ist nur für Proxy-Server verfügbar
debug.running=§cDebug-Tool läuft aktuell bereits
debug.starting=§aDebug-Tool wird gestartet...
debug.sendingPackets=§aSenden von KeepAlivePackets
debug.finished.first=§aDebug-Datei wurde erstellt
debug.finished.second=§aDie Datei finden Sie hier -> §7
utils.copy=(kopieren)
9 changes: 8 additions & 1 deletion src/main/resources/language_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ setup.command.required=Please use /np setup first before using other commands
setup.finished=§6Setup has been successfully completed

available.commands=Available commands:
console.command=You cannot execute this command via the console
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}

Expand All @@ -33,3 +33,10 @@ utils.activated=§aactivated
utils.deactivated=§cdeactivated

plugin.outdated.message=§cThe plugin {0} is outdated, please download the latest version {1}
debug.spigot=This command is only available for proxy-server
debug.running=§cDebug tool is progressing right now
debug.starting=§aStarting debug tool...
debug.sendingPackets=§aSending KeepAlivePackets
debug.finished.first=§aDebug file has been created
debug.finished.second=§aFile can be found here -> §7
utils.copy=(copy)

0 comments on commit 7ce302f

Please sign in to comment.