|
27 | 27 |
|
28 | 28 | import io.netty.bootstrap.Bootstrap; |
29 | 29 | import io.netty.buffer.ByteBufAllocator; |
30 | | -import io.netty.channel.Channel; |
| 30 | +import io.netty.channel.ChannelHandlerContext; |
| 31 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
31 | 32 | import io.netty.channel.ChannelInitializer; |
32 | 33 | import io.netty.channel.ChannelOption; |
33 | 34 | import io.netty.channel.ChannelPipeline; |
@@ -101,7 +102,7 @@ public void initChannel(@NonNull LocalChannelWithRemoteAddress channel) { |
101 | 102 |
|
102 | 103 | ChannelPipeline pipeline = channel.pipeline(); |
103 | 104 |
|
104 | | - initializeHAProxySupport(channel); |
| 105 | + addHAProxySupport(pipeline); |
105 | 106 |
|
106 | 107 | pipeline.addLast("read-timeout", new ReadTimeoutHandler(getFlag(BuiltinFlags.READ_TIMEOUT, 30))); |
107 | 108 | pipeline.addLast("write-timeout", new WriteTimeoutHandler(getFlag(BuiltinFlags.WRITE_TIMEOUT, 0))); |
@@ -142,21 +143,33 @@ public MinecraftCodecHelper getCodecHelper() { |
142 | 143 | return (MinecraftCodecHelper) this.codecHelper; |
143 | 144 | } |
144 | 145 |
|
145 | | - private void initializeHAProxySupport(Channel channel) { |
| 146 | + // TODO duplicate code |
| 147 | + private void addHAProxySupport(ChannelPipeline pipeline) { |
146 | 148 | 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 | + } |
149 | 172 | } |
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 | | - } |
160 | 173 |
|
161 | 174 | /** |
162 | 175 | * Should only be called when direct ByteBufs should be preferred. At this moment, this should only be called on BungeeCord. |
|
0 commit comments