Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: include proxy protocol in server conn dimms #1103

Merged
merged 2 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -45,6 +47,10 @@ public final class HAProxyMessageChannelHandler extends ChannelInboundHandlerAda

@VisibleForTesting
static final Attrs.Key<Integer> HAPM_DEST_PORT = Attrs.newKey("hapm_port");
@VisibleForTesting
static final Attrs.Key<String> HAPM_DEST_IP_VERSION = Attrs.newKey("hapm_dst_ipproto");
@VisibleForTesting
static final Attrs.Key<String> HAPM_SRC_IP_VERSION = Attrs.newKey("hapm_src_ipproto");

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Expand Down Expand Up @@ -72,6 +78,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
Expand Down Expand Up @@ -99,8 +112,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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}