From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: PanSzelescik Date: Thu, 7 Apr 2022 16:13:39 +0200 Subject: [PATCH] Add support for Proxy Protocol diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java index 72e17ac76e5f3ac1283bec2133f76bc197353dd6..d4ff14b9e6028259c27ea9cb2b4be587ebabfb41 100644 --- a/src/main/java/com/destroystokyo/paper/PaperConfig.java +++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java @@ -647,4 +647,9 @@ public class PaperConfig { private static void useDimensionTypeForCustomSpawners() { useDimensionTypeForCustomSpawners = getBoolean("settings.use-dimension-type-for-custom-spawners", false); } + + public static boolean useProxyProtocol; + private static void useProxyProtocol() { + useProxyProtocol = getBoolean("settings.proxy-protocol", false); + } } diff --git a/src/main/java/net/minecraft/server/network/ServerConnectionListener.java b/src/main/java/net/minecraft/server/network/ServerConnectionListener.java index 510a1fc4e315483809035b1e5c28b441f18a7c14..947f7afb1d61ece0b1a51b275b22f21ec2216dde 100644 --- a/src/main/java/net/minecraft/server/network/ServerConnectionListener.java +++ b/src/main/java/net/minecraft/server/network/ServerConnectionListener.java @@ -110,6 +110,12 @@ public class ServerConnectionListener { ServerConnectionListener.LOGGER.info("Paper: Using " + com.velocitypowered.natives.util.Natives.cipher.getLoadedVariant() + " cipher from Velocity."); // Paper end + // Paper start - indicate Proxy Protocol usage + if (com.destroystokyo.paper.PaperConfig.useProxyProtocol) { + ServerConnectionListener.LOGGER.info("Paper: Using Proxy Protocol"); + } + // Paper end + this.channels.add(((ServerBootstrap) ((ServerBootstrap) (new ServerBootstrap()).channel(oclass)).childHandler(new ChannelInitializer() { protected void initChannel(Channel channel) { try { @@ -123,6 +129,30 @@ public class ServerConnectionListener { int j = ServerConnectionListener.this.server.getRateLimitPacketsPerSecond(); Object object = j > 0 ? new RateKickingConnection(j) : new Connection(PacketFlow.SERVERBOUND); + // Paper start - Add support for Proxy Protocol + if (com.destroystokyo.paper.PaperConfig.useProxyProtocol) { + channel.pipeline().addAfter("timeout", "haproxy-decoder", new io.netty.handler.codec.haproxy.HAProxyMessageDecoder()); + channel.pipeline().addAfter("haproxy-decoder", "haproxy-handler", new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + if (msg instanceof io.netty.handler.codec.haproxy.HAProxyMessage message) { + if (message.command() == io.netty.handler.codec.haproxy.HAProxyCommand.PROXY) { + String realaddress = message.sourceAddress(); + int realport = message.sourcePort(); + + SocketAddress socketaddr = new java.net.InetSocketAddress(realaddress, realport); + + Connection connection = (Connection) channel.pipeline().get("packet_handler"); + connection.address = socketaddr; + } + } else { + super.channelRead(ctx, msg); + } + } + }); + } + // Paper end + // ServerConnectionListener.this.connections.add((Connection) object); // CraftBukkit - decompile error pending.add((Connection) object); // Paper channel.pipeline().addLast("packet_handler", (ChannelHandler) object);