From 147e08a30e63b5f9afbdf1cb827d7894ba9fba55 Mon Sep 17 00:00:00 2001 From: Vladimir Schafer Date: Sun, 2 Feb 2014 21:31:27 +0200 Subject: [PATCH] #2177 Adding support for bound host and port for the SOCKS5 command response. Changes are fully backward compatible. --- .../handler/codec/socks/SocksCmdResponse.java | 101 ++++++++++++++++-- .../codec/socks/SocksCmdResponseDecoder.java | 6 +- .../socks/SocksCmdResponseDecoderTest.java | 41 ++++++- .../codec/socks/SocksCmdResponseTest.java | 90 ++++++++++++++++ 4 files changed, 222 insertions(+), 16 deletions(-) diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponse.java b/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponse.java index 03218b6921..567ceb63ff 100644 --- a/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponse.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponse.java @@ -16,9 +16,13 @@ package io.netty.handler.codec.socks; import io.netty.buffer.ByteBuf; +import io.netty.util.CharsetUtil; +import io.netty.util.NetUtil; + +import java.net.IDN; /** - * An socks cmd response. + * A socks cmd response. * * @see SocksCmdRequest * @see SocksCmdResponseDecoder @@ -27,7 +31,11 @@ public final class SocksCmdResponse extends SocksResponse { private final SocksCmdStatus cmdStatus; private final SocksAddressType addressType; + private final String host; + private final int port; + // All arrays are initialized on construction time to 0/false/null remove array Initialization + private static final byte[] DOMAIN_ZEROED = {0x00}; private static final byte[] IPv4_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00}; private static final byte[] IPv6_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -35,6 +43,23 @@ public final class SocksCmdResponse extends SocksResponse { 0x00, 0x00, 0x00, 0x00}; public SocksCmdResponse(SocksCmdStatus cmdStatus, SocksAddressType addressType) { + this(cmdStatus, addressType, null, 0); + } + + /** + * Constructs new response and includes provided host and port as part of it. + * + * @param cmdStatus status of the response + * @param addressType type of host parameter + * @param host host (BND.ADDR field) is address that server used when connecting to the target host. + * When null a value of 4/8 0x00 octets will be used for IPv4/IPv6 and a single 0x00 byte will be + * used for domain addressType. Value is converted to ASCII using {@link IDN#toASCII(String)}. + * @param port port (BND.PORT field) that the server assigned to connect to the target host + * @throws NullPointerException in case cmdStatus or addressType are missing + * @throws IllegalArgumentException in case host or port cannot be validated + * @see IDN#toASCII(String) + */ + public SocksCmdResponse(SocksCmdStatus cmdStatus, SocksAddressType addressType, String host, int port) { super(SocksResponseType.CMD); if (cmdStatus == null) { throw new NullPointerException("cmdStatus"); @@ -42,8 +67,36 @@ public final class SocksCmdResponse extends SocksResponse { if (addressType == null) { throw new NullPointerException("addressType"); } + if (host != null) { + switch (addressType) { + case IPv4: + if (!NetUtil.isValidIpV4Address(host)) { + throw new IllegalArgumentException(host + " is not a valid IPv4 address"); + } + break; + case DOMAIN: + if (IDN.toASCII(host).length() > 255) { + throw new IllegalArgumentException(host + " IDN: " + + IDN.toASCII(host) + " exceeds 255 char limit"); + } + break; + case IPv6: + if (!NetUtil.isValidIpV6Address(host)) { + throw new IllegalArgumentException(host + " is not a valid IPv6 address"); + } + break; + case UNKNOWN: + break; + } + host = IDN.toASCII(host); + } + if (port < 0 && port >= 65535) { + throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536"); + } this.cmdStatus = cmdStatus; this.addressType = addressType; + this.host = host; + this.port = port; } /** @@ -64,6 +117,32 @@ public final class SocksCmdResponse extends SocksResponse { return addressType; } + /** + * Returns host that is used as a parameter in {@link io.netty.handler.codec.socks.SocksCmdType}. + * Host (BND.ADDR field in response) is address that server used when connecting to the target host. + * This is typically different from address which client uses to connect to the SOCKS server. + * + * @return host that is used as a parameter in {@link io.netty.handler.codec.socks.SocksCmdType} + * or null when there was no host specified during response construction + */ + public String host() { + if (host != null) { + return IDN.toUnicode(host); + } else { + return null; + } + } + + /** + * Returns port that is used as a parameter in {@link io.netty.handler.codec.socks.SocksCmdType}. + * Port (BND.PORT field in response) is port that the server assigned to connect to the target host. + * + * @return port that is used as a parameter in {@link io.netty.handler.codec.socks.SocksCmdType} + */ + public int port() { + return port; + } + @Override public void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); @@ -72,19 +151,25 @@ public final class SocksCmdResponse extends SocksResponse { byteBuf.writeByte(addressType.byteValue()); switch (addressType) { case IPv4: { - byteBuf.writeBytes(IPv4_HOSTNAME_ZEROED); - byteBuf.writeShort(0); + byte[] hostContent = host == null ? + IPv4_HOSTNAME_ZEROED : NetUtil.createByteArrayFromIpAddressString(host); + byteBuf.writeBytes(hostContent); + byteBuf.writeShort(port); break; } case DOMAIN: { - byteBuf.writeByte(1); // domain length - byteBuf.writeByte(0); // domain value - byteBuf.writeShort(0); // port value + byte[] hostContent = host == null ? + DOMAIN_ZEROED : host.getBytes(CharsetUtil.US_ASCII); + byteBuf.writeByte(hostContent.length); // domain length + byteBuf.writeBytes(hostContent); // domain value + byteBuf.writeShort(port); // port value break; } case IPv6: { - byteBuf.writeBytes(IPv6_HOSTNAME_ZEROED); - byteBuf.writeShort(0); + byte[] hostContent = host == null + ? IPv6_HOSTNAME_ZEROED : NetUtil.createByteArrayFromIpAddressString(host); + byteBuf.writeBytes(hostContent); + byteBuf.writeShort(port); break; } } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponseDecoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponseDecoder.java index e313128be4..d93674b752 100644 --- a/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponseDecoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socks/SocksCmdResponseDecoder.java @@ -67,20 +67,20 @@ public class SocksCmdResponseDecoder extends ReplayingDecoder