From 35e019e173091fb7f06a65b97e166391d7984a18 Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Wed, 1 Sep 2021 11:43:02 -0700 Subject: [PATCH 1/2] core: include proxy protocol in server conn dimms --- .../HAProxyMessageChannelHandler.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/zuul-core/src/main/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandler.java b/zuul-core/src/main/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandler.java index 5739c4a257..528c16b26f 100644 --- a/zuul-core/src/main/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandler.java +++ b/zuul-core/src/main/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandler.java @@ -28,6 +28,8 @@ import io.netty.handler.codec.haproxy.HAProxyMessage; import io.netty.handler.codec.haproxy.HAProxyProtocolVersion; import io.netty.util.AttributeKey; +import java.net.Inet4Address; +import java.net.Inet6Address; import java.net.InetSocketAddress; import java.net.SocketAddress; @@ -45,6 +47,8 @@ public final class HAProxyMessageChannelHandler extends ChannelInboundHandlerAda @VisibleForTesting static final Attrs.Key HAPM_DEST_PORT = Attrs.newKey("hapm_port"); + static final Attrs.Key HAPM_DEST_IP_VERSION = Attrs.newKey("hapm_dst_ipproto"); + static final Attrs.Key HAPM_SRC_IP_VERSION = Attrs.newKey("hapm_src_ipproto"); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { @@ -72,6 +76,13 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception // setting PPv2 explicitly because SourceAddressChannelHandler.ATTR_LOCAL_ADDR could be PPv2 or not channel.attr(SourceAddressChannelHandler.ATTR_PROXY_PROTOCOL_DESTINATION_ADDRESS).set(inetAddr); Attrs attrs = ctx.channel().attr(Server.CONN_DIMENSIONS).get(); + if (inetAddr.getAddress() instanceof Inet4Address) { + HAPM_DEST_IP_VERSION.put(attrs, "v4"); + } else if (inetAddr.getAddress() instanceof Inet6Address) { + HAPM_DEST_IP_VERSION.put(attrs, "v6"); + } else { + HAPM_DEST_IP_VERSION.put(attrs, "unknown"); + } HAPM_DEST_PORT.put(attrs, hapm.destinationPort()); break out; case UNIX_STREAM: // TODO: implement @@ -99,8 +110,17 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception throw new IllegalArgumentException("unknown proxy protocl" + sourceAddress); case TCP4: case TCP6: - addr = new InetSocketAddress( + InetSocketAddress inetAddr; + addr = inetAddr = new InetSocketAddress( InetAddresses.forString(sourceAddress), hapm.sourcePort()); + Attrs attrs = ctx.channel().attr(Server.CONN_DIMENSIONS).get(); + if (inetAddr.getAddress() instanceof Inet4Address) { + HAPM_SRC_IP_VERSION.put(attrs, "v4"); + } else if (inetAddr.getAddress() instanceof Inet6Address) { + HAPM_SRC_IP_VERSION.put(attrs, "v6"); + } else { + HAPM_SRC_IP_VERSION.put(attrs, "unknown"); + } break out; case UNIX_STREAM: // TODO: implement case UDP4: From e2bbe936f1003d6a9a7886c3ed935d5048b41347 Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Wed, 1 Sep 2021 11:52:47 -0700 Subject: [PATCH 2/2] add tests --- .../common/proxyprotocol/HAProxyMessageChannelHandler.java | 2 ++ .../proxyprotocol/HAProxyMessageChannelHandlerTest.java | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/zuul-core/src/main/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandler.java b/zuul-core/src/main/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandler.java index 528c16b26f..088b5ae944 100644 --- a/zuul-core/src/main/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandler.java +++ b/zuul-core/src/main/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandler.java @@ -47,7 +47,9 @@ public final class HAProxyMessageChannelHandler extends ChannelInboundHandlerAda @VisibleForTesting static final Attrs.Key HAPM_DEST_PORT = Attrs.newKey("hapm_port"); + @VisibleForTesting static final Attrs.Key HAPM_DEST_IP_VERSION = Attrs.newKey("hapm_dst_ipproto"); + @VisibleForTesting static final Attrs.Key HAPM_SRC_IP_VERSION = Attrs.newKey("hapm_src_ipproto"); @Override diff --git a/zuul-core/src/test/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandlerTest.java b/zuul-core/src/test/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandlerTest.java index ee2409f715..0e879fedcb 100644 --- a/zuul-core/src/test/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandlerTest.java +++ b/zuul-core/src/test/java/com/netflix/netty/common/proxyprotocol/HAProxyMessageChannelHandlerTest.java @@ -66,5 +66,9 @@ public void setClientDestPortForHAPM() { Attrs attrs = channel.attr(Server.CONN_DIMENSIONS).get(); Integer port = HAProxyMessageChannelHandler.HAPM_DEST_PORT.get(attrs); assertEquals(443, port.intValue()); + String sourceIpVersion = HAProxyMessageChannelHandler.HAPM_SRC_IP_VERSION.get(attrs); + assertEquals("v4", sourceIpVersion); + String destIpVersion = HAProxyMessageChannelHandler.HAPM_DEST_IP_VERSION.get(attrs); + assertEquals("v4", destIpVersion); } } \ No newline at end of file