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

Commit

Permalink
Changes -> '/neoprotect debugPing' now works for BungeeCord
Browse files Browse the repository at this point in the history
  • Loading branch information
EinfacheSache committed Jul 11, 2023
1 parent 5d0a6d2 commit 3e6055b
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 93 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.cubeattack</groupId>
<artifactId>neoprotect</artifactId>
<version>1.1.5-Beta</version>
<version>1.1.6-Beta</version>
<packaging>jar</packaging>

<name>NeoProtect</name>
Expand Down
31 changes: 19 additions & 12 deletions src/main/java/de/cubeattack/neoprotect/bungee/NeoProtectBungee.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,41 @@ public Core getCore() {
}

@Override
public void sendMessage(Object sender, String text) {
sendMessage(sender, text, null, null, null, null);
public void sendMessage(Object receiver, String text) {
sendMessage(receiver, text, null, null, null, null);
}

@Override
public void sendMessage(Object sender, String text, String clickAction, String clickMsg, String hoverAction, String hoverMsg) {
public void sendMessage(Object receiver, String text, String clickAction, String clickMsg, String hoverAction, String hoverMsg) {
TextComponent msg = new TextComponent(core.getPrefix() + text);

if(clickAction != null) msg.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(clickAction), clickMsg));
if(hoverAction != null) msg.setHoverEvent(new HoverEvent(HoverEvent.Action.valueOf(hoverAction), Collections.singletonList(new Text(hoverMsg))));
if(sender instanceof CommandSender) ((CommandSender) sender).sendMessage(msg);
if(receiver instanceof CommandSender) ((CommandSender) receiver).sendMessage(msg);
}

@Override
public void sendAdminMessage(Permission permission, String text, String clickAction, String clickMsg, String hoverAction, String hoverMsg) {
getProxy().getPlayers().forEach(pp -> {
if(pp.hasPermission("neoprotect.admin") || pp.hasPermission(permission.value))
sendMessage(pp, text, clickAction, clickMsg, hoverAction, hoverMsg);
getProxy().getPlayers().forEach(receiver -> {
if(receiver.hasPermission("neoprotect.admin") || receiver.hasPermission(permission.value))
sendMessage(receiver, text, clickAction, clickMsg, hoverAction, hoverMsg);
});
}

@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);
public void sendKeepAliveMessage(Object receiver, long id) {
if(receiver instanceof ProxiedPlayer){
((ProxiedPlayer)receiver).unsafe().sendPacket(new KeepAlive(id));
getCore().getPingMap().put(new KeepAliveResponseKey(((ProxiedPlayer)receiver).getSocketAddress(), id), System.currentTimeMillis());
}
return null;
}

@Override
public long sendKeepAliveMessage(long id) {
for (ProxiedPlayer player : this.getProxy().getPlayers()) {
sendKeepAliveMessage(player, id);
}
return id;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import de.cubeattack.api.utils.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 io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.epoll.EpollTcpInfo;
import io.netty.handler.codec.haproxy.HAProxyMessage;
import io.netty.handler.codec.haproxy.HAProxyMessageDecoder;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.netty.HandlerBoss;
import net.md_5.bungee.netty.PipelineUtils;
Expand All @@ -20,11 +26,11 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;

public class ProxyProtocol {

private final Reflection.FieldAccessor<ChannelWrapper> channelWrapperAccessor = Reflection.getField(HandlerBoss.class, "channel", ChannelWrapper.class);
private final ChannelInitializer<Channel> bungeeChannelInitializer = PipelineUtils.SERVER_CHILD;
private final Reflection.MethodInvoker initChannelMethod = Reflection.getMethod(bungeeChannelInitializer.getClass(), "initChannel", Channel.class);
Expand Down Expand Up @@ -77,19 +83,51 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
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);

if (!(msg instanceof PacketWrapper)) {
return;
}
if (!(((PacketWrapper) msg).packet instanceof KeepAlive)) {
return;
}

KeepAlive keepAlive = (KeepAlive) ((PacketWrapper) msg).packet;
ConcurrentHashMap<KeepAliveResponseKey, Long> pingMap = instance.getCore().getPingMap();

for (KeepAliveResponseKey keepAliveResponseKey : pingMap.keySet()) {

if (!keepAliveResponseKey.getAddress().equals(inetAddress.get()) || !(keepAliveResponseKey.getId() == keepAlive.getRandomId())) {
continue;
}

for (ProxiedPlayer player : BungeeCord.getInstance().getPlayers()) {

if (!(player).getPendingConnection().getSocketAddress().equals(inetAddress.get())) {
continue;
}

EpollTcpInfo tcpInfo = ((EpollSocketChannel) channel).tcpInfo();
EpollTcpInfo tcpInfoBackend = ((EpollSocketChannel) ((UserConnection) player).getServer().getCh().getHandle()).tcpInfo();

long ping = System.currentTimeMillis() - pingMap.get(keepAliveResponseKey);
long neoRTT = 0;
long backendRTT = 0;

if (tcpInfo != null) {
neoRTT = tcpInfo.rtt() / 1000;
}
if (tcpInfoBackend != null) {
backendRTT = tcpInfoBackend.rtt() / 1000;
}

instance.getCore().getDebugPingResponses().put(player.getName(), new DebugPingResponse(ping, neoRTT, backendRTT));

}
instance.getCore().getPingMap().remove(keepAliveResponseKey);
}

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

} catch (Exception ex) {
instance.getLogger().log(Level.SEVERE, "Cannot inject incoming channel " + channel, ex);
}
Expand Down
21 changes: 21 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,15 +4,19 @@
import de.cubeattack.api.logger.LogManager;
import de.cubeattack.api.utils.FileUtils;
import de.cubeattack.api.utils.VersionUtils;
import de.cubeattack.neoprotect.core.model.DebugPingResponse;
import de.cubeattack.neoprotect.core.model.KeepAliveResponseKey;
import de.cubeattack.neoprotect.core.request.RestAPIRequests;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SuppressWarnings("unused")
public class Core {
Expand All @@ -26,12 +30,17 @@ public class Core {

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 ExecutorService executorService;
private VersionUtils.Result versionResult;

public Core(NeoProtectPlugin plugin) {
LogManager.getLogger().setLogger(plugin.getLogger());

this.plugin = plugin;
this.executorService = Executors.newSingleThreadExecutor();
this.versionResult = VersionUtils.checkVersion("NeoProtect", "NeoPlugin", "v" + plugin.getVersion()).message();

FileUtils config = new FileUtils(Core.class.getResourceAsStream("/config.yml"), "plugins/NeoProtect", "config.yml", false);
Expand Down Expand Up @@ -66,6 +75,10 @@ public NeoProtectPlugin getPlugin() {
return plugin;
}

public ExecutorService getExecutorService() {
return executorService;
}

public UUID getMaintainerUUID() {
return maintainerUUID;
}
Expand All @@ -90,6 +103,14 @@ public ConcurrentHashMap<KeepAliveResponseKey, Long> getPingMap() {
return pingMap;
}

public ConcurrentHashMap<Long, Timestamp> getTimestampsMap() {
return timestampsMap;
}

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

public VersionUtils.Result getVersionResult() {
return versionResult;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package de.cubeattack.neoprotect.core;

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

import java.util.logging.Logger;

public interface NeoProtectPlugin {

void sendMessage(Object sender, String text);
void sendMessage(Object sender, String text, String clickAction, String clickMsg, String hoverAction, String hoverMsg);

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

KeepAliveResponseKey sendKeepAliveMessage(Object sender, long id);
long sendKeepAliveMessage(long id);
void sendKeepAliveMessage(Object receiver, long id);

Core getCore();
Logger getLogger();
String getVersion();
Expand Down
Loading

0 comments on commit 3e6055b

Please sign in to comment.