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

Commit

Permalink
Changes -> Added analytics command, prepared debugPing command, some …
Browse files Browse the repository at this point in the history
…codes changes
  • Loading branch information
EinfacheSache committed Jul 9, 2023
1 parent eaba799 commit e8518d4
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
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 net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.protocol.packet.KeepAlive;
import org.bstats.bungeecord.Metrics;

import java.util.Collections;
Expand Down Expand Up @@ -54,8 +57,22 @@ public void sendAdminMessage(Permission permission, String text, String clickAct
});
}

@Override
public KeepAliveResponseKey sendKeepAliveMessage(Object sender, long id) {
if(sender instanceof ProxiedPlayer){
((ProxiedPlayer)sender).unsafe().sendPacket(new KeepAlive(id));
return new KeepAliveResponseKey(((ProxiedPlayer)sender).getSocketAddress(), id);
}
return null;
}

@Override
public String getVersion() {
return getDescription().getVersion();
}

@Override
public PluginType getPluginType() {
return PluginType.BUNGEECORD;
}
}
2 changes: 1 addition & 1 deletion src/main/java/de/cubeattack/neoprotect/bungee/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ private void register(NeoProtectBungee instance){
PluginManager pm = ProxyServer.getInstance().getPluginManager();
pm.registerCommand(instance, new NeoProtectCommand(instance,"neoprotect", "neoprotect.admin", "np"));

pm.registerListener(instance, new NeoProtectTabCompleter());
pm.registerListener(instance, new ChatListener(instance));
pm.registerListener(instance, new LoginListener(instance));
pm.registerListener(instance, new DisconnectListener(instance));
pm.registerListener(instance, new NeoProtectTabCompleter(instance));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.cubeattack.neoprotect.bungee.command;

import de.cubeattack.neoprotect.core.NeoProtectPlugin;
import net.md_5.bungee.api.event.TabCompleteEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
Expand All @@ -10,6 +11,12 @@

public class NeoProtectTabCompleter implements Listener {

private final NeoProtectPlugin instance;

public NeoProtectTabCompleter(NeoProtectPlugin instance) {
this.instance = instance;
}

@EventHandler(priority = EventPriority.HIGH)
public void onTabComplete(TabCompleteEvent event) {

Expand All @@ -20,9 +27,14 @@ public void onTabComplete(TabCompleteEvent event) {
commands.add("/np");
commands.add("/neoprotect");
tabListOne.add("setup");
tabListOne.add("setgameshield");
tabListOne.add("setbackend");
tabListOne.add("ipanic");

if(instance.getCore().isSetup()){
tabListOne.add("ipanic");
tabListOne.add("debugPing");
tabListOne.add("analytics");
tabListOne.add("setgameshield");
tabListOne.add("setbackend");
}

event.getSuggestions().addAll(completer(true, cursor, commands, tabListOne));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.cubeattack.api.utils.JavaUtils;
import de.cubeattack.neoprotect.bungee.NeoProtectBungee;
import de.cubeattack.neoprotect.core.Config;
import de.cubeattack.neoprotect.core.model.KeepAliveResponseKey;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
Expand All @@ -12,11 +13,14 @@
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.netty.HandlerBoss;
import net.md_5.bungee.netty.PipelineUtils;
import net.md_5.bungee.protocol.PacketWrapper;
import net.md_5.bungee.protocol.packet.KeepAlive;
import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;

public class ProxyProtocol {
Expand All @@ -33,9 +37,10 @@ public ProxyProtocol(NeoProtectBungee instance) {
ChannelInitializer<Channel> channelInitializer = new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) {

try {

AtomicReference<InetSocketAddress> inetAddress = new AtomicReference<>();

initChannelMethod.invoke(bungeeChannelInitializer, channel);

if(channel.localAddress().toString().startsWith("local:"))return;
Expand All @@ -55,19 +60,36 @@ protected void initChannel(Channel channel) {
channel.pipeline().remove("HAProxyMessageDecoder#0");
});


channel.pipeline().addFirst("haproxy-decoder", new HAProxyMessageDecoder());
channel.pipeline().addAfter("haproxy-decoder", "haproxy-handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HAProxyMessage) {
HAProxyMessage message = (HAProxyMessage) msg;
channelWrapperAccessor.get(channel.pipeline().get(HandlerBoss.class)).setRemoteAddress(new InetSocketAddress(message.sourceAddress(), message.sourcePort()));
inetAddress.set(new InetSocketAddress(message.sourceAddress(), message.sourcePort()));
channelWrapperAccessor.get(channel.pipeline().get(HandlerBoss.class)).setRemoteAddress(inetAddress.get());
} else {
super.channelRead(ctx, msg);
}
}
});

channel.pipeline().addAfter(PipelineUtils.PACKET_DECODER, "neo-keep-alive-handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof PacketWrapper && ((PacketWrapper) msg).packet instanceof KeepAlive) {
KeepAlive keepAlive = (KeepAlive) ((PacketWrapper) msg).packet;
for (KeepAliveResponseKey keepAliveResponseKey : instance.getCore().getPingMap().keySet()) {
if(keepAliveResponseKey.getAddress().equals(inetAddress.get()) && keepAliveResponseKey.getId() == keepAlive.getRandomId()){
instance.getLogger().info("Ping: " + (System.currentTimeMillis() - instance.getCore().getPingMap().get(keepAliveResponseKey)) + "ms");
}
}
}

super.channelRead(ctx, msg);
}
});

} catch (Exception ex) {
instance.getLogger().log(Level.SEVERE, "Cannot inject incoming channel " + channel, ex);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/de/cubeattack/neoprotect/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import de.cubeattack.api.logger.LogManager;
import de.cubeattack.api.utils.FileUtils;
import de.cubeattack.api.utils.VersionUtils;
import de.cubeattack.neoprotect.core.model.KeepAliveResponseKey;
import de.cubeattack.neoprotect.core.request.RestAPIRequests;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings("unused")
public class Core {
Expand All @@ -23,6 +25,7 @@ public class Core {
private final Localization localization;

private final List<Object> PLAYER_IN_SETUP = new ArrayList<>();
private final ConcurrentHashMap<KeepAliveResponseKey, Long> pingMap = new ConcurrentHashMap<>();
private VersionUtils.Result versionResult;

public Core(NeoProtectPlugin plugin) {
Expand Down Expand Up @@ -83,6 +86,10 @@ public List<Object> getPlayerInSetup() {
return PLAYER_IN_SETUP;
}

public ConcurrentHashMap<KeepAliveResponseKey, Long> getPingMap() {
return pingMap;
}

public VersionUtils.Result getVersionResult() {
return versionResult;
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/de/cubeattack/neoprotect/core/NeoProtectPlugin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.cubeattack.neoprotect.core;

import de.cubeattack.neoprotect.core.model.KeepAliveResponseKey;

import java.util.logging.Logger;

public interface NeoProtectPlugin {
Expand All @@ -9,7 +11,15 @@ public interface NeoProtectPlugin {

void sendAdminMessage(Permission permission,String text, String clickAction, String clickMsg, String hoverAction, String hoverMsg);

KeepAliveResponseKey sendKeepAliveMessage(Object sender, long id);
Core getCore();
Logger getLogger();
String getVersion();
PluginType getPluginType();

enum PluginType {
SPIGOT,
VELOCITY,
BUNGEECORD,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import de.cubeattack.neoprotect.core.NeoProtectPlugin;
import de.cubeattack.neoprotect.core.model.Backend;
import de.cubeattack.neoprotect.core.model.Gameshield;
import org.json.JSONObject;

import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;

public class NeoProtectExecutor {
Expand Down Expand Up @@ -68,6 +70,16 @@ private void command(ExecutorBuilder executorBuilder) {
break;
}

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

case "analytics": {
analytics();
break;
}

case "setgameshield": {
if(args.length == 1){
gameshieldSelector(sender);
Expand Down Expand Up @@ -95,6 +107,11 @@ private void command(ExecutorBuilder executorBuilder) {
}
}

private void analytics(){
JSONObject analytics = instance.getCore().getRestAPI().getAnalytics();
analytics.keySet().forEach(k -> instance.sendMessage(sender, k + ": " + analytics.get(k).toString().replace("onlinePlayers", "online players")));
}

private void setup(Object sender){
instance.getCore().getPlayerInSetup().add(sender);
instance.sendMessage(sender, localization.get("command.setup") + localization.get("utils.click"),
Expand All @@ -111,6 +128,23 @@ private void iPanic(Object sender, String[] args){
}
}

private void debugPing(){
if(1==1){
instance.sendMessage(sender , "§cthis feature is coming soon");
return;
}

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

int id = new Random().nextInt(9) * 10000 + 1337;

instance.getCore().getPingMap().put(instance.sendKeepAliveMessage(sender, id), System.currentTimeMillis());
instance.sendMessage(sender , "Keep-alive packets send");
}

private void gameshieldSelector(Object sender){
instance.sendMessage(sender, localization.get("select.gameshield"));

Expand All @@ -125,7 +159,7 @@ private void gameshieldSelector(Object sender){

private void setGameshield(Object sender, String[] args){

if(!instance.getCore().getRestAPI().isGameshieldFound(args[0])) {
if(instance.getCore().getRestAPI().isGameshieldInvalid(args[1])) {
instance.sendMessage(sender, localization.get("invalid.gameshield", args[1]));
return;
}
Expand All @@ -151,7 +185,7 @@ private void backendSelector(Object sender) {

private void setBackend(Object sender, String[] args) {

if (!instance.getCore().getRestAPI().isBackendFound(args[0])) {
if (instance.getCore().getRestAPI().isBackendInvalid(args[1])) {
instance.sendMessage(sender, localization.get("invalid.backend", args[1]));
return;
}
Expand All @@ -162,12 +196,16 @@ private void setBackend(Object sender, String[] args) {
if(instance.getCore().getPlayerInSetup().remove(sender)){
instance.sendMessage(sender, localization.get("setup.finished"));
}

instance.getCore().getRestAPI().testCredentials();
}

private void showHelp(Object sender){
instance.sendMessage(sender, localization.get("available.commands"));
instance.sendMessage(sender, " - /np setup");
instance.sendMessage(sender, " - /np ipanic");
instance.sendMessage(sender, " - /np analytics");
instance.sendMessage(sender, " - /np debugping");
instance.sendMessage(sender, " - /np setgameshield");
instance.sendMessage(sender, " - /np setbackend");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.cubeattack.neoprotect.core.model;

import java.net.SocketAddress;

public class KeepAliveResponseKey {

private final SocketAddress address;
private final long id;

public KeepAliveResponseKey(SocketAddress address, long id) {
this.address = address;
this.id = id;
}

public SocketAddress getAddress() {
return address;
}

public long getId() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public void setProxyProtocol(boolean setting){
rest.request(RequestType.POST_GAMESHIELD_UPDATE, RequestBody.create(MediaType.parse("application/json"), new JsonBuilder().appendField("proxyProtocol", String.valueOf(setting)).build().toString()), Config.getGameShieldID());
}

public JSONObject getAnalytics() {
return rest.request(RequestType.GET_GAMESHIELD_LASTSTATS, null, Config.getGameShieldID()).getResponseBodyObject();
}

public void testCredentials(){

if(isAPIInvalid(Config.getAPIKey())){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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 net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
Expand Down Expand Up @@ -54,8 +55,18 @@ public void sendAdminMessage(Permission permission, String text, String clickAct
});
}

@Override
public KeepAliveResponseKey sendKeepAliveMessage(Object sender, long id) {
return null;
}

@Override
public String getVersion() {
return getDescription().getVersion();
}

@Override
public PluginType getPluginType() {
return PluginType.SPIGOT;
}
}
2 changes: 1 addition & 1 deletion src/main/java/de/cubeattack/neoprotect/spigot/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Startup(NeoProtectSpigot instance){
private void register(NeoProtectSpigot instance){
PluginManager pm = Bukkit.getPluginManager();
Objects.requireNonNull(instance.getCommand("neoprotect")).setExecutor(new NeoProtectCommand(instance));
Objects.requireNonNull(instance.getCommand("neoprotect")).setTabCompleter(new NeoProtectTabCompleter());
Objects.requireNonNull(instance.getCommand("neoprotect")).setTabCompleter(new NeoProtectTabCompleter(instance));

pm.registerEvents(new ChatListener(instance), instance);
pm.registerEvents(new LoginListener(instance), instance);
Expand Down
Loading

0 comments on commit e8518d4

Please sign in to comment.