Skip to content

Commit 50de9d6

Browse files
committed
Fix PROXY protocol support
1 parent 123c5cc commit 50de9d6

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

core/src/main/java/org/geysermc/geyser/network/netty/LocalSession.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
import io.netty.bootstrap.Bootstrap;
2929
import io.netty.buffer.ByteBufAllocator;
30-
import io.netty.channel.Channel;
30+
import io.netty.channel.ChannelHandlerContext;
31+
import io.netty.channel.ChannelInboundHandlerAdapter;
3132
import io.netty.channel.ChannelInitializer;
3233
import io.netty.channel.ChannelOption;
3334
import io.netty.channel.ChannelPipeline;
@@ -101,7 +102,7 @@ public void initChannel(@NonNull LocalChannelWithRemoteAddress channel) {
101102

102103
ChannelPipeline pipeline = channel.pipeline();
103104

104-
initializeHAProxySupport(channel);
105+
addHAProxySupport(pipeline);
105106

106107
pipeline.addLast("read-timeout", new ReadTimeoutHandler(getFlag(BuiltinFlags.READ_TIMEOUT, 30)));
107108
pipeline.addLast("write-timeout", new WriteTimeoutHandler(getFlag(BuiltinFlags.WRITE_TIMEOUT, 0)));
@@ -142,21 +143,33 @@ public MinecraftCodecHelper getCodecHelper() {
142143
return (MinecraftCodecHelper) this.codecHelper;
143144
}
144145

145-
private void initializeHAProxySupport(Channel channel) {
146+
// TODO duplicate code
147+
private void addHAProxySupport(ChannelPipeline pipeline) {
146148
InetSocketAddress clientAddress = getFlag(BuiltinFlags.CLIENT_PROXIED_ADDRESS);
147-
if (clientAddress == null) {
148-
return;
149+
if (clientAddress != null) {
150+
pipeline.addFirst("proxy-protocol-packet-sender", new ChannelInboundHandlerAdapter() {
151+
@Override
152+
public void channelActive(@NonNull ChannelHandlerContext ctx) throws Exception {
153+
HAProxyProxiedProtocol proxiedProtocol = clientAddress.getAddress() instanceof Inet4Address ? HAProxyProxiedProtocol.TCP4 : HAProxyProxiedProtocol.TCP6;
154+
InetSocketAddress remoteAddress;
155+
if (ctx.channel().remoteAddress() instanceof InetSocketAddress) {
156+
remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
157+
} else {
158+
remoteAddress = new InetSocketAddress(host, port);
159+
}
160+
ctx.channel().writeAndFlush(new HAProxyMessage(
161+
HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, proxiedProtocol,
162+
clientAddress.getAddress().getHostAddress(), remoteAddress.getAddress().getHostAddress(),
163+
clientAddress.getPort(), remoteAddress.getPort()
164+
));
165+
ctx.pipeline().remove(this);
166+
ctx.pipeline().remove("proxy-protocol-encoder");
167+
super.channelActive(ctx);
168+
}
169+
});
170+
pipeline.addFirst("proxy-protocol-encoder", HAProxyMessageEncoder.INSTANCE);
171+
}
149172
}
150-
151-
channel.pipeline().addLast("proxy-protocol-encoder", HAProxyMessageEncoder.INSTANCE);
152-
HAProxyProxiedProtocol proxiedProtocol = clientAddress.getAddress() instanceof Inet4Address ? HAProxyProxiedProtocol.TCP4 : HAProxyProxiedProtocol.TCP6;
153-
InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
154-
channel.writeAndFlush(new HAProxyMessage(
155-
HAProxyProtocolVersion.V2, HAProxyCommand.PROXY, proxiedProtocol,
156-
clientAddress.getAddress().getHostAddress(), remoteAddress.getAddress().getHostAddress(),
157-
clientAddress.getPort(), remoteAddress.getPort()
158-
)).addListener(future -> channel.pipeline().remove("proxy-protocol-encoder"));
159-
}
160173

161174
/**
162175
* Should only be called when direct ByteBufs should be preferred. At this moment, this should only be called on BungeeCord.

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protocol-common = "3.0.0.Beta5-20240916.181041-6"
1515
protocol-codec = "3.0.0.Beta5-20240916.181041-6"
1616
raknet = "1.0.0.CR3-20240416.144209-1"
1717
minecraftauth = "4.1.1"
18-
mcprotocollib = "1.21-20241008.134549-23"
18+
mcprotocollib = "1.21-20241010.155958-24"
1919
adventure = "4.14.0"
2020
adventure-platform = "4.3.0"
2121
junit = "5.9.2"

0 commit comments

Comments
 (0)