From 84b272a6357de75b00825ccad52532d1443bbcc8 Mon Sep 17 00:00:00 2001 From: ytendx <81882687+ytendx@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:29:23 +0200 Subject: [PATCH 1/6] Allowing newer versions to work as well --- .../ytendx/haproxy/tinyprotocol/Reflection.java | 14 +++++++++----- .../ytendx/haproxy/tinyprotocol/TinyProtocol.java | 15 +++++++++++---- src/main/resources/plugin.yml | 1 + 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java b/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java index 2502369..cd097fe 100644 --- a/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java +++ b/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java @@ -73,9 +73,9 @@ public interface FieldAccessor { } // Deduce the net.minecraft.server.v* package - private static String OBC_PREFIX = Bukkit.getServer().getClass().getPackage().getName(); - private static String VERSION = OBC_PREFIX.replace("org.bukkit.craftbukkit", "").replace(".", ""); - private static String NMS_PREFIX = OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server"); + public static String OBC_PREFIX = Bukkit.getServer().getClass().getPackage().getName(); + public static String VERSION = OBC_PREFIX.replace("org.bukkit.craftbukkit", "").replace(".", ""); + public static String NMS_PREFIX = OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server"); // Variable replacement private static Pattern MATCH_VARIABLE = Pattern.compile("\\{([^\\}]+)\\}"); @@ -366,13 +366,17 @@ private static Class getCanonicalClass(String canonicalName) { } } + public static boolean isNewerPackage(){ + return VERSION.contains("17") || VERSION.contains("18") || VERSION.contains("19") || VERSION.contains("20"); + } + /** * Expand variables such as "{nms}" and "{obc}" to their corresponding packages. * * @param name - the full name of the class. * @return The expanded string. */ - private static String expandVariables(String name) { + public static String expandVariables(String name) { StringBuffer output = new StringBuffer(); Matcher matcher = MATCH_VARIABLE.matcher(name); @@ -382,7 +386,7 @@ private static String expandVariables(String name) { // Expand all detected variables if ("nms".equalsIgnoreCase(variable)) - replacement = NMS_PREFIX; + replacement = isNewerPackage() ? "net.minecraft.server" : NMS_PREFIX; else if ("obc".equalsIgnoreCase(variable)) replacement = OBC_PREFIX; else if ("version".equalsIgnoreCase(variable)) diff --git a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java index 7a4384a..c453a96 100644 --- a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java +++ b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java @@ -4,6 +4,7 @@ import java.net.InetSocketAddress; import java.net.SocketAddress; +import java.sql.Ref; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; @@ -26,12 +27,18 @@ public class TinyProtocol { // Looking up ServerConnection private static final Class minecraftServerClass = Reflection.getUntypedClass("{nms}.MinecraftServer"); - private static final Class serverConnectionClass = Reflection.getUntypedClass("{nms}.ServerConnection"); + private static final Class serverConnectionClass = Reflection.getUntypedClass("{nms}" + (Reflection.isNewerPackage() ? ".network" : "") + ".ServerConnection"); private static final Reflection.FieldAccessor getMinecraftServer = Reflection.getField("{obc}.CraftServer", minecraftServerClass, 0); private static final Reflection.FieldAccessor getServerConnection = Reflection.getField(minecraftServerClass, serverConnectionClass, 0); - private static final Reflection.MethodInvoker getNetworkMarkers = Reflection.getTypedMethod(serverConnectionClass, null, List.class, serverConnectionClass); + private static final Reflection.MethodInvoker getNetworkMarkers; + private static final Reflection.FieldAccessor networkManagersFieldAccessor; - private static final Class networkManager = Reflection.getUntypedClass("{nms}.NetworkManager"); + static { + getNetworkMarkers = !Reflection.isNewerPackage() ? Reflection.getTypedMethod(serverConnectionClass, null, List.class, serverConnectionClass) : null; + networkManagersFieldAccessor = Reflection.isNewerPackage() ? Reflection.getField(serverConnectionClass, List.class, 0) : null; + } + + private static final Class networkManager = Reflection.getUntypedClass(Reflection.isNewerPackage() ? "net.minecraft.network.NetworkManager" : "{nms}.NetworkManager"); private static final Reflection.FieldAccessor socketAddressFieldAccessor = Reflection.getField(networkManager, SocketAddress.class, 0); // List of network markers private List networkManagers; @@ -128,7 +135,7 @@ private void registerChannelHandler() { boolean looking = true; // We need to synchronize against this list - networkManagers = (List) getNetworkMarkers.invoke(null, serverConnection); + networkManagers = Reflection.isNewerPackage() ? networkManagersFieldAccessor.get(serverConnection) : (List) getNetworkMarkers.invoke(null, serverConnection); createServerChannelHandler(); // Find the correct list, or implicitly throw an exception diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 3073742..2bc7bff 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,7 @@ name: HAProxyImplementor author: ytendx version: 1.1.1 +api-version: 1.13 main: de.ytendx.haproxy.HAProxySpigotImplementor website: https://neoprotect.net/ description: Allows the usage of ip forwarding via the HAProxy Protocol v2/v1 From 326a096fc6b6e2af4653aaeb73427e210a67cf6e Mon Sep 17 00:00:00 2001 From: ytendx <81882687+ytendx@users.noreply.github.com> Date: Sat, 29 Oct 2022 18:38:50 +0200 Subject: [PATCH 2/6] Forgot to increase version --- src/main/resources/plugin.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2bc7bff..9439825 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: HAProxyImplementor author: ytendx -version: 1.1.1 +version: 1.1.2 api-version: 1.13 main: de.ytendx.haproxy.HAProxySpigotImplementor website: https://neoprotect.net/ From a0149451f09c2429a11d1c8c588762c246e02a61 Mon Sep 17 00:00:00 2001 From: ytendx <81882687+ytendx@users.noreply.github.com> Date: Sun, 4 Dec 2022 19:30:01 +0100 Subject: [PATCH 3/6] lol --- .../haproxy/tinyprotocol/Reflection.java | 1 + .../haproxy/tinyprotocol/TinyProtocol.java | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java b/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java index cd097fe..bf8c556 100644 --- a/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java +++ b/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java @@ -367,6 +367,7 @@ private static Class getCanonicalClass(String canonicalName) { } public static boolean isNewerPackage(){ + System.out.println(VERSION + " | " + Reflection.NMS_PREFIX + " | " + OBC_PREFIX); return VERSION.contains("17") || VERSION.contains("18") || VERSION.contains("19") || VERSION.contains("20"); } diff --git a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java index c453a96..29a51c1 100644 --- a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java +++ b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java @@ -1,13 +1,17 @@ package de.ytendx.haproxy.tinyprotocol; +import io.netty.buffer.ByteBuf; import io.netty.channel.*; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.sql.Ref; +import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; + +import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.haproxy.HAProxyMessage; import io.netty.handler.codec.haproxy.HAProxyMessageDecoder; import org.bukkit.Bukkit; @@ -92,13 +96,27 @@ private void createServerChannelHandler() { @Override protected void initChannel(Channel channel) throws Exception { + System.out.println("END INIT"); try { + if (Reflection.isNewerPackage()){ + System.out.println("NEWER"); + synchronized (networkManagers) { + // Adding the decoder to the pipeline + System.out.println("SYMCED"); + channel.pipeline().addFirst("lul", new Debugger()); + channel.pipeline().addFirst("haproxy-decoder", new HAProxyMessageDecoder()); + // Adding the proxy message handler to the pipeline too + channel.pipeline().addAfter("haproxy-decoder", "haproxy-handler", HAPROXY_MESSAGE_HANDLER); + } + return; + } + // Adding the decoder to the pipeline channel.pipeline().addAfter("timeout", "haproxy-decoder", new HAProxyMessageDecoder()); // Adding the proxy message handler to the pipeline too channel.pipeline().addAfter("haproxy-decoder", "haproxy-handler", HAPROXY_MESSAGE_HANDLER); } catch (Exception e) { - plugin.getLogger().log(Level.SEVERE, "Cannot inject incomming channel " + channel, e); + plugin.getLogger().log(Level.SEVERE, "Cannot inject incoming channel " + channel, e); } } @@ -109,6 +127,7 @@ protected void initChannel(Channel channel) throws Exception { @Override protected void initChannel(Channel channel) throws Exception { + System.out.println("BEGIN INIT"); channel.pipeline().addLast(endInitProtocol); } @@ -120,6 +139,8 @@ protected void initChannel(Channel channel) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Channel channel = (Channel) msg; + System.out.println("HANDLE"); + // Prepare to initialize ths channel channel.pipeline().addFirst(beginInitProtocol); ctx.fireChannelRead(msg); @@ -169,6 +190,14 @@ protected String getHandlerName() { return "haproxy-implementor-tiny-" + plugin.getName() + "-" + ID.incrementAndGet(); } + public static class Debugger extends ByteToMessageDecoder { + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + System.out.println("PACKET!!!!"); + out.add(in); + } + } + private final HAProxyMessageHandler HAPROXY_MESSAGE_HANDLER = new HAProxyMessageHandler(); @ChannelHandler.Sharable From 66d6c31a2e641bc01c8f1c71e32f335a1172a26a Mon Sep 17 00:00:00 2001 From: ytendx <81882687+ytendx@users.noreply.github.com> Date: Mon, 5 Dec 2022 18:51:44 +0100 Subject: [PATCH 4/6] Fixed everything --- .../haproxy/tinyprotocol/Reflection.java | 9 +++-- .../haproxy/tinyprotocol/TinyProtocol.java | 36 ++----------------- src/main/resources/plugin.yml | 2 +- 3 files changed, 10 insertions(+), 37 deletions(-) diff --git a/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java b/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java index bf8c556..3920ff2 100644 --- a/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java +++ b/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java @@ -6,6 +6,8 @@ import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; + +import de.ytendx.haproxy.HAProxySpigotImplementor; import org.bukkit.Bukkit; /** @@ -75,7 +77,10 @@ public interface FieldAccessor { // Deduce the net.minecraft.server.v* package public static String OBC_PREFIX = Bukkit.getServer().getClass().getPackage().getName(); public static String VERSION = OBC_PREFIX.replace("org.bukkit.craftbukkit", "").replace(".", ""); - public static String NMS_PREFIX = OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server"); + public static String NMS_PREFIX = (Reflection.isNewerPackage() ? + OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server").replace(VERSION, "") : + OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server")) + ; // Variable replacement private static Pattern MATCH_VARIABLE = Pattern.compile("\\{([^\\}]+)\\}"); @@ -367,7 +372,7 @@ private static Class getCanonicalClass(String canonicalName) { } public static boolean isNewerPackage(){ - System.out.println(VERSION + " | " + Reflection.NMS_PREFIX + " | " + OBC_PREFIX); + HAProxySpigotImplementor.getPlugin(HAProxySpigotImplementor.class).getLogger().info(VERSION + " | " + Reflection.NMS_PREFIX + " | " + OBC_PREFIX); return VERSION.contains("17") || VERSION.contains("18") || VERSION.contains("19") || VERSION.contains("20"); } diff --git a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java index 29a51c1..644dc4f 100644 --- a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java +++ b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java @@ -1,5 +1,6 @@ package de.ytendx.haproxy.tinyprotocol; +import de.ytendx.haproxy.HAProxySpigotImplementor; import io.netty.buffer.ByteBuf; import io.netty.channel.*; @@ -27,7 +28,6 @@ * @author Kristian // Love ya <3 ~ytendx */ public class TinyProtocol { - private static final AtomicInteger ID = new AtomicInteger(0); // Looking up ServerConnection private static final Class minecraftServerClass = Reflection.getUntypedClass("{nms}.MinecraftServer"); @@ -53,10 +53,6 @@ public class TinyProtocol { private ChannelInitializer beginInitProtocol; private ChannelInitializer endInitProtocol; - // Current handler name - private String handlerName; - - protected volatile boolean closed; protected Plugin plugin; /** @@ -69,9 +65,7 @@ public class TinyProtocol { public TinyProtocol(final Plugin plugin) { this.plugin = plugin; - // Compute handler name - this.handlerName = getHandlerName(); - + plugin.getLogger().info(Reflection.isNewerPackage() + " | " + Reflection.NMS_PREFIX); try { plugin.getLogger().info("Proceeding with the server channel injection..."); @@ -96,14 +90,10 @@ private void createServerChannelHandler() { @Override protected void initChannel(Channel channel) throws Exception { - System.out.println("END INIT"); try { if (Reflection.isNewerPackage()){ - System.out.println("NEWER"); synchronized (networkManagers) { // Adding the decoder to the pipeline - System.out.println("SYMCED"); - channel.pipeline().addFirst("lul", new Debugger()); channel.pipeline().addFirst("haproxy-decoder", new HAProxyMessageDecoder()); // Adding the proxy message handler to the pipeline too channel.pipeline().addAfter("haproxy-decoder", "haproxy-handler", HAPROXY_MESSAGE_HANDLER); @@ -127,7 +117,6 @@ protected void initChannel(Channel channel) throws Exception { @Override protected void initChannel(Channel channel) throws Exception { - System.out.println("BEGIN INIT"); channel.pipeline().addLast(endInitProtocol); } @@ -139,8 +128,6 @@ protected void initChannel(Channel channel) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Channel channel = (Channel) msg; - System.out.println("HANDLE"); - // Prepare to initialize ths channel channel.pipeline().addFirst(beginInitProtocol); ctx.fireChannelRead(msg); @@ -179,25 +166,6 @@ private void registerChannelHandler() { } } - /** - * Retrieve the name of the channel injector, default implementation is "tiny-" + plugin name + "-" + a unique ID. - *

- * Note that this method will only be invoked once. It is no longer necessary to override this to support multiple instances. - * - * @return A unique channel handler name. - */ - protected String getHandlerName() { - return "haproxy-implementor-tiny-" + plugin.getName() + "-" + ID.incrementAndGet(); - } - - public static class Debugger extends ByteToMessageDecoder { - @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - System.out.println("PACKET!!!!"); - out.add(in); - } - } - private final HAProxyMessageHandler HAPROXY_MESSAGE_HANDLER = new HAProxyMessageHandler(); @ChannelHandler.Sharable diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9439825..1dc10e5 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: HAProxyImplementor author: ytendx -version: 1.1.2 +version: 1.1.3 api-version: 1.13 main: de.ytendx.haproxy.HAProxySpigotImplementor website: https://neoprotect.net/ From ad3831b312edc943cd9ae2b34dae8e0f0b7b9d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thore=20M=C3=BCller?= <75487485+LUPLUV@users.noreply.github.com> Date: Mon, 11 Mar 2024 17:42:12 +0100 Subject: [PATCH 5/6] fix paper version 1.16 --- src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java | 3 +-- .../java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java b/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java index 3920ff2..1b8e4a1 100644 --- a/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java +++ b/src/main/java/de/ytendx/haproxy/tinyprotocol/Reflection.java @@ -79,8 +79,7 @@ public interface FieldAccessor { public static String VERSION = OBC_PREFIX.replace("org.bukkit.craftbukkit", "").replace(".", ""); public static String NMS_PREFIX = (Reflection.isNewerPackage() ? OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server").replace(VERSION, "") : - OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server")) - ; + OBC_PREFIX.replace("org.bukkit.craftbukkit", "net.minecraft.server")); // Variable replacement private static Pattern MATCH_VARIABLE = Pattern.compile("\\{([^\\}]+)\\}"); diff --git a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java index 644dc4f..4f06f50 100644 --- a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java +++ b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java @@ -7,6 +7,7 @@ import java.net.InetSocketAddress; import java.net.SocketAddress; import java.sql.Ref; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -34,11 +35,9 @@ public class TinyProtocol { private static final Class serverConnectionClass = Reflection.getUntypedClass("{nms}" + (Reflection.isNewerPackage() ? ".network" : "") + ".ServerConnection"); private static final Reflection.FieldAccessor getMinecraftServer = Reflection.getField("{obc}.CraftServer", minecraftServerClass, 0); private static final Reflection.FieldAccessor getServerConnection = Reflection.getField(minecraftServerClass, serverConnectionClass, 0); - private static final Reflection.MethodInvoker getNetworkMarkers; private static final Reflection.FieldAccessor networkManagersFieldAccessor; static { - getNetworkMarkers = !Reflection.isNewerPackage() ? Reflection.getTypedMethod(serverConnectionClass, null, List.class, serverConnectionClass) : null; networkManagersFieldAccessor = Reflection.isNewerPackage() ? Reflection.getField(serverConnectionClass, List.class, 0) : null; } @@ -143,7 +142,7 @@ private void registerChannelHandler() { boolean looking = true; // We need to synchronize against this list - networkManagers = Reflection.isNewerPackage() ? networkManagersFieldAccessor.get(serverConnection) : (List) getNetworkMarkers.invoke(null, serverConnection); + networkManagers = Reflection.isNewerPackage() ? networkManagersFieldAccessor.get(serverConnection) : new ArrayList<>(); createServerChannelHandler(); // Find the correct list, or implicitly throw an exception From 6761f9a2ec1ba2b56d0932a2993757c5015b301e Mon Sep 17 00:00:00 2001 From: Kalibrier <157916660+Kalibrier@users.noreply.github.com> Date: Thu, 5 Sep 2024 09:23:28 +0200 Subject: [PATCH 6/6] Update TinyProtocol.java Removed useless logging at Line 67 --- .../java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java index 4f06f50..58ba2d3 100644 --- a/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java +++ b/src/main/java/de/ytendx/haproxy/tinyprotocol/TinyProtocol.java @@ -63,8 +63,8 @@ public class TinyProtocol { */ public TinyProtocol(final Plugin plugin) { this.plugin = plugin; - - plugin.getLogger().info(Reflection.isNewerPackage() + " | " + Reflection.NMS_PREFIX); + + //Useless logging -> plugin.getLogger().info(Reflection.isNewerPackage() + " | " + Reflection.NMS_PREFIX); try { plugin.getLogger().info("Proceeding with the server channel injection...");