From fc1429c00c6a365e37363091a3ee14291bb8f010 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 14 Aug 2014 16:30:36 -0700 Subject: [PATCH] Overall clean-up on socksx package - SocksV[45] -> Socks[45] - Make encodeAsByteBuf package private with some hassle - Split SocksMessageEncoder into Socks4MessageEncoder and Socks5MessageEncoder, and remove the original - Remove lazy singleton instantiation; we don't need it. - Remove the deprecated methods - Fix Javadoc errors --- .../handler/codec/socks/package-info.java | 1 - .../handler/codec/socksx/SocksMessage.java | 14 +---- .../codec/socksx/SocksProtocolVersion.java | 8 --- .../handler/codec/socksx/SocksRequest.java | 14 ++--- .../handler/codec/socksx/SocksResponse.java | 7 +++ .../handler/codec/socksx/package-info.java | 2 +- ...4CmdRequest.java => Socks4CmdRequest.java} | 34 ++++++------ ...oder.java => Socks4CmdRequestDecoder.java} | 24 ++++----- ...mdResponse.java => Socks4CmdResponse.java} | 30 +++++------ ...der.java => Socks4CmdResponseDecoder.java} | 22 ++++---- ...sV4CmdStatus.java => Socks4CmdStatus.java} | 8 +-- ...SocksV4CmdType.java => Socks4CmdType.java} | 8 +-- ...ommonUtils.java => Socks4CommonUtils.java} | 5 +- .../codec/socksx/v4/Socks4MessageEncoder.java | 52 +++++++++++++++++++ ...SocksV4Request.java => Socks4Request.java} | 20 ++++--- ...cksV4Response.java => Socks4Response.java} | 21 +++++--- ...Request.java => UnknownSocks4Request.java} | 19 +++---- ...sponse.java => UnknownSocks4Response.java} | 19 +++---- .../handler/codec/socksx/v4/package-info.java | 2 +- ...ddressType.java => Socks5AddressType.java} | 8 +-- ...uthRequest.java => Socks5AuthRequest.java} | 16 +++--- ...der.java => Socks5AuthRequestDecoder.java} | 18 +++---- ...hResponse.java => Socks5AuthResponse.java} | 24 ++++----- ...er.java => Socks5AuthResponseDecoder.java} | 22 ++++---- ...5AuthScheme.java => Socks5AuthScheme.java} | 8 +-- ...5AuthStatus.java => Socks5AuthStatus.java} | 8 +-- ...5CmdRequest.java => Socks5CmdRequest.java} | 36 ++++++------- ...oder.java => Socks5CmdRequestDecoder.java} | 28 +++++----- ...mdResponse.java => Socks5CmdResponse.java} | 38 +++++++------- ...der.java => Socks5CmdResponseDecoder.java} | 31 ++++++----- ...sV5CmdStatus.java => Socks5CmdStatus.java} | 8 +-- ...SocksV5CmdType.java => Socks5CmdType.java} | 8 +-- ...ommonUtils.java => Socks5CommonUtils.java} | 5 +- ...nitRequest.java => Socks5InitRequest.java} | 22 ++++---- ...der.java => Socks5InitRequestDecoder.java} | 16 +++--- ...tResponse.java => Socks5InitResponse.java} | 20 +++---- ...er.java => Socks5InitResponseDecoder.java} | 16 +++--- .../Socks5MessageEncoder.java} | 46 ++++++++-------- ...SocksV5Request.java => Socks5Request.java} | 34 +++++++----- ...equestType.java => Socks5RequestType.java} | 2 +- ...cksV5Response.java => Socks5Response.java} | 34 +++++++----- ...ponseType.java => Socks5ResponseType.java} | 2 +- ....java => Socks5SubnegotiationVersion.java} | 8 +-- .../codec/socksx/v5/UnknownSocks5Request.java | 39 ++++++++++++++ .../socksx/v5/UnknownSocks5Response.java | 39 ++++++++++++++ .../socksx/v5/UnknownSocksV5Request.java | 45 ---------------- .../socksx/v5/UnknownSocksV5Response.java | 45 ---------------- .../handler/codec/socksx/v5/package-info.java | 3 +- ....java => Socks4CmdRequestDecoderTest.java} | 26 ++++------ ...estTest.java => Socks4CmdRequestTest.java} | 4 +- ...java => Socks4CmdResponseDecoderTest.java} | 28 +++++----- ...seTest.java => Socks4CmdResponseTest.java} | 4 +- ...tUtils.java => Socks4CommonTestUtils.java} | 14 +++-- ...java => Socks5AuthRequestDecoderTest.java} | 10 ++-- ...stTest.java => Socks5AuthRequestTest.java} | 14 ++--- ...ava => Socks5AuthResponseDecoderTest.java} | 16 +++--- ...eTest.java => Socks5AuthResponseTest.java} | 4 +- ....java => Socks5CmdRequestDecoderTest.java} | 38 +++++++------- ...estTest.java => Socks5CmdRequestTest.java} | 18 +++---- ...java => Socks5CmdResponseDecoderTest.java} | 40 +++++++------- ...seTest.java => Socks5CmdResponseTest.java} | 42 +++++++-------- ...tUtils.java => Socks5CommonTestUtils.java} | 14 +++-- ...stTest.java => Socks5InitRequestTest.java} | 4 +- ...eTest.java => Socks5InitResponseTest.java} | 4 +- .../SocksPortUnificationServerHandler.java | 18 ++++--- .../socksproxy/SocksServerConnectHandler.java | 36 ++++++------- .../socksproxy/SocksServerHandler.java | 50 ++++++++---------- 67 files changed, 668 insertions(+), 655 deletions(-) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdRequest.java => Socks4CmdRequest.java} (74%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdRequestDecoder.java => Socks4CmdRequestDecoder.java} (77%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdResponse.java => Socks4CmdResponse.java} (76%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdResponseDecoder.java => Socks4CmdResponseDecoder.java} (69%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdStatus.java => Socks4CmdStatus.java} (86%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdType.java => Socks4CmdType.java} (86%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{SocksV4CommonUtils.java => Socks4CommonUtils.java} (97%) create mode 100755 codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4MessageEncoder.java rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{SocksV4Request.java => Socks4Request.java} (65%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{SocksV4Response.java => Socks4Response.java} (65%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{UnknownSocksV4Request.java => UnknownSocks4Request.java} (62%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/{UnknownSocksV4Response.java => UnknownSocks4Response.java} (62%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5AddressType.java => Socks5AddressType.java} (85%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthRequest.java => Socks5AuthRequest.java} (85%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthRequestDecoder.java => Socks5AuthRequestDecoder.java} (76%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthResponse.java => Socks5AuthResponse.java} (61%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthResponseDecoder.java => Socks5AuthResponseDecoder.java} (68%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthScheme.java => Socks5AuthScheme.java} (85%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthStatus.java => Socks5AuthStatus.java} (85%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdRequest.java => Socks5CmdRequest.java} (75%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdRequestDecoder.java => Socks5CmdRequestDecoder.java} (74%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdResponse.java => Socks5CmdResponse.java} (82%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdResponseDecoder.java => Socks5CmdResponseDecoder.java} (71%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdStatus.java => Socks5CmdStatus.java} (88%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdType.java => Socks5CmdType.java} (86%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5CommonUtils.java => Socks5CommonUtils.java} (97%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5InitRequest.java => Socks5InitRequest.java} (66%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5InitRequestDecoder.java => Socks5InitRequestDecoder.java} (78%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5InitResponse.java => Socks5InitResponse.java} (66%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5InitResponseDecoder.java => Socks5InitResponseDecoder.java} (77%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/{SocksMessageEncoder.java => v5/Socks5MessageEncoder.java} (50%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5Request.java => Socks5Request.java} (61%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5RequestType.java => Socks5RequestType.java} (95%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5Response.java => Socks5Response.java} (60%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5ResponseType.java => Socks5ResponseType.java} (95%) rename codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/{SocksV5SubnegotiationVersion.java => Socks5SubnegotiationVersion.java} (81%) create mode 100755 codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocks5Request.java create mode 100755 codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocks5Response.java delete mode 100755 codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocksV5Request.java delete mode 100755 codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocksV5Response.java rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdRequestDecoderTest.java => Socks4CmdRequestDecoderTest.java} (70%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdRequestTest.java => Socks4CmdRequestTest.java} (92%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdResponseDecoderTest.java => Socks4CmdResponseDecoderTest.java} (59%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/{SocksV4CmdResponseTest.java => Socks4CmdResponseTest.java} (91%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/{SocksV4CommonTestUtils.java => Socks4CommonTestUtils.java} (77%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthRequestDecoderTest.java => Socks5AuthRequestDecoderTest.java} (77%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthRequestTest.java => Socks5AuthRequestTest.java} (88%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthResponseDecoderTest.java => Socks5AuthResponseDecoderTest.java} (74%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5AuthResponseTest.java => Socks5AuthResponseTest.java} (91%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdRequestDecoderTest.java => Socks5CmdRequestDecoderTest.java} (77%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdRequestTest.java => Socks5CmdRequestTest.java} (79%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdResponseDecoderTest.java => Socks5CmdResponseDecoderTest.java} (66%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5CmdResponseTest.java => Socks5CmdResponseTest.java} (71%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5CommonTestUtils.java => Socks5CommonTestUtils.java} (77%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5InitRequestTest.java => Socks5InitRequestTest.java} (92%) rename codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/{SocksV5InitResponseTest.java => Socks5InitResponseTest.java} (91%) diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socks/package-info.java b/codec-socks/src/main/java/io/netty/handler/codec/socks/package-info.java index bf76bf924d..06d06a4010 100644 --- a/codec-socks/src/main/java/io/netty/handler/codec/socks/package-info.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socks/package-info.java @@ -18,4 +18,3 @@ * Encoder, decoder and their related message types for Socks. */ package io.netty.handler.codec.socks; -// TODO: Combine decoders into one. diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksMessage.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksMessage.java index 13d5604c66..a31c8034db 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksMessage.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksMessage.java @@ -15,16 +15,10 @@ */ package io.netty.handler.codec.socksx; -import io.netty.buffer.ByteBuf; - /** * An abstract class that defines a SocksMessage, providing common properties for - * {@link SocksV5Request} and {@link SocksV5Response}. - * - * @see SocksV5Request - * @see SocksV5Response + * {@link SocksRequest} and {@link SocksResponse}. */ - public abstract class SocksMessage { private final SocksMessageType type; private final SocksProtocolVersion protocolVersion; @@ -57,10 +51,4 @@ public abstract class SocksMessage { public SocksProtocolVersion protocolVersion() { return protocolVersion; } - - /** - * @deprecated Do not use; this method was intended for an internal use only. - */ - @Deprecated - public abstract void encodeAsByteBuf(ByteBuf byteBuf); } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksProtocolVersion.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksProtocolVersion.java index 02b7419548..c0650df526 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksProtocolVersion.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksProtocolVersion.java @@ -27,14 +27,6 @@ public enum SocksProtocolVersion { this.b = b; } - /** - * @deprecated Use {@link #valueOf(byte)} instead. - */ - @Deprecated - public static SocksProtocolVersion fromByte(byte b) { - return valueOf(b); - } - public static SocksProtocolVersion valueOf(byte b) { for (SocksProtocolVersion code : values()) { if (code.b == b) { diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksRequest.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksRequest.java index a67290fe82..449cdbc891 100644 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksRequest.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksRequest.java @@ -16,18 +16,12 @@ package io.netty.handler.codec.socksx; +import io.netty.handler.codec.socksx.v4.Socks4Request; +import io.netty.handler.codec.socksx.v5.Socks5Request; /** - * An abstract class that defines a SocksRequest, providing common properties for - * {@link SocksV5InitRequest}, - * {@link SocksV5AuthRequest}, - * {@link SocksV5CmdRequest} - * and {@link UnknownSocksV5Request}. - * - * @see io.netty.handler.codec.socks.SocksInitRequest - * @see io.netty.handler.codec.socks.SocksAuthRequest - * @see io.netty.handler.codec.socks.SocksCmdRequest - * @see io.netty.handler.codec.socks.UnknownSocksRequest + * An abstract class that defines a SOCKS request, providing common properties for + * {@link Socks4Request} and {@link Socks5Request}. */ public abstract class SocksRequest extends SocksMessage { diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksResponse.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksResponse.java index 4299364b51..975123b6d6 100644 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksResponse.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksResponse.java @@ -15,6 +15,13 @@ */ package io.netty.handler.codec.socksx; +import io.netty.handler.codec.socksx.v4.Socks4Response; +import io.netty.handler.codec.socksx.v5.Socks5Response; + +/** + * An abstract class that defines a SOCKS response, providing common properties for + * {@link Socks4Response} and {@link Socks5Response}. + */ public abstract class SocksResponse extends SocksMessage { protected SocksResponse(SocksProtocolVersion protocolVersion) { super(protocolVersion, SocksMessageType.RESPONSE); diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/package-info.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/package-info.java index 99151c23fe..799f65997f 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/package-info.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/package-info.java @@ -15,6 +15,6 @@ */ /** - * Encoder, decoder and their related message types for Socks. + * Encoder, decoder and their related message types for SOCKS protocol. */ package io.netty.handler.codec.socksx; diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequest.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequest.java similarity index 74% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequest.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequest.java index edf2a34764..42e3866b7f 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequest.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequest.java @@ -18,26 +18,24 @@ package io.netty.handler.codec.socksx.v4; import io.netty.buffer.ByteBuf; import io.netty.util.CharsetUtil; import io.netty.util.NetUtil; -import io.netty.util.internal.SystemPropertyUtil; import java.net.IDN; /** * An socksv4a cmd request. * - * @see SocksV4Response - * @see SocksV4CmdRequestDecoder + * @see Socks4Response + * @see Socks4CmdRequestDecoder */ - -public final class SocksV4CmdRequest extends SocksV4Request { +public final class Socks4CmdRequest extends Socks4Request { private final String userId; - private final SocksV4CmdType cmdType; + private final Socks4CmdType cmdType; private final String host; private final int port; private static final byte[] IPv4_DOMAIN_MARKER = {0x00, 0x00, 0x00, 0x01}; - public SocksV4CmdRequest(String userId, SocksV4CmdType cmdType, String host, int port) { + public Socks4CmdRequest(String userId, Socks4CmdType cmdType, String host, int port) { if (userId == null) { throw new NullPointerException("username"); } @@ -56,48 +54,48 @@ public final class SocksV4CmdRequest extends SocksV4Request { this.port = port; } - public SocksV4CmdRequest(SocksV4CmdType cmdType, String host, int port) { + public Socks4CmdRequest(Socks4CmdType cmdType, String host, int port) { this("", cmdType, host, port); } /** - * Returns the {@link SocksV4CmdType} of this {@link SocksV4Request} + * Returns the {@link Socks4CmdType} of this {@link Socks4Request} * - * @return The {@link SocksV4CmdType} of this {@link SocksV4Request} + * @return The {@link Socks4CmdType} of this {@link Socks4Request} */ - public SocksV4CmdType cmdType() { + public Socks4CmdType cmdType() { return cmdType; } /** - * Returns host that is used as a parameter in {@link SocksV4CmdType} + * Returns host that is used as a parameter in {@link Socks4CmdType} * - * @return host that is used as a parameter in {@link SocksV4CmdType} + * @return host that is used as a parameter in {@link Socks4CmdType} */ public String host() { return IDN.toUnicode(host); } /** - * Returns userId that is used as a parameter in {@link SocksV4CmdType} + * Returns userId that is used as a parameter in {@link Socks4CmdType} * - * @return userId that is used as a parameter in {@link SocksV4CmdType} + * @return userId that is used as a parameter in {@link Socks4CmdType} */ public String userId() { return userId; } /** - * Returns port that is used as a parameter in {@link SocksV4CmdType} + * Returns port that is used as a parameter in {@link Socks4CmdType} * - * @return port that is used as a parameter in {@link SocksV4CmdType} + * @return port that is used as a parameter in {@link Socks4CmdType} */ public int port() { return port; } @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { + void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); byteBuf.writeByte(cmdType.byteValue()); byteBuf.writeShort(port); diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequestDecoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequestDecoder.java similarity index 77% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequestDecoder.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequestDecoder.java index 747388ea0f..aa44677c7d 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequestDecoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequestDecoder.java @@ -19,29 +19,27 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.socksx.SocksProtocolVersion; -import io.netty.handler.codec.socksx.v4.SocksV4CmdRequestDecoder.State; +import io.netty.handler.codec.socksx.v4.Socks4CmdRequestDecoder.State; import io.netty.util.CharsetUtil; -import io.netty.util.internal.SystemPropertyUtil; import java.util.List; /** - * Decodes {@link ByteBuf}s into {@link SocksV4CmdRequest}. + * Decodes {@link ByteBuf}s into {@link Socks4CmdRequest}. * Before returning SocksRequest decoder removes itself from pipeline. */ -public class SocksV4CmdRequestDecoder extends ReplayingDecoder { - private static final String name = "SOCKS_CMD_REQUEST_DECODER"; +public class Socks4CmdRequestDecoder extends ReplayingDecoder { private SocksProtocolVersion version; - private SocksV4CmdType cmdType; + private Socks4CmdType cmdType; @SuppressWarnings("UnusedDeclaration") private byte reserved; private String host; private int port; private String userId; - private SocksV4Request msg = UnknownSocksV4Request.getInstance(); + private Socks4Request msg = UnknownSocks4Request.INSTANCE; - public SocksV4CmdRequestDecoder() { + public Socks4CmdRequestDecoder() { super(State.CHECK_PROTOCOL_VERSION); } @@ -56,9 +54,9 @@ public class SocksV4CmdRequestDecoder extends ReplayingDecoder { checkpoint(State.READ_CMD_HEADER); } case READ_CMD_HEADER: { - cmdType = SocksV4CmdType.valueOf(byteBuf.readByte()); + cmdType = Socks4CmdType.valueOf(byteBuf.readByte()); port = byteBuf.readUnsignedShort(); - host = SocksV4CommonUtils.intToIp(byteBuf.readInt()); + host = Socks4CommonUtils.intToIp(byteBuf.readInt()); checkpoint(State.READ_CMD_USERID); } case READ_CMD_USERID: { @@ -67,17 +65,17 @@ public class SocksV4CmdRequestDecoder extends ReplayingDecoder { } case READ_CMD_DOMAIN: { // Check for Socks4a protocol marker 0,0,0,x - if (!host.equals("0.0.0.0") && host.startsWith("0.0.0.")) { + if (!"0.0.0.0".equals(host) && host.startsWith("0.0.0.")) { host = readNullTerminatedString(byteBuf); } - msg = new SocksV4CmdRequest(userId, cmdType, host, port); + msg = new Socks4CmdRequest(userId, cmdType, host, port); } } ctx.pipeline().remove(this); out.add(msg); } private static String readNullTerminatedString(ByteBuf byteBuf) throws Exception { - byte NULL_BYTE = (byte) 0x00; + byte NULL_BYTE = 0x00; // Could be used for DoS String string = byteBuf.readBytes(byteBuf.bytesBefore(NULL_BYTE)).toString(CharsetUtil.US_ASCII); // Read NULL-byte diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponse.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponse.java similarity index 76% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponse.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponse.java index dc591dbdb3..78142c969a 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponse.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponse.java @@ -23,18 +23,18 @@ import java.net.IDN; /** * A socks cmd response. * - * @see SocksV4CmdRequest - * @see SocksV4CmdResponseDecoder + * @see Socks4CmdRequest + * @see Socks4CmdResponseDecoder */ -public final class SocksV4CmdResponse extends SocksV4Response { - private final SocksV4CmdStatus cmdStatus; +public final class Socks4CmdResponse extends Socks4Response { + private final Socks4CmdStatus cmdStatus; 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[] IPv4_HOSTNAME_ZEROED = { 0x00, 0x00, 0x00, 0x00 }; - public SocksV4CmdResponse(SocksV4CmdStatus cmdStatus) { + public Socks4CmdResponse(Socks4CmdStatus cmdStatus) { this(cmdStatus, null, 0); } @@ -44,13 +44,13 @@ public final class SocksV4CmdResponse extends SocksV4Response { * @param cmdStatus status of the response * @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 java.net.IDN#toASCII(String)}. + * 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 java.net.IDN#toASCII(String) + * @see IDN#toASCII(String) */ - public SocksV4CmdResponse(SocksV4CmdStatus cmdStatus, String host, int port) { + public Socks4CmdResponse(Socks4CmdStatus cmdStatus, String host, int port) { if (cmdStatus == null) { throw new NullPointerException("cmdStatus"); } @@ -68,20 +68,20 @@ public final class SocksV4CmdResponse extends SocksV4Response { } /** - * Returns the {@link SocksV4CmdStatus} of this {@link SocksV4Response} + * Returns the {@link Socks4CmdStatus} of this {@link Socks4Response} * - * @return The {@link SocksV4CmdStatus} of this {@link SocksV4Response} + * @return The {@link Socks4CmdStatus} of this {@link Socks4Response} */ - public SocksV4CmdStatus cmdStatus() { + public Socks4CmdStatus cmdStatus() { return cmdStatus; } /** - * Returns host that is used as a parameter in {@link io.netty.handler.codec.socks.v4.SocksV4CmdType}. + * Returns host that is used as a parameter in {@link Socks4CmdType}. * 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.v4.SocksV4CmdType} + * @return host that is used as a parameter in {@link Socks4CmdType} * or null when there was no host specified during response construction */ public String host() { @@ -93,10 +93,10 @@ public final class SocksV4CmdResponse extends SocksV4Response { } /** - * Returns port that is used as a parameter in {@link io.netty.handler.codec.socks.v4.SocksV4CmdType}. + * Returns port that is used as a parameter in {@link Socks4CmdType}. * 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.v4.SocksV4CmdType} + * @return port that is used as a parameter in {@link Socks4CmdType} */ public int port() { return port; diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponseDecoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponseDecoder.java similarity index 69% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponseDecoder.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponseDecoder.java index 48055a0e07..27bf318e3a 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponseDecoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponseDecoder.java @@ -18,27 +18,23 @@ package io.netty.handler.codec.socksx.v4; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; -import io.netty.handler.codec.socks.SocksProtocolVersion; -import io.netty.handler.codec.socksx.v4.SocksV4CmdResponseDecoder.State; -import io.netty.util.CharsetUtil; +import io.netty.handler.codec.socksx.v4.Socks4CmdResponseDecoder.State; import java.util.List; /** - * Decodes {@link ByteBuf}s into {@link SocksV4CmdResponse}. + * Decodes {@link ByteBuf}s into {@link Socks4CmdResponse}. * Before returning SocksResponse decoder removes itself from pipeline. */ -public class SocksV4CmdResponseDecoder extends ReplayingDecoder { - private static final String name = "SOCKS_CMD_RESPONSE_DECODER"; +public class Socks4CmdResponseDecoder extends ReplayingDecoder { - private SocksProtocolVersion version; - private SocksV4CmdStatus cmdStatus; + private Socks4CmdStatus cmdStatus; private String host; private int port; - private SocksV4Response msg = UnknownSocksV4Response.getInstance(); + private Socks4Response msg = UnknownSocks4Response.INSTANCE; - public SocksV4CmdResponseDecoder() { + public Socks4CmdResponseDecoder() { super(State.CHECK_NULL_BYTE); } @@ -52,13 +48,13 @@ public class SocksV4CmdResponseDecoder extends ReplayingDecoder { checkpoint(State.READ_CMD_HEADER); } case READ_CMD_HEADER: { - cmdStatus = SocksV4CmdStatus.valueOf(byteBuf.readByte()); + cmdStatus = Socks4CmdStatus.valueOf(byteBuf.readByte()); checkpoint(State.READ_CMD_ADDRESS); } case READ_CMD_ADDRESS: { port = byteBuf.readUnsignedShort(); - host = SocksV4CommonUtils.intToIp(byteBuf.readInt()); - msg = new SocksV4CmdResponse(cmdStatus, host, port); + host = Socks4CommonUtils.intToIp(byteBuf.readInt()); + msg = new Socks4CmdResponse(cmdStatus, host, port); } } ctx.pipeline().remove(this); diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdStatus.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdStatus.java similarity index 86% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdStatus.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdStatus.java index 765d2a254e..65b38a7c93 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdStatus.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdStatus.java @@ -15,7 +15,7 @@ */ package io.netty.handler.codec.socksx.v4; -public enum SocksV4CmdStatus { +public enum Socks4CmdStatus { SUCCESS((byte) 0x5a), REJECTED_OR_FAILED((byte) 0x5b), IDENTD_UNREACHABLE((byte) 0x5c), @@ -24,12 +24,12 @@ public enum SocksV4CmdStatus { private final byte b; - SocksV4CmdStatus(byte b) { + Socks4CmdStatus(byte b) { this.b = b; } - public static SocksV4CmdStatus valueOf(byte b) { - for (SocksV4CmdStatus code : values()) { + public static Socks4CmdStatus valueOf(byte b) { + for (Socks4CmdStatus code : values()) { if (code.b == b) { return code; } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdType.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdType.java similarity index 86% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdType.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdType.java index f7266fdb20..40d9c612ba 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CmdType.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CmdType.java @@ -15,19 +15,19 @@ */ package io.netty.handler.codec.socksx.v4; -public enum SocksV4CmdType { +public enum Socks4CmdType { CONNECT((byte) 0x01), BIND((byte) 0x02), UNKNOWN((byte) 0xff); private final byte b; - SocksV4CmdType(byte b) { + Socks4CmdType(byte b) { this.b = b; } - public static SocksV4CmdType valueOf(byte b) { - for (SocksV4CmdType code : values()) { + public static Socks4CmdType valueOf(byte b) { + for (Socks4CmdType code : values()) { if (code.b == b) { return code; } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CommonUtils.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CommonUtils.java similarity index 97% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CommonUtils.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CommonUtils.java index fdc5f45e2c..56f20737a8 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4CommonUtils.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4CommonUtils.java @@ -16,7 +16,8 @@ package io.netty.handler.codec.socksx.v4; import io.netty.util.internal.StringUtil; -public final class SocksV4CommonUtils { +final class Socks4CommonUtils { + private static final int SECOND_ADDRESS_OCTET_SHIFT = 16; private static final int FIRST_ADDRESS_OCTET_SHIFT = 24; private static final int THIRD_ADDRESS_OCTET_SHIFT = 8; @@ -25,7 +26,7 @@ public final class SocksV4CommonUtils { /** * A constructor to stop this class being constructed. */ - private SocksV4CommonUtils() { + private Socks4CommonUtils() { // NOOP } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4MessageEncoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4MessageEncoder.java new file mode 100755 index 0000000000..4f68cfff56 --- /dev/null +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4MessageEncoder.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.socksx.v4; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.handler.codec.socksx.SocksMessage; +import io.netty.handler.codec.socksx.SocksProtocolVersion; + +/** + * Encodes a {@link Socks4Request} and {@link Socks4Response} into a {@link ByteBuf}. + */ +@ChannelHandler.Sharable +public final class Socks4MessageEncoder extends MessageToByteEncoder { + + public static final Socks4MessageEncoder INSTANCE = new Socks4MessageEncoder(); + + private Socks4MessageEncoder() { } + + @Override + public boolean acceptOutboundMessage(Object msg) throws Exception { + return super.acceptOutboundMessage(msg) && + ((SocksMessage) msg).protocolVersion() == SocksProtocolVersion.SOCKS4a; + } + + @Override + protected void encode(ChannelHandlerContext ctx, SocksMessage msg, ByteBuf out) throws Exception { + if (msg instanceof Socks4Response) { + ((Socks4Response) msg).encodeAsByteBuf(out); + } else if (msg instanceof Socks4Request) { + ((Socks4Request) msg).encodeAsByteBuf(out); + } else { + // Should not reach here. + throw new Error(); + } + } +} diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4Request.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4Request.java similarity index 65% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4Request.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4Request.java index cee0cc25bb..c57a89abf0 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4Request.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4Request.java @@ -15,20 +15,26 @@ */ package io.netty.handler.codec.socksx.v4; -import io.netty.handler.codec.socksx.SocksMessage; -import io.netty.handler.codec.socksx.SocksMessageType; +import io.netty.buffer.ByteBuf; +import io.netty.handler.codec.socks.SocksMessage; import io.netty.handler.codec.socksx.SocksProtocolVersion; import io.netty.handler.codec.socksx.SocksRequest; /** * An abstract class that defines a SocksRequest, providing common properties for - * {@link SocksV4CmdRequest}. + * {@link Socks4CmdRequest}. * - * @see SocksV4CmdRequest - * @see UnknownSocksV4Request + * @see Socks4CmdRequest + * @see UnknownSocks4Request */ -public abstract class SocksV4Request extends SocksRequest { - protected SocksV4Request() { +public abstract class Socks4Request extends SocksRequest { + protected Socks4Request() { super(SocksProtocolVersion.SOCKS4a); } + + /** + * We could have defined this method in {@link SocksMessage} as a protected method, but we did not, + * because we do not want to expose this method to users. + */ + abstract void encodeAsByteBuf(ByteBuf byteBuf); } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4Response.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4Response.java similarity index 65% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4Response.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4Response.java index cf4ad582c9..50924dd59c 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/SocksV4Response.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/Socks4Response.java @@ -15,22 +15,27 @@ */ package io.netty.handler.codec.socksx.v4; -import io.netty.handler.codec.socksx.SocksMessage; -import io.netty.handler.codec.socksx.SocksMessageType; +import io.netty.buffer.ByteBuf; +import io.netty.handler.codec.socks.SocksMessage; import io.netty.handler.codec.socksx.SocksProtocolVersion; import io.netty.handler.codec.socksx.SocksResponse; -import io.netty.handler.codec.socksx.v5.SocksV5ResponseType; /** * An abstract class that defines a SocksResponse, providing common properties for - * {@link SocksV4CmdResponse}. + * {@link Socks4CmdResponse}. * - * @see SocksV4CmdResponse - * @see UnknownSocksV4Response + * @see Socks4CmdResponse + * @see UnknownSocks4Response */ -public abstract class SocksV4Response extends SocksResponse { +public abstract class Socks4Response extends SocksResponse { - protected SocksV4Response() { + protected Socks4Response() { super(SocksProtocolVersion.SOCKS4a); } + + /** + * We could have defined this method in {@link SocksMessage} as a protected method, but we did not, + * because we do not want to expose this method to users. + */ + abstract void encodeAsByteBuf(ByteBuf byteBuf); } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocksV4Request.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocks4Request.java similarity index 62% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocksV4Request.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocks4Request.java index f19ce391e8..5d0ee6c9cb 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocksV4Request.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocks4Request.java @@ -20,23 +20,16 @@ import io.netty.buffer.ByteBuf; /** * An unknown socks request. * - * @see SocksV4CmdRequestDecoder + * @see Socks4CmdRequestDecoder */ -public final class UnknownSocksV4Request extends SocksV4Request { +public final class UnknownSocks4Request extends Socks4Request { - public UnknownSocksV4Request() { - } + public static final UnknownSocks4Request INSTANCE = new UnknownSocks4Request(); + + private UnknownSocks4Request() { } @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { + void encodeAsByteBuf(ByteBuf byteBuf) { // NOOP } - - private static class UnknownSocksV4RequestHolder { - public static final UnknownSocksV4Request HOLDER_INSTANCE = new UnknownSocksV4Request(); - } - - public static UnknownSocksV4Request getInstance() { - return UnknownSocksV4RequestHolder.HOLDER_INSTANCE; - } } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocksV4Response.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocks4Response.java similarity index 62% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocksV4Response.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocks4Response.java index a629a3b0b2..7be1de8f12 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocksV4Response.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/UnknownSocks4Response.java @@ -20,23 +20,16 @@ import io.netty.buffer.ByteBuf; /** * An unknown socks response. * - * @see SocksV4CmdResponseDecoder + * @see Socks4CmdResponseDecoder */ -public final class UnknownSocksV4Response extends SocksV4Response { +public final class UnknownSocks4Response extends Socks4Response { - public UnknownSocksV4Response() { - } + public static final UnknownSocks4Response INSTANCE = new UnknownSocks4Response(); + + private UnknownSocks4Response() { } @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { + void encodeAsByteBuf(ByteBuf byteBuf) { // NOOP } - - private static class UnknownSocksV4ResponseHolder { - public static final UnknownSocksV4Response HOLDER_INSTANCE = new UnknownSocksV4Response(); - } - - public static UnknownSocksV4Response getInstance() { - return UnknownSocksV4ResponseHolder.HOLDER_INSTANCE; - } } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/package-info.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/package-info.java index 2e1dc4b888..07018d3b93 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/package-info.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v4/package-info.java @@ -15,6 +15,6 @@ */ /** - * Encoder, decoder and their related message types for Socks. + * Encoder, decoder and their related message types for SOCKSv4 protocol. */ package io.netty.handler.codec.socksx.v4; diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AddressType.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AddressType.java similarity index 85% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AddressType.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AddressType.java index 3bb952c9b9..f0415f4cea 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AddressType.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AddressType.java @@ -16,7 +16,7 @@ package io.netty.handler.codec.socksx.v5; -public enum SocksV5AddressType { +public enum Socks5AddressType { IPv4((byte) 0x01), DOMAIN((byte) 0x03), IPv6((byte) 0x04), @@ -24,12 +24,12 @@ public enum SocksV5AddressType { private final byte b; - SocksV5AddressType(byte b) { + Socks5AddressType(byte b) { this.b = b; } - public static SocksV5AddressType valueOf(byte b) { - for (SocksV5AddressType code : values()) { + public static Socks5AddressType valueOf(byte b) { + for (Socks5AddressType code : values()) { if (code.b == b) { return code; } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequest.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequest.java similarity index 85% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequest.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequest.java index 8bb3b07ac8..19b0260a6a 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequest.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequest.java @@ -23,18 +23,18 @@ import java.nio.charset.CharsetEncoder; /** * An socks auth request. * - * @see SocksV5AuthResponse - * @see SocksV5AuthRequestDecoder + * @see Socks5AuthResponse + * @see Socks5AuthRequestDecoder */ -public final class SocksV5AuthRequest extends SocksV5Request { +public final class Socks5AuthRequest extends Socks5Request { private static final CharsetEncoder asciiEncoder = CharsetUtil.getEncoder(CharsetUtil.US_ASCII); - private static final SocksV5SubnegotiationVersion SUBNEGOTIATION_VERSION = - SocksV5SubnegotiationVersion.AUTH_PASSWORD; + private static final Socks5SubnegotiationVersion SUBNEGOTIATION_VERSION = + Socks5SubnegotiationVersion.AUTH_PASSWORD; private final String username; private final String password; - public SocksV5AuthRequest(String username, String password) { - super(SocksV5RequestType.AUTH); + public Socks5AuthRequest(String username, String password) { + super(Socks5RequestType.AUTH); if (username == null) { throw new NullPointerException("username"); } @@ -74,7 +74,7 @@ public final class SocksV5AuthRequest extends SocksV5Request { } @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { + void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(SUBNEGOTIATION_VERSION.byteValue()); byteBuf.writeByte(username.length()); byteBuf.writeBytes(username.getBytes(CharsetUtil.US_ASCII)); diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequestDecoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequestDecoder.java similarity index 76% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequestDecoder.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequestDecoder.java index 9f8034b39a..3126337353 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequestDecoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequestDecoder.java @@ -18,23 +18,23 @@ package io.netty.handler.codec.socksx.v5; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; -import io.netty.handler.codec.socksx.v5.SocksV5AuthRequestDecoder.State; +import io.netty.handler.codec.socksx.v5.Socks5AuthRequestDecoder.State; import io.netty.util.CharsetUtil; import java.util.List; /** - * Decodes {@link ByteBuf}s into {@link SocksV5AuthRequest}. + * Decodes {@link ByteBuf}s into {@link Socks5AuthRequest}. * Before returning SocksRequest decoder removes itself from pipeline. */ -public class SocksV5AuthRequestDecoder extends ReplayingDecoder { - private SocksV5SubnegotiationVersion version; +public class Socks5AuthRequestDecoder extends ReplayingDecoder { + private Socks5SubnegotiationVersion version; private int fieldLength; private String username; private String password; - private SocksV5Request msg = UnknownSocksV5Request.getInstance(); + private Socks5Request msg = UnknownSocks5Request.INSTANCE; - public SocksV5AuthRequestDecoder() { + public Socks5AuthRequestDecoder() { super(State.CHECK_PROTOCOL_VERSION); } @@ -42,8 +42,8 @@ public class SocksV5AuthRequestDecoder extends ReplayingDecoder { protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List out) throws Exception { switch (state()) { case CHECK_PROTOCOL_VERSION: { - version = SocksV5SubnegotiationVersion.valueOf(byteBuf.readByte()); - if (version != SocksV5SubnegotiationVersion.AUTH_PASSWORD) { + version = Socks5SubnegotiationVersion.valueOf(byteBuf.readByte()); + if (version != Socks5SubnegotiationVersion.AUTH_PASSWORD) { break; } checkpoint(State.READ_USERNAME); @@ -56,7 +56,7 @@ public class SocksV5AuthRequestDecoder extends ReplayingDecoder { case READ_PASSWORD: { fieldLength = byteBuf.readByte(); password = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII); - msg = new SocksV5AuthRequest(username, password); + msg = new Socks5AuthRequest(username, password); } } ctx.pipeline().remove(this); diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponse.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponse.java similarity index 61% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponse.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponse.java index 4ceffdb0fc..7fe87755e1 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponse.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponse.java @@ -20,16 +20,16 @@ import io.netty.buffer.ByteBuf; /** * An socks auth response. * - * @see SocksV5AuthRequest - * @see SocksV5AuthResponseDecoder + * @see Socks5AuthRequest + * @see Socks5AuthResponseDecoder */ -public final class SocksV5AuthResponse extends SocksV5Response { - private static final SocksV5SubnegotiationVersion SUBNEGOTIATION_VERSION = - SocksV5SubnegotiationVersion.AUTH_PASSWORD; - private final SocksV5AuthStatus authStatus; +public final class Socks5AuthResponse extends Socks5Response { + private static final Socks5SubnegotiationVersion SUBNEGOTIATION_VERSION = + Socks5SubnegotiationVersion.AUTH_PASSWORD; + private final Socks5AuthStatus authStatus; - public SocksV5AuthResponse(SocksV5AuthStatus authStatus) { - super(SocksV5ResponseType.AUTH); + public Socks5AuthResponse(Socks5AuthStatus authStatus) { + super(Socks5ResponseType.AUTH); if (authStatus == null) { throw new NullPointerException("authStatus"); } @@ -37,16 +37,16 @@ public final class SocksV5AuthResponse extends SocksV5Response { } /** - * Returns the {@link SocksV5AuthStatus} of this {@link SocksV5AuthResponse} + * Returns the {@link Socks5AuthStatus} of this {@link Socks5AuthResponse} * - * @return The {@link SocksV5AuthStatus} of this {@link SocksV5AuthResponse} + * @return The {@link Socks5AuthStatus} of this {@link Socks5AuthResponse} */ - public SocksV5AuthStatus authStatus() { + public Socks5AuthStatus authStatus() { return authStatus; } @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { + void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(SUBNEGOTIATION_VERSION.byteValue()); byteBuf.writeByte(authStatus.byteValue()); } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponseDecoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponseDecoder.java similarity index 68% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponseDecoder.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponseDecoder.java index c5e02d18a8..73447b9429 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponseDecoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponseDecoder.java @@ -18,20 +18,20 @@ package io.netty.handler.codec.socksx.v5; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; -import io.netty.handler.codec.socksx.v5.SocksV5AuthResponseDecoder.State; +import io.netty.handler.codec.socksx.v5.Socks5AuthResponseDecoder.State; import java.util.List; /** - * Decodes {@link ByteBuf}s into {@link SocksV5AuthResponse}. + * Decodes {@link ByteBuf}s into {@link Socks5AuthResponse}. * Before returning SocksResponse decoder removes itself from pipeline. */ -public class SocksV5AuthResponseDecoder extends ReplayingDecoder { - private SocksV5SubnegotiationVersion version; - private SocksV5AuthStatus authStatus; - private SocksV5Response msg = UnknownSocksV5Response.getInstance(); +public class Socks5AuthResponseDecoder extends ReplayingDecoder { + private Socks5SubnegotiationVersion version; + private Socks5AuthStatus authStatus; + private Socks5Response msg = UnknownSocks5Response.INSTANCE; - public SocksV5AuthResponseDecoder() { + public Socks5AuthResponseDecoder() { super(State.CHECK_PROTOCOL_VERSION); } @@ -40,15 +40,15 @@ public class SocksV5AuthResponseDecoder extends ReplayingDecoder { throws Exception { switch (state()) { case CHECK_PROTOCOL_VERSION: { - version = SocksV5SubnegotiationVersion.valueOf(byteBuf.readByte()); - if (version != SocksV5SubnegotiationVersion.AUTH_PASSWORD) { + version = Socks5SubnegotiationVersion.valueOf(byteBuf.readByte()); + if (version != Socks5SubnegotiationVersion.AUTH_PASSWORD) { break; } checkpoint(State.READ_AUTH_RESPONSE); } case READ_AUTH_RESPONSE: { - authStatus = SocksV5AuthStatus.valueOf(byteBuf.readByte()); - msg = new SocksV5AuthResponse(authStatus); + authStatus = Socks5AuthStatus.valueOf(byteBuf.readByte()); + msg = new Socks5AuthResponse(authStatus); } } channelHandlerContext.pipeline().remove(this); diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthScheme.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthScheme.java similarity index 85% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthScheme.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthScheme.java index 5613fdab7a..1a7bd581e9 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthScheme.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthScheme.java @@ -16,7 +16,7 @@ package io.netty.handler.codec.socksx.v5; -public enum SocksV5AuthScheme { +public enum Socks5AuthScheme { NO_AUTH((byte) 0x00), AUTH_GSSAPI((byte) 0x01), AUTH_PASSWORD((byte) 0x02), @@ -24,12 +24,12 @@ public enum SocksV5AuthScheme { private final byte b; - SocksV5AuthScheme(byte b) { + Socks5AuthScheme(byte b) { this.b = b; } - public static SocksV5AuthScheme valueOf(byte b) { - for (SocksV5AuthScheme code : values()) { + public static Socks5AuthScheme valueOf(byte b) { + for (Socks5AuthScheme code : values()) { if (code.b == b) { return code; } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthStatus.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthStatus.java similarity index 85% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthStatus.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthStatus.java index 4742f7b190..ec04b279d5 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5AuthStatus.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5AuthStatus.java @@ -16,18 +16,18 @@ package io.netty.handler.codec.socksx.v5; -public enum SocksV5AuthStatus { +public enum Socks5AuthStatus { SUCCESS((byte) 0x00), FAILURE((byte) 0xff); private final byte b; - SocksV5AuthStatus(byte b) { + Socks5AuthStatus(byte b) { this.b = b; } - public static SocksV5AuthStatus valueOf(byte b) { - for (SocksV5AuthStatus code : values()) { + public static Socks5AuthStatus valueOf(byte b) { + for (Socks5AuthStatus code : values()) { if (code.b == b) { return code; } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequest.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequest.java similarity index 75% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequest.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequest.java index b2a84b96e6..5c3f8cec49 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequest.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequest.java @@ -24,17 +24,17 @@ import java.net.IDN; /** * An socks cmd request. * - * @see SocksV5CmdResponse - * @see SocksV5CmdRequestDecoder + * @see Socks5CmdResponse + * @see Socks5CmdRequestDecoder */ -public final class SocksV5CmdRequest extends SocksV5Request { - private final SocksV5CmdType cmdType; - private final SocksV5AddressType addressType; +public final class Socks5CmdRequest extends Socks5Request { + private final Socks5CmdType cmdType; + private final Socks5AddressType addressType; private final String host; private final int port; - public SocksV5CmdRequest(SocksV5CmdType cmdType, SocksV5AddressType addressType, String host, int port) { - super(SocksV5RequestType.CMD); + public Socks5CmdRequest(Socks5CmdType cmdType, Socks5AddressType addressType, String host, int port) { + super(Socks5RequestType.CMD); if (cmdType == null) { throw new NullPointerException("cmdType"); } @@ -73,43 +73,43 @@ public final class SocksV5CmdRequest extends SocksV5Request { } /** - * Returns the {@link SocksV5CmdType} of this {@link SocksV5CmdRequest} + * Returns the {@link Socks5CmdType} of this {@link Socks5CmdRequest} * - * @return The {@link SocksV5CmdType} of this {@link SocksV5CmdRequest} + * @return The {@link Socks5CmdType} of this {@link Socks5CmdRequest} */ - public SocksV5CmdType cmdType() { + public Socks5CmdType cmdType() { return cmdType; } /** - * Returns the {@link SocksV5AddressType} of this {@link SocksV5CmdRequest} + * Returns the {@link Socks5AddressType} of this {@link Socks5CmdRequest} * - * @return The {@link SocksV5AddressType} of this {@link SocksV5CmdRequest} + * @return The {@link Socks5AddressType} of this {@link Socks5CmdRequest} */ - public SocksV5AddressType addressType() { + public Socks5AddressType addressType() { return addressType; } /** - * Returns host that is used as a parameter in {@link SocksV5CmdType} + * Returns host that is used as a parameter in {@link Socks5CmdType} * - * @return host that is used as a parameter in {@link SocksV5CmdType} + * @return host that is used as a parameter in {@link Socks5CmdType} */ public String host() { return IDN.toUnicode(host); } /** - * Returns port that is used as a parameter in {@link SocksV5CmdType} + * Returns port that is used as a parameter in {@link Socks5CmdType} * - * @return port that is used as a parameter in {@link SocksV5CmdType} + * @return port that is used as a parameter in {@link Socks5CmdType} */ public int port() { return port; } @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { + void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); byteBuf.writeByte(cmdType.byteValue()); byteBuf.writeByte(0x00); diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequestDecoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequestDecoder.java similarity index 74% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequestDecoder.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequestDecoder.java index d2a4245900..44609fd88e 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequestDecoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequestDecoder.java @@ -19,27 +19,27 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.socksx.SocksProtocolVersion; -import io.netty.handler.codec.socksx.v5.SocksV5CmdRequestDecoder.State; +import io.netty.handler.codec.socksx.v5.Socks5CmdRequestDecoder.State; import io.netty.util.CharsetUtil; import java.util.List; /** - * Decodes {@link ByteBuf}s into {@link SocksV5CmdRequest}. + * Decodes {@link ByteBuf}s into {@link Socks5CmdRequest}. * Before returning SocksRequest decoder removes itself from pipeline. */ -public class SocksV5CmdRequestDecoder extends ReplayingDecoder { +public class Socks5CmdRequestDecoder extends ReplayingDecoder { private SocksProtocolVersion version; private int fieldLength; - private SocksV5CmdType cmdType; - private SocksV5AddressType addressType; + private Socks5CmdType cmdType; + private Socks5AddressType addressType; @SuppressWarnings("UnusedDeclaration") private byte reserved; private String host; private int port; - private SocksV5Request msg = UnknownSocksV5Request.getInstance(); + private Socks5Request msg = UnknownSocks5Request.INSTANCE; - public SocksV5CmdRequestDecoder() { + public Socks5CmdRequestDecoder() { super(State.CHECK_PROTOCOL_VERSION); } @@ -54,30 +54,30 @@ public class SocksV5CmdRequestDecoder extends ReplayingDecoder { checkpoint(State.READ_CMD_HEADER); } case READ_CMD_HEADER: { - cmdType = SocksV5CmdType.valueOf(byteBuf.readByte()); + cmdType = Socks5CmdType.valueOf(byteBuf.readByte()); reserved = byteBuf.readByte(); - addressType = SocksV5AddressType.valueOf(byteBuf.readByte()); + addressType = Socks5AddressType.valueOf(byteBuf.readByte()); checkpoint(State.READ_CMD_ADDRESS); } case READ_CMD_ADDRESS: { switch (addressType) { case IPv4: { - host = SocksV5CommonUtils.intToIp(byteBuf.readInt()); + host = Socks5CommonUtils.intToIp(byteBuf.readInt()); port = byteBuf.readUnsignedShort(); - msg = new SocksV5CmdRequest(cmdType, addressType, host, port); + msg = new Socks5CmdRequest(cmdType, addressType, host, port); break; } case DOMAIN: { fieldLength = byteBuf.readByte(); host = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII); port = byteBuf.readUnsignedShort(); - msg = new SocksV5CmdRequest(cmdType, addressType, host, port); + msg = new Socks5CmdRequest(cmdType, addressType, host, port); break; } case IPv6: { - host = SocksV5CommonUtils.ipv6toStr(byteBuf.readBytes(16).array()); + host = Socks5CommonUtils.ipv6toStr(byteBuf.readBytes(16).array()); port = byteBuf.readUnsignedShort(); - msg = new SocksV5CmdRequest(cmdType, addressType, host, port); + msg = new Socks5CmdRequest(cmdType, addressType, host, port); break; } case UNKNOWN: diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponse.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponse.java similarity index 82% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponse.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponse.java index c57da6bacf..b464c59701 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponse.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponse.java @@ -24,13 +24,13 @@ import java.net.IDN; /** * A socks cmd response. * - * @see SocksV5CmdRequest - * @see SocksV5CmdResponseDecoder + * @see Socks5CmdRequest + * @see Socks5CmdResponseDecoder */ -public final class SocksV5CmdResponse extends SocksV5Response { - private final SocksV5CmdStatus cmdStatus; +public final class Socks5CmdResponse extends Socks5Response { + private final Socks5CmdStatus cmdStatus; - private final SocksV5AddressType addressType; + private final Socks5AddressType addressType; private final String host; private final int port; @@ -42,7 +42,7 @@ public final class SocksV5CmdResponse extends SocksV5Response { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - public SocksV5CmdResponse(SocksV5CmdStatus cmdStatus, SocksV5AddressType addressType) { + public Socks5CmdResponse(Socks5CmdStatus cmdStatus, Socks5AddressType addressType) { this(cmdStatus, addressType, null, 0); } @@ -59,8 +59,8 @@ public final class SocksV5CmdResponse extends SocksV5Response { * @throws IllegalArgumentException in case host or port cannot be validated * @see IDN#toASCII(String) */ - public SocksV5CmdResponse(SocksV5CmdStatus cmdStatus, SocksV5AddressType addressType, String host, int port) { - super(SocksV5ResponseType.CMD); + public Socks5CmdResponse(Socks5CmdStatus cmdStatus, Socks5AddressType addressType, String host, int port) { + super(Socks5ResponseType.CMD); if (cmdStatus == null) { throw new NullPointerException("cmdStatus"); } @@ -100,29 +100,29 @@ public final class SocksV5CmdResponse extends SocksV5Response { } /** - * Returns the {@link SocksV5CmdStatus} of this {@link SocksV5CmdResponse} + * Returns the {@link Socks5CmdStatus} of this {@link Socks5CmdResponse} * - * @return The {@link SocksV5CmdStatus} of this {@link SocksV5CmdResponse} + * @return The {@link Socks5CmdStatus} of this {@link Socks5CmdResponse} */ - public SocksV5CmdStatus cmdStatus() { + public Socks5CmdStatus cmdStatus() { return cmdStatus; } /** - * Returns the {@link SocksV5AddressType} of this {@link SocksV5CmdResponse} + * Returns the {@link Socks5AddressType} of this {@link Socks5CmdResponse} * - * @return The {@link SocksV5AddressType} of this {@link SocksV5CmdResponse} + * @return The {@link Socks5AddressType} of this {@link Socks5CmdResponse} */ - public SocksV5AddressType addressType() { + public Socks5AddressType addressType() { return addressType; } /** - * Returns host that is used as a parameter in {@link SocksV5CmdType}. + * Returns host that is used as a parameter in {@link Socks5CmdType}. * 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 SocksV5CmdType} + * @return host that is used as a parameter in {@link Socks5CmdType} * or null when there was no host specified during response construction */ public String host() { @@ -134,17 +134,17 @@ public final class SocksV5CmdResponse extends SocksV5Response { } /** - * Returns port that is used as a parameter in {@link SocksV5CmdType}. + * Returns port that is used as a parameter in {@link Socks5CmdType}. * 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 SocksV5CmdType} + * @return port that is used as a parameter in {@link Socks5CmdType} */ public int port() { return port; } @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { + void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); byteBuf.writeByte(cmdStatus.byteValue()); byteBuf.writeByte(0x00); diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponseDecoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponseDecoder.java similarity index 71% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponseDecoder.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponseDecoder.java index 91c3ef64a7..5354bf57b4 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponseDecoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponseDecoder.java @@ -19,26 +19,25 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.socksx.SocksProtocolVersion; -import io.netty.handler.codec.socksx.v5.SocksV5CmdResponseDecoder.State; +import io.netty.handler.codec.socksx.v5.Socks5CmdResponseDecoder.State; import io.netty.util.CharsetUtil; import java.util.List; /** - * Decodes {@link ByteBuf}s into {@link SocksV5CmdResponse}. + * Decodes {@link ByteBuf}s into {@link Socks5CmdResponse}. * Before returning SocksResponse decoder removes itself from pipeline. */ -public class SocksV5CmdResponseDecoder extends ReplayingDecoder { +public class Socks5CmdResponseDecoder extends ReplayingDecoder { private SocksProtocolVersion version; private int fieldLength; - private SocksV5CmdStatus cmdStatus; - private SocksV5AddressType addressType; - private byte reserved; + private Socks5CmdStatus cmdStatus; + private Socks5AddressType addressType; private String host; private int port; - private SocksV5Response msg = UnknownSocksV5Response.getInstance(); + private Socks5Response msg = UnknownSocks5Response.INSTANCE; - public SocksV5CmdResponseDecoder() { + public Socks5CmdResponseDecoder() { super(State.CHECK_PROTOCOL_VERSION); } @@ -53,30 +52,30 @@ public class SocksV5CmdResponseDecoder extends ReplayingDecoder { checkpoint(State.READ_CMD_HEADER); } case READ_CMD_HEADER: { - cmdStatus = SocksV5CmdStatus.valueOf(byteBuf.readByte()); - reserved = byteBuf.readByte(); - addressType = SocksV5AddressType.valueOf(byteBuf.readByte()); + cmdStatus = Socks5CmdStatus.valueOf(byteBuf.readByte()); + byteBuf.skipBytes(1); // reserved + addressType = Socks5AddressType.valueOf(byteBuf.readByte()); checkpoint(State.READ_CMD_ADDRESS); } case READ_CMD_ADDRESS: { switch (addressType) { case IPv4: { - host = SocksV5CommonUtils.intToIp(byteBuf.readInt()); + host = Socks5CommonUtils.intToIp(byteBuf.readInt()); port = byteBuf.readUnsignedShort(); - msg = new SocksV5CmdResponse(cmdStatus, addressType, host, port); + msg = new Socks5CmdResponse(cmdStatus, addressType, host, port); break; } case DOMAIN: { fieldLength = byteBuf.readByte(); host = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII); port = byteBuf.readUnsignedShort(); - msg = new SocksV5CmdResponse(cmdStatus, addressType, host, port); + msg = new Socks5CmdResponse(cmdStatus, addressType, host, port); break; } case IPv6: { - host = SocksV5CommonUtils.ipv6toStr(byteBuf.readBytes(16).array()); + host = Socks5CommonUtils.ipv6toStr(byteBuf.readBytes(16).array()); port = byteBuf.readUnsignedShort(); - msg = new SocksV5CmdResponse(cmdStatus, addressType, host, port); + msg = new Socks5CmdResponse(cmdStatus, addressType, host, port); break; } case UNKNOWN: diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdStatus.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdStatus.java similarity index 88% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdStatus.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdStatus.java index 756986f9ca..201e7624bf 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdStatus.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdStatus.java @@ -16,7 +16,7 @@ package io.netty.handler.codec.socksx.v5; -public enum SocksV5CmdStatus { +public enum Socks5CmdStatus { SUCCESS((byte) 0x00), FAILURE((byte) 0x01), FORBIDDEN((byte) 0x02), @@ -30,12 +30,12 @@ public enum SocksV5CmdStatus { private final byte b; - SocksV5CmdStatus(byte b) { + Socks5CmdStatus(byte b) { this.b = b; } - public static SocksV5CmdStatus valueOf(byte b) { - for (SocksV5CmdStatus code : values()) { + public static Socks5CmdStatus valueOf(byte b) { + for (Socks5CmdStatus code : values()) { if (code.b == b) { return code; } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdType.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdType.java similarity index 86% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdType.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdType.java index f1b1ec6c75..e63850a55b 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CmdType.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CmdType.java @@ -16,7 +16,7 @@ package io.netty.handler.codec.socksx.v5; -public enum SocksV5CmdType { +public enum Socks5CmdType { CONNECT((byte) 0x01), BIND((byte) 0x02), UDP((byte) 0x03), @@ -24,12 +24,12 @@ public enum SocksV5CmdType { private final byte b; - SocksV5CmdType(byte b) { + Socks5CmdType(byte b) { this.b = b; } - public static SocksV5CmdType valueOf(byte b) { - for (SocksV5CmdType code : values()) { + public static Socks5CmdType valueOf(byte b) { + for (Socks5CmdType code : values()) { if (code.b == b) { return code; } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CommonUtils.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CommonUtils.java similarity index 97% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CommonUtils.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CommonUtils.java index 25ebd013dc..82e8baf5f7 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5CommonUtils.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5CommonUtils.java @@ -16,7 +16,8 @@ package io.netty.handler.codec.socksx.v5; import io.netty.util.internal.StringUtil; -public final class SocksV5CommonUtils { +final class Socks5CommonUtils { + private static final int SECOND_ADDRESS_OCTET_SHIFT = 16; private static final int FIRST_ADDRESS_OCTET_SHIFT = 24; private static final int THIRD_ADDRESS_OCTET_SHIFT = 8; @@ -25,7 +26,7 @@ public final class SocksV5CommonUtils { /** * A constructor to stop this class being constructed. */ - private SocksV5CommonUtils() { + private Socks5CommonUtils() { // NOOP } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitRequest.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitRequest.java similarity index 66% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitRequest.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitRequest.java index fa73204ab7..1e8adeafb1 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitRequest.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitRequest.java @@ -23,14 +23,14 @@ import java.util.List; /** * An socks init request. * - * @see SocksV5InitResponse - * @see SocksV5InitRequestDecoder + * @see Socks5InitResponse + * @see Socks5InitRequestDecoder */ -public final class SocksV5InitRequest extends SocksV5Request { - private final List authSchemes; +public final class Socks5InitRequest extends Socks5Request { + private final List authSchemes; - public SocksV5InitRequest(List authSchemes) { - super(SocksV5RequestType.INIT); + public Socks5InitRequest(List authSchemes) { + super(Socks5RequestType.INIT); if (authSchemes == null) { throw new NullPointerException("authSchemes"); } @@ -38,19 +38,19 @@ public final class SocksV5InitRequest extends SocksV5Request { } /** - * Returns the List<{@link SocksV5AuthScheme}> of this {@link SocksV5InitRequest} + * Returns the List<{@link Socks5AuthScheme}> of this {@link Socks5InitRequest} * - * @return The List<{@link SocksV5AuthScheme}> of this {@link SocksV5InitRequest} + * @return The List<{@link Socks5AuthScheme}> of this {@link Socks5InitRequest} */ - public List authSchemes() { + public List authSchemes() { return Collections.unmodifiableList(authSchemes); } @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { + void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); byteBuf.writeByte(authSchemes.size()); - for (SocksV5AuthScheme authScheme : authSchemes) { + for (Socks5AuthScheme authScheme : authSchemes) { byteBuf.writeByte(authScheme.byteValue()); } } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitRequestDecoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitRequestDecoder.java similarity index 78% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitRequestDecoder.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitRequestDecoder.java index 511848cc1c..8ed617c19f 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitRequestDecoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitRequestDecoder.java @@ -19,22 +19,22 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.socksx.SocksProtocolVersion; -import io.netty.handler.codec.socksx.v5.SocksV5InitRequestDecoder.State; +import io.netty.handler.codec.socksx.v5.Socks5InitRequestDecoder.State; import java.util.ArrayList; import java.util.List; /** - * Decodes {@link ByteBuf}s into {@link SocksV5InitRequest}. + * Decodes {@link ByteBuf}s into {@link Socks5InitRequest}. * Before returning SocksRequest decoder removes itself from pipeline. */ -public class SocksV5InitRequestDecoder extends ReplayingDecoder { - private final List authSchemes = new ArrayList(); +public class Socks5InitRequestDecoder extends ReplayingDecoder { + private final List authSchemes = new ArrayList(); private SocksProtocolVersion version; private byte authSchemeNum; - private SocksV5Request msg = UnknownSocksV5Request.getInstance(); + private Socks5Request msg = UnknownSocks5Request.INSTANCE; - public SocksV5InitRequestDecoder() { + public Socks5InitRequestDecoder() { super(State.CHECK_PROTOCOL_VERSION); } @@ -52,9 +52,9 @@ public class SocksV5InitRequestDecoder extends ReplayingDecoder { authSchemes.clear(); authSchemeNum = byteBuf.readByte(); for (int i = 0; i < authSchemeNum; i++) { - authSchemes.add(SocksV5AuthScheme.valueOf(byteBuf.readByte())); + authSchemes.add(Socks5AuthScheme.valueOf(byteBuf.readByte())); } - msg = new SocksV5InitRequest(authSchemes); + msg = new Socks5InitRequest(authSchemes); break; } } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitResponse.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitResponse.java similarity index 66% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitResponse.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitResponse.java index 7ceec9ba0f..dd4be05c32 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitResponse.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitResponse.java @@ -20,14 +20,14 @@ import io.netty.buffer.ByteBuf; /** * An socks init response. * - * @see SocksV5InitRequest - * @see SocksV5InitResponseDecoder + * @see Socks5InitRequest + * @see Socks5InitResponseDecoder */ -public final class SocksV5InitResponse extends SocksV5Response { - private final SocksV5AuthScheme authScheme; +public final class Socks5InitResponse extends Socks5Response { + private final Socks5AuthScheme authScheme; - public SocksV5InitResponse(SocksV5AuthScheme authScheme) { - super(SocksV5ResponseType.INIT); + public Socks5InitResponse(Socks5AuthScheme authScheme) { + super(Socks5ResponseType.INIT); if (authScheme == null) { throw new NullPointerException("authScheme"); } @@ -35,16 +35,16 @@ public final class SocksV5InitResponse extends SocksV5Response { } /** - * Returns the {@link SocksV5AuthScheme} of this {@link SocksV5InitResponse} + * Returns the {@link Socks5AuthScheme} of this {@link Socks5InitResponse} * - * @return The {@link SocksV5AuthScheme} of this {@link SocksV5InitResponse} + * @return The {@link Socks5AuthScheme} of this {@link Socks5InitResponse} */ - public SocksV5AuthScheme authScheme() { + public Socks5AuthScheme authScheme() { return authScheme; } @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { + void encodeAsByteBuf(ByteBuf byteBuf) { byteBuf.writeByte(protocolVersion().byteValue()); byteBuf.writeByte(authScheme.byteValue()); } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitResponseDecoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitResponseDecoder.java similarity index 77% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitResponseDecoder.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitResponseDecoder.java index d2b23fc03b..bf4cbeff4a 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5InitResponseDecoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5InitResponseDecoder.java @@ -19,21 +19,21 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.socksx.SocksProtocolVersion; -import io.netty.handler.codec.socksx.v5.SocksV5InitResponseDecoder.State; +import io.netty.handler.codec.socksx.v5.Socks5InitResponseDecoder.State; import java.util.List; /** - * Decodes {@link ByteBuf}s into {@link SocksV5InitResponse}. + * Decodes {@link ByteBuf}s into {@link Socks5InitResponse}. * Before returning SocksResponse decoder removes itself from pipeline. */ -public class SocksV5InitResponseDecoder extends ReplayingDecoder { +public class Socks5InitResponseDecoder extends ReplayingDecoder { private SocksProtocolVersion version; - private SocksV5AuthScheme authScheme; + private Socks5AuthScheme authScheme; - private SocksV5Response msg = UnknownSocksV5Response.getInstance(); + private Socks5Response msg = UnknownSocks5Response.INSTANCE; - public SocksV5InitResponseDecoder() { + public Socks5InitResponseDecoder() { super(State.CHECK_PROTOCOL_VERSION); } @@ -48,8 +48,8 @@ public class SocksV5InitResponseDecoder extends ReplayingDecoder { checkpoint(State.READ_PREFFERED_AUTH_TYPE); } case READ_PREFFERED_AUTH_TYPE: { - authScheme = SocksV5AuthScheme.valueOf(byteBuf.readByte()); - msg = new SocksV5InitResponse(authScheme); + authScheme = Socks5AuthScheme.valueOf(byteBuf.readByte()); + msg = new Socks5InitResponse(authScheme); break; } } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksMessageEncoder.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5MessageEncoder.java similarity index 50% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksMessageEncoder.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5MessageEncoder.java index 2708bf3a08..00d02c157f 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/SocksMessageEncoder.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5MessageEncoder.java @@ -13,40 +13,40 @@ * License for the specific language governing permissions and limitations * under the License. */ -package io.netty.handler.codec.socksx; +package io.netty.handler.codec.socksx.v5; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.handler.codec.socksx.SocksMessage; +import io.netty.handler.codec.socksx.SocksProtocolVersion; /** - * Encodes an {@link SocksMessage} into a {@link ByteBuf}. - * {@link MessageToByteEncoder} implementation. - * Use this with {@link SocksV4CmdRequest}, - * {@link SocksV4CmdRequest}, - * {@link SocksV5InitRequest}, - * {@link SocksV5InitResponse}, - * {@link SocksV5AuthRequest}, - * {@link SocksV5AuthResponse}, - * {@link SocksV5CmdRequest} and - * {@link SocksV5CmdResponse} + * Encodes a {@link Socks5Request} and {@link Socks5Response} into a {@link ByteBuf}. */ @ChannelHandler.Sharable -public class SocksMessageEncoder extends MessageToByteEncoder { - private static final String name = "SOCKS_MESSAGE_ENCODER"; +public final class Socks5MessageEncoder extends MessageToByteEncoder { + + public static final Socks5MessageEncoder INSTANCE = new Socks5MessageEncoder(); + + private Socks5MessageEncoder() { } + + @Override + public boolean acceptOutboundMessage(Object msg) throws Exception { + return super.acceptOutboundMessage(msg) && + ((SocksMessage) msg).protocolVersion() == SocksProtocolVersion.SOCKS5; + } @Override - @SuppressWarnings("deprecation") protected void encode(ChannelHandlerContext ctx, SocksMessage msg, ByteBuf out) throws Exception { - msg.encodeAsByteBuf(out); - } - - private static class SocksMessageEncoderHolder { - public static final SocksMessageEncoder HOLDER_INSTANCE = new SocksMessageEncoder(); - } - - public static SocksMessageEncoder getInstance() { - return SocksMessageEncoderHolder.HOLDER_INSTANCE; + if (msg instanceof Socks5Response) { + ((Socks5Response) msg).encodeAsByteBuf(out); + } else if (msg instanceof Socks5Request) { + ((Socks5Request) msg).encodeAsByteBuf(out); + } else { + // Should not reach here. + throw new Error(); + } } } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5Request.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5Request.java similarity index 61% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5Request.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5Request.java index 9a8897c5ae..d416a05895 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5Request.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5Request.java @@ -15,27 +15,27 @@ */ package io.netty.handler.codec.socksx.v5; -import io.netty.handler.codec.socksx.SocksMessage; -import io.netty.handler.codec.socksx.SocksMessageType; +import io.netty.buffer.ByteBuf; +import io.netty.handler.codec.socks.SocksMessage; import io.netty.handler.codec.socksx.SocksProtocolVersion; import io.netty.handler.codec.socksx.SocksRequest; /** * An abstract class that defines a SocksRequest, providing common properties for - * {@link SocksV5InitRequest}, - * {@link SocksV5AuthRequest}, - * {@link SocksV5CmdRequest} and - * {@link UnknownSocksV5Request}. + * {@link Socks5InitRequest}, + * {@link Socks5AuthRequest}, + * {@link Socks5CmdRequest} and + * {@link UnknownSocks5Request}. * - * @see SocksV5InitRequest - * @see SocksV5AuthRequest - * @see SocksV5CmdRequest - * @see UnknownSocksV5Request + * @see Socks5InitRequest + * @see Socks5AuthRequest + * @see Socks5CmdRequest + * @see UnknownSocks5Request */ -public abstract class SocksV5Request extends SocksRequest { - private final SocksV5RequestType requestType; +public abstract class Socks5Request extends SocksRequest { + private final Socks5RequestType requestType; - protected SocksV5Request(SocksV5RequestType requestType) { + protected Socks5Request(Socks5RequestType requestType) { super(SocksProtocolVersion.SOCKS5); if (requestType == null) { throw new NullPointerException("requestType"); @@ -48,7 +48,13 @@ public abstract class SocksV5Request extends SocksRequest { * * @return socks request type */ - public SocksV5RequestType requestType() { + public Socks5RequestType requestType() { return requestType; } + + /** + * We could have defined this method in {@link SocksMessage} as a protected method, but we did not, + * because we do not want to expose this method to users. + */ + abstract void encodeAsByteBuf(ByteBuf byteBuf); } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5RequestType.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5RequestType.java similarity index 95% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5RequestType.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5RequestType.java index ba1c1541df..28194ee682 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5RequestType.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5RequestType.java @@ -19,7 +19,7 @@ package io.netty.handler.codec.socksx.v5; /** * Type of socks request */ -public enum SocksV5RequestType { +public enum Socks5RequestType { INIT, AUTH, CMD, diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5Response.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5Response.java similarity index 60% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5Response.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5Response.java index 37cbb1fe14..4f570f2dfa 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5Response.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5Response.java @@ -15,27 +15,27 @@ */ package io.netty.handler.codec.socksx.v5; -import io.netty.handler.codec.socksx.SocksMessage; -import io.netty.handler.codec.socksx.SocksMessageType; +import io.netty.buffer.ByteBuf; +import io.netty.handler.codec.socks.SocksMessage; import io.netty.handler.codec.socksx.SocksProtocolVersion; import io.netty.handler.codec.socksx.SocksResponse; /** * An abstract class that defines a SocksResponse, providing common properties for - * {@link SocksV5InitResponse}, - * {@link SocksV5AuthResponse}, - * {@link SocksV5CmdResponse} - * and {@link UnknownSocksV5Response}. + * {@link Socks5InitResponse}, + * {@link Socks5AuthResponse}, + * {@link Socks5CmdResponse} + * and {@link UnknownSocks5Response}. * - * @see SocksV5InitResponse - * @see SocksV5AuthResponse - * @see SocksV5CmdResponse - * @see UnknownSocksV5Response + * @see Socks5InitResponse + * @see Socks5AuthResponse + * @see Socks5CmdResponse + * @see UnknownSocks5Response */ -public abstract class SocksV5Response extends SocksResponse { - private final SocksV5ResponseType responseType; +public abstract class Socks5Response extends SocksResponse { + private final Socks5ResponseType responseType; - protected SocksV5Response(SocksV5ResponseType responseType) { + protected Socks5Response(Socks5ResponseType responseType) { super(SocksProtocolVersion.SOCKS5); if (responseType == null) { throw new NullPointerException("responseType"); @@ -48,7 +48,13 @@ public abstract class SocksV5Response extends SocksResponse { * * @return socks response type */ - public SocksV5ResponseType responseType() { + public Socks5ResponseType responseType() { return responseType; } + + /** + * We could have defined this method in {@link SocksMessage} as a protected method, but we did not, + * because we do not want to expose this method to users. + */ + abstract void encodeAsByteBuf(ByteBuf byteBuf); } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5ResponseType.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5ResponseType.java similarity index 95% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5ResponseType.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5ResponseType.java index c181c9e31c..17bd2ffb37 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5ResponseType.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5ResponseType.java @@ -19,7 +19,7 @@ package io.netty.handler.codec.socksx.v5; /** * Type of socks response */ -public enum SocksV5ResponseType { +public enum Socks5ResponseType { INIT, AUTH, CMD, diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5SubnegotiationVersion.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5SubnegotiationVersion.java similarity index 81% rename from codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5SubnegotiationVersion.java rename to codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5SubnegotiationVersion.java index d3d1609d93..e37150e3bf 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/SocksV5SubnegotiationVersion.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/Socks5SubnegotiationVersion.java @@ -16,18 +16,18 @@ package io.netty.handler.codec.socksx.v5; -public enum SocksV5SubnegotiationVersion { +public enum Socks5SubnegotiationVersion { AUTH_PASSWORD((byte) 0x01), UNKNOWN((byte) 0xff); private final byte b; - SocksV5SubnegotiationVersion(byte b) { + Socks5SubnegotiationVersion(byte b) { this.b = b; } - public static SocksV5SubnegotiationVersion valueOf(byte b) { - for (SocksV5SubnegotiationVersion code : values()) { + public static Socks5SubnegotiationVersion valueOf(byte b) { + for (Socks5SubnegotiationVersion code : values()) { if (code.b == b) { return code; } diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocks5Request.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocks5Request.java new file mode 100755 index 0000000000..b3e3bb8770 --- /dev/null +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocks5Request.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.socksx.v5; + +import io.netty.buffer.ByteBuf; + +/** + * An unknown socks request. + * + * @see Socks5InitRequestDecoder + * @see Socks5AuthRequestDecoder + * @see Socks5CmdRequestDecoder + */ +public final class UnknownSocks5Request extends Socks5Request { + + public static final UnknownSocks5Request INSTANCE = new UnknownSocks5Request(); + + private UnknownSocks5Request() { + super(Socks5RequestType.UNKNOWN); + } + + @Override + void encodeAsByteBuf(ByteBuf byteBuf) { + // NOOP + } +} diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocks5Response.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocks5Response.java new file mode 100755 index 0000000000..2bdbdae188 --- /dev/null +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocks5Response.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.socksx.v5; + +import io.netty.buffer.ByteBuf; + +/** + * An unknown socks response. + * + * @see Socks5InitResponseDecoder + * @see Socks5AuthResponseDecoder + * @see Socks5CmdResponseDecoder + */ +public final class UnknownSocks5Response extends Socks5Response { + + public static final UnknownSocks5Response INSTANCE = new UnknownSocks5Response(); + + private UnknownSocks5Response() { + super(Socks5ResponseType.UNKNOWN); + } + + @Override + void encodeAsByteBuf(ByteBuf byteBuf) { + // NOOP + } +} diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocksV5Request.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocksV5Request.java deleted file mode 100755 index 28ab600cc0..0000000000 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocksV5Request.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.handler.codec.socksx.v5; - -import io.netty.buffer.ByteBuf; - -/** - * An unknown socks request. - * - * @see SocksV5InitRequestDecoder - * @see SocksV5AuthRequestDecoder - * @see SocksV5CmdRequestDecoder - */ -public final class UnknownSocksV5Request extends SocksV5Request { - - public UnknownSocksV5Request() { - super(SocksV5RequestType.UNKNOWN); - } - - @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { - // NOOP - } - - private static class UnknownSocksV5RequestHolder { - public static final UnknownSocksV5Request HOLDER_INSTANCE = new UnknownSocksV5Request(); - } - - public static UnknownSocksV5Request getInstance() { - return UnknownSocksV5RequestHolder.HOLDER_INSTANCE; - } -} diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocksV5Response.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocksV5Response.java deleted file mode 100755 index 4ce2d560ce..0000000000 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/UnknownSocksV5Response.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2012 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.handler.codec.socksx.v5; - -import io.netty.buffer.ByteBuf; - -/** - * An unknown socks response. - * - * @see SocksV5InitResponseDecoder - * @see SocksV5AuthResponseDecoder - * @see SocksV5CmdResponseDecoder - */ -public final class UnknownSocksV5Response extends SocksV5Response { - - public UnknownSocksV5Response() { - super(SocksV5ResponseType.UNKNOWN); - } - - @Override - public void encodeAsByteBuf(ByteBuf byteBuf) { - // NOOP - } - - private static class UnknownSocksV5ResponseHolder { - public static final UnknownSocksV5Response HOLDER_INSTANCE = new UnknownSocksV5Response(); - } - - public static UnknownSocksV5Response getInstance() { - return UnknownSocksV5ResponseHolder.HOLDER_INSTANCE; - } -} diff --git a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/package-info.java b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/package-info.java index 592c44cb39..558cdc36a3 100755 --- a/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/package-info.java +++ b/codec-socks/src/main/java/io/netty/handler/codec/socksx/v5/package-info.java @@ -15,7 +15,6 @@ */ /** - * Encoder, decoder and their related message types for Socks. + * Encoder, decoder and their related message types for SOCKSv5 protocol. */ package io.netty.handler.codec.socksx.v5; -// TODO: Combine decoders into one. diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequestDecoderTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequestDecoderTest.java similarity index 70% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequestDecoderTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequestDecoderTest.java index 710a34d322..cde2101f18 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequestDecoderTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequestDecoderTest.java @@ -16,35 +16,27 @@ package io.netty.handler.codec.socksx.v4; import io.netty.channel.embedded.EmbeddedChannel; -import io.netty.handler.codec.socksx.v5.SocksV5AddressType; -import io.netty.handler.codec.socksx.v5.SocksV5CmdRequest; -import io.netty.handler.codec.socksx.v5.SocksV5CmdType; -import io.netty.handler.codec.socksx.v5.UnknownSocksV5Request; -import io.netty.util.internal.SystemPropertyUtil; import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLoggerFactory; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -public class SocksV4CmdRequestDecoderTest { - private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksV4CmdRequestDecoderTest.class); +public class Socks4CmdRequestDecoderTest { + private static final InternalLogger logger = InternalLoggerFactory.getInstance(Socks4CmdRequestDecoderTest.class); private static void testSocksV4CmdRequestDecoderWithDifferentParams(String userId, - SocksV4CmdType cmdType, + Socks4CmdType cmdType, String host, int port) { logger.debug("Testing cmdType: " + cmdType + " userId: " + userId + " host: " + host + " port: " + port); - SocksV4CmdRequest msg = new SocksV4CmdRequest(userId, cmdType, host, port); - SocksV4CmdRequestDecoder decoder = new SocksV4CmdRequestDecoder(); + Socks4CmdRequest msg = new Socks4CmdRequest(userId, cmdType, host, port); + Socks4CmdRequestDecoder decoder = new Socks4CmdRequestDecoder(); EmbeddedChannel embedder = new EmbeddedChannel(decoder); - SocksV4CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); + Socks4CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); Object obj = embedder.readInbound(); - msg = (SocksV4CmdRequest) obj; + msg = (Socks4CmdRequest) obj; assertSame(msg.cmdType(), cmdType); assertEquals(msg.userId(), userId); assertEquals(msg.host(), host); @@ -57,7 +49,7 @@ public class SocksV4CmdRequestDecoderTest { String[] hosts = {"127.0.0.1", }; String[] userIds = {"test", }; int[] ports = {1, 32769, 65535}; - for (SocksV4CmdType cmdType : SocksV4CmdType.values()) { + for (Socks4CmdType cmdType : Socks4CmdType.values()) { for (String userId : userIds) { for (String host : hosts) { for (int port : ports) { diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequestTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequestTest.java similarity index 92% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequestTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequestTest.java index 79a55804a8..57a60a6f38 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdRequestTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdRequestTest.java @@ -18,6 +18,6 @@ package io.netty.handler.codec.socksx.v4; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SocksV4CmdRequestTest { - private static final Logger logger = LoggerFactory.getLogger(SocksV4CmdRequestTest.class); +public class Socks4CmdRequestTest { + private static final Logger logger = LoggerFactory.getLogger(Socks4CmdRequestTest.class); } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponseDecoderTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponseDecoderTest.java similarity index 59% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponseDecoderTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponseDecoderTest.java index 54f40ba936..819feaad50 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponseDecoderTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponseDecoderTest.java @@ -16,33 +16,29 @@ package io.netty.handler.codec.socksx.v4; import io.netty.channel.embedded.EmbeddedChannel; -import io.netty.handler.codec.socksx.v5.SocksV5AddressType; -import io.netty.handler.codec.socksx.v5.SocksV5CmdStatus; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -public class SocksV4CmdResponseDecoderTest { - private static final Logger logger = LoggerFactory.getLogger(SocksV4CmdResponseDecoderTest.class); +public class Socks4CmdResponseDecoderTest { + private static final Logger logger = LoggerFactory.getLogger(Socks4CmdResponseDecoderTest.class); private static void testSocksCmdResponseDecoderWithDifferentParams( - SocksV4CmdStatus cmdStatus, String host, int port) { + Socks4CmdStatus cmdStatus, String host, int port) { logger.debug("Testing cmdStatus: " + cmdStatus); - SocksV4Response msg = new SocksV4CmdResponse(cmdStatus, host, port); - SocksV4CmdResponseDecoder decoder = new SocksV4CmdResponseDecoder(); + Socks4Response msg = new Socks4CmdResponse(cmdStatus, host, port); + Socks4CmdResponseDecoder decoder = new Socks4CmdResponseDecoder(); EmbeddedChannel embedder = new EmbeddedChannel(decoder); - SocksV4CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); + Socks4CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); - msg = (SocksV4Response) embedder.readInbound(); - assertEquals(((SocksV4CmdResponse) msg).cmdStatus(), cmdStatus); + msg = (Socks4Response) embedder.readInbound(); + assertEquals(((Socks4CmdResponse) msg).cmdStatus(), cmdStatus); if (host != null) { - assertEquals(((SocksV4CmdResponse) msg).host(), host); + assertEquals(((Socks4CmdResponse) msg).host(), host); } - assertEquals(((SocksV4CmdResponse) msg).port(), port); + assertEquals(((Socks4CmdResponse) msg).port(), port); assertNull(embedder.readInbound()); } @@ -51,7 +47,7 @@ public class SocksV4CmdResponseDecoderTest { */ @Test public void testSocksCmdResponseDecoder() { - for (SocksV4CmdStatus cmdStatus : SocksV4CmdStatus.values()) { + for (Socks4CmdStatus cmdStatus : Socks4CmdStatus.values()) { testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, null, 0); } } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponseTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponseTest.java similarity index 91% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponseTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponseTest.java index be07247619..fae7839557 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CmdResponseTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CmdResponseTest.java @@ -18,6 +18,6 @@ package io.netty.handler.codec.socksx.v4; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SocksV4CmdResponseTest { - private static final Logger logger = LoggerFactory.getLogger(SocksV4CmdResponseTest.class); +public class Socks4CmdResponseTest { + private static final Logger logger = LoggerFactory.getLogger(Socks4CmdResponseTest.class); } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CommonTestUtils.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CommonTestUtils.java similarity index 77% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CommonTestUtils.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CommonTestUtils.java index bdb8947af0..9d347be4d4 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/SocksV4CommonTestUtils.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v4/Socks4CommonTestUtils.java @@ -18,18 +18,22 @@ package io.netty.handler.codec.socksx.v4; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -import io.netty.handler.codec.socksx.SocksMessage; -final class SocksV4CommonTestUtils { +final class Socks4CommonTestUtils { /** * A constructor to stop this class being constructed. */ - private SocksV4CommonTestUtils() { + private Socks4CommonTestUtils() { //NOOP } - @SuppressWarnings("deprecation") - public static void writeMessageIntoEmbedder(EmbeddedChannel embedder, SocksMessage msg) { + public static void writeMessageIntoEmbedder(EmbeddedChannel embedder, Socks4Request msg) { + ByteBuf buf = Unpooled.buffer(); + msg.encodeAsByteBuf(buf); + embedder.writeInbound(buf); + } + + public static void writeMessageIntoEmbedder(EmbeddedChannel embedder, Socks4Response msg) { ByteBuf buf = Unpooled.buffer(); msg.encodeAsByteBuf(buf); embedder.writeInbound(buf); diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequestDecoderTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequestDecoderTest.java similarity index 77% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequestDecoderTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequestDecoderTest.java index d0f32769a9..06f9d8f5c5 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequestDecoderTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequestDecoderTest.java @@ -20,17 +20,17 @@ import org.junit.Test; import static org.junit.Assert.*; -public class SocksV5AuthRequestDecoderTest { +public class Socks5AuthRequestDecoderTest { @Test public void testAuthRequestDecoder() { String username = "test"; String password = "test"; - SocksV5AuthRequest msg = new SocksV5AuthRequest(username, password); - SocksV5AuthRequestDecoder decoder = new SocksV5AuthRequestDecoder(); + Socks5AuthRequest msg = new Socks5AuthRequest(username, password); + Socks5AuthRequestDecoder decoder = new Socks5AuthRequestDecoder(); EmbeddedChannel embedder = new EmbeddedChannel(decoder); - SocksV5CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); - msg = (SocksV5AuthRequest) embedder.readInbound(); + Socks5CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); + msg = (Socks5AuthRequest) embedder.readInbound(); assertEquals(msg.username(), username); assertEquals(msg.username(), password); assertNull(embedder.readInbound()); diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequestTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequestTest.java similarity index 88% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequestTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequestTest.java index d140805cf7..32e98ec8e2 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthRequestTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthRequestTest.java @@ -18,16 +18,16 @@ package io.netty.handler.codec.socksx.v5; import org.junit.Test; import static org.junit.Assert.assertTrue; -public class SocksV5AuthRequestTest { +public class Socks5AuthRequestTest { @Test public void testConstructorParamsAreNotNull() { try { - new SocksV5AuthRequest(null, ""); + new Socks5AuthRequest(null, ""); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } try { - new SocksV5AuthRequest("", null); + new Socks5AuthRequest("", null); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } @@ -36,12 +36,12 @@ public class SocksV5AuthRequestTest { @Test public void testUsernameOrPasswordIsNotAscii() { try { - new SocksV5AuthRequest("παράδειγμα.δοκιμή", "password"); + new Socks5AuthRequest("παράδειγμα.δοκιμή", "password"); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } try { - new SocksV5AuthRequest("username", "παράδειγμα.δοκιμή"); + new Socks5AuthRequest("username", "παράδειγμα.δοκιμή"); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } @@ -50,7 +50,7 @@ public class SocksV5AuthRequestTest { @Test public void testUsernameOrPasswordLengthIsLessThan255Chars() { try { - new SocksV5AuthRequest( + new Socks5AuthRequest( "passwordpasswordpasswordpasswordpasswordpasswordpassword" + "passwordpasswordpasswordpasswordpasswordpasswordpassword" + "passwordpasswordpasswordpasswordpasswordpasswordpassword" + @@ -64,7 +64,7 @@ public class SocksV5AuthRequestTest { assertTrue(e instanceof IllegalArgumentException); } try { - new SocksV5AuthRequest("password", + new Socks5AuthRequest("password", "passwordpasswordpasswordpasswordpasswordpasswordpassword" + "passwordpasswordpasswordpasswordpasswordpasswordpassword" + "passwordpasswordpasswordpasswordpasswordpasswordpassword" + diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponseDecoderTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponseDecoderTest.java similarity index 74% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponseDecoderTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponseDecoderTest.java index 3ea8f37075..acf3d96cd7 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponseDecoderTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponseDecoderTest.java @@ -22,24 +22,24 @@ import org.junit.Test; import static org.junit.Assert.*; -public class SocksV5AuthResponseDecoderTest { +public class Socks5AuthResponseDecoderTest { private static final InternalLogger logger = InternalLoggerFactory.getInstance( - SocksV5AuthResponseDecoderTest.class); + Socks5AuthResponseDecoderTest.class); - private static void testSocksAuthResponseDecoderWithDifferentParams(SocksV5AuthStatus authStatus) { + private static void testSocksAuthResponseDecoderWithDifferentParams(Socks5AuthStatus authStatus) { logger.debug("Testing SocksAuthResponseDecoder with authStatus: " + authStatus); - SocksV5AuthResponse msg = new SocksV5AuthResponse(authStatus); - SocksV5AuthResponseDecoder decoder = new SocksV5AuthResponseDecoder(); + Socks5AuthResponse msg = new Socks5AuthResponse(authStatus); + Socks5AuthResponseDecoder decoder = new Socks5AuthResponseDecoder(); EmbeddedChannel embedder = new EmbeddedChannel(decoder); - SocksV5CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); - msg = (SocksV5AuthResponse) embedder.readInbound(); + Socks5CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); + msg = (Socks5AuthResponse) embedder.readInbound(); assertSame(msg.authStatus(), authStatus); assertNull(embedder.readInbound()); } @Test public void testSocksCmdResponseDecoder() { - for (SocksV5AuthStatus authStatus: SocksV5AuthStatus.values()) { + for (Socks5AuthStatus authStatus: Socks5AuthStatus.values()) { testSocksAuthResponseDecoderWithDifferentParams(authStatus); } } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponseTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponseTest.java similarity index 91% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponseTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponseTest.java index fb6ee19fac..3c7fe2e8b2 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5AuthResponseTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5AuthResponseTest.java @@ -18,11 +18,11 @@ package io.netty.handler.codec.socksx.v5; import org.junit.Test; import static org.junit.Assert.assertTrue; -public class SocksV5AuthResponseTest { +public class Socks5AuthResponseTest { @Test public void testConstructorParamsAreNotNull() { try { - new SocksV5AuthResponse(null); + new Socks5AuthResponse(null); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequestDecoderTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequestDecoderTest.java similarity index 77% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequestDecoderTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequestDecoderTest.java index d8494a459a..297380e2fd 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequestDecoderTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequestDecoderTest.java @@ -23,23 +23,23 @@ import sun.net.util.IPAddressUtil; import static org.junit.Assert.*; -public class SocksV5CmdRequestDecoderTest { - private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksV5CmdRequestDecoderTest.class); +public class Socks5CmdRequestDecoderTest { + private static final InternalLogger logger = InternalLoggerFactory.getInstance(Socks5CmdRequestDecoderTest.class); - private static void testSocksCmdRequestDecoderWithDifferentParams(SocksV5CmdType cmdType, - SocksV5AddressType addressType, + private static void testSocksCmdRequestDecoderWithDifferentParams(Socks5CmdType cmdType, + Socks5AddressType addressType, String host, int port) { logger.debug("Testing cmdType: " + cmdType + " addressType: " + addressType + " host: " + host + " port: " + port); - SocksV5CmdRequest msg = new SocksV5CmdRequest(cmdType, addressType, host, port); - SocksV5CmdRequestDecoder decoder = new SocksV5CmdRequestDecoder(); + Socks5CmdRequest msg = new Socks5CmdRequest(cmdType, addressType, host, port); + Socks5CmdRequestDecoder decoder = new Socks5CmdRequestDecoder(); EmbeddedChannel embedder = new EmbeddedChannel(decoder); - SocksV5CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); - if (msg.addressType() == SocksV5AddressType.UNKNOWN) { - assertTrue(embedder.readInbound() instanceof UnknownSocksV5Request); + Socks5CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); + if (msg.addressType() == Socks5AddressType.UNKNOWN) { + assertTrue(embedder.readInbound() instanceof UnknownSocks5Request); } else { - msg = (SocksV5CmdRequest) embedder.readInbound(); + msg = (Socks5CmdRequest) embedder.readInbound(); assertSame(msg.cmdType(), cmdType); assertSame(msg.addressType(), addressType); assertEquals(msg.host(), host); @@ -52,10 +52,10 @@ public class SocksV5CmdRequestDecoderTest { public void testCmdRequestDecoderIPv4() { String[] hosts = {"127.0.0.1", }; int[] ports = {1, 32769, 65535 }; - for (SocksV5CmdType cmdType : SocksV5CmdType.values()) { + for (Socks5CmdType cmdType : Socks5CmdType.values()) { for (String host : hosts) { for (int port : ports) { - testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksV5AddressType.IPv4, host, port); + testSocksCmdRequestDecoderWithDifferentParams(cmdType, Socks5AddressType.IPv4, host, port); } } } @@ -63,12 +63,12 @@ public class SocksV5CmdRequestDecoderTest { @Test public void testCmdRequestDecoderIPv6() { - String[] hosts = {SocksV5CommonUtils.ipv6toStr(IPAddressUtil.textToNumericFormatV6("::1"))}; + String[] hosts = { Socks5CommonUtils.ipv6toStr(IPAddressUtil.textToNumericFormatV6("::1"))}; int[] ports = {1, 32769, 65535}; - for (SocksV5CmdType cmdType : SocksV5CmdType.values()) { + for (Socks5CmdType cmdType : Socks5CmdType.values()) { for (String host : hosts) { for (int port : ports) { - testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksV5AddressType.IPv6, host, port); + testSocksCmdRequestDecoderWithDifferentParams(cmdType, Socks5AddressType.IPv6, host, port); } } } @@ -89,10 +89,10 @@ public class SocksV5CmdRequestDecoderTest { "실례.테스트", "உதாரணம்.பரிட்சை"}; int[] ports = {1, 32769, 65535}; - for (SocksV5CmdType cmdType : SocksV5CmdType.values()) { + for (Socks5CmdType cmdType : Socks5CmdType.values()) { for (String host : hosts) { for (int port : ports) { - testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksV5AddressType.DOMAIN, host, port); + testSocksCmdRequestDecoderWithDifferentParams(cmdType, Socks5AddressType.DOMAIN, host, port); } } } @@ -102,8 +102,8 @@ public class SocksV5CmdRequestDecoderTest { public void testCmdRequestDecoderUnknown() { String host = "google.com"; int port = 80; - for (SocksV5CmdType cmdType : SocksV5CmdType.values()) { - testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksV5AddressType.UNKNOWN, host, port); + for (Socks5CmdType cmdType : Socks5CmdType.values()) { + testSocksCmdRequestDecoderWithDifferentParams(cmdType, Socks5AddressType.UNKNOWN, host, port); } } } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequestTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequestTest.java similarity index 79% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequestTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequestTest.java index ccd9eee32e..2869458fa9 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdRequestTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdRequestTest.java @@ -19,23 +19,23 @@ import org.junit.Test; import static org.junit.Assert.*; -public class SocksV5CmdRequestTest { +public class Socks5CmdRequestTest { @Test public void testConstructorParamsAreNotNull() { try { - new SocksV5CmdRequest(null, SocksV5AddressType.UNKNOWN, "", 1); + new Socks5CmdRequest(null, Socks5AddressType.UNKNOWN, "", 1); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } try { - new SocksV5CmdRequest(SocksV5CmdType.UNKNOWN, null, "", 1); + new Socks5CmdRequest(Socks5CmdType.UNKNOWN, null, "", 1); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } try { - new SocksV5CmdRequest(SocksV5CmdType.UNKNOWN, SocksV5AddressType.UNKNOWN, null, 1); + new Socks5CmdRequest(Socks5CmdType.UNKNOWN, Socks5AddressType.UNKNOWN, null, 1); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } @@ -44,7 +44,7 @@ public class SocksV5CmdRequestTest { @Test public void testIPv4CorrectAddress() { try { - new SocksV5CmdRequest(SocksV5CmdType.BIND, SocksV5AddressType.IPv4, "54.54.1111.253", 1); + new Socks5CmdRequest(Socks5CmdType.BIND, Socks5AddressType.IPv4, "54.54.1111.253", 1); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } @@ -53,7 +53,7 @@ public class SocksV5CmdRequestTest { @Test public void testIPv6CorrectAddress() { try { - new SocksV5CmdRequest(SocksV5CmdType.BIND, SocksV5AddressType.IPv6, "xxx:xxx:xxx", 1); + new Socks5CmdRequest(Socks5CmdType.BIND, Socks5AddressType.IPv6, "xxx:xxx:xxx", 1); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } @@ -62,7 +62,7 @@ public class SocksV5CmdRequestTest { @Test public void testIDNNotExceeds255CharsLimit() { try { - new SocksV5CmdRequest(SocksV5CmdType.BIND, SocksV5AddressType.DOMAIN, + new Socks5CmdRequest(Socks5CmdType.BIND, Socks5AddressType.DOMAIN, "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" + "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" + "παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" + @@ -75,14 +75,14 @@ public class SocksV5CmdRequestTest { @Test public void testValidPortRange() { try { - new SocksV5CmdRequest(SocksV5CmdType.BIND, SocksV5AddressType.DOMAIN, + new Socks5CmdRequest(Socks5CmdType.BIND, Socks5AddressType.DOMAIN, "παράδειγμα.δοκιμήπαράδει", 0); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } try { - new SocksV5CmdRequest(SocksV5CmdType.BIND, SocksV5AddressType.DOMAIN, + new Socks5CmdRequest(Socks5CmdType.BIND, Socks5AddressType.DOMAIN, "παράδειγμα.δοκιμήπαράδει", 65536); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponseDecoderTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponseDecoderTest.java similarity index 66% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponseDecoderTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponseDecoderTest.java index d991e1cedc..74c315e5c1 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponseDecoderTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponseDecoderTest.java @@ -22,25 +22,25 @@ import org.junit.Test; import static org.junit.Assert.*; -public class SocksV5CmdResponseDecoderTest { - private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksV5CmdResponseDecoderTest.class); +public class Socks5CmdResponseDecoderTest { + private static final InternalLogger logger = InternalLoggerFactory.getInstance(Socks5CmdResponseDecoderTest.class); private static void testSocksCmdResponseDecoderWithDifferentParams( - SocksV5CmdStatus cmdStatus, SocksV5AddressType addressType, String host, int port) { + Socks5CmdStatus cmdStatus, Socks5AddressType addressType, String host, int port) { logger.debug("Testing cmdStatus: " + cmdStatus + " addressType: " + addressType); - SocksV5Response msg = new SocksV5CmdResponse(cmdStatus, addressType, host, port); - SocksV5CmdResponseDecoder decoder = new SocksV5CmdResponseDecoder(); + Socks5Response msg = new Socks5CmdResponse(cmdStatus, addressType, host, port); + Socks5CmdResponseDecoder decoder = new Socks5CmdResponseDecoder(); EmbeddedChannel embedder = new EmbeddedChannel(decoder); - SocksV5CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); - if (addressType == SocksV5AddressType.UNKNOWN) { - assertTrue(embedder.readInbound() instanceof UnknownSocksV5Response); + Socks5CommonTestUtils.writeMessageIntoEmbedder(embedder, msg); + if (addressType == Socks5AddressType.UNKNOWN) { + assertTrue(embedder.readInbound() instanceof UnknownSocks5Response); } else { - msg = (SocksV5Response) embedder.readInbound(); - assertEquals(((SocksV5CmdResponse) msg).cmdStatus(), cmdStatus); + msg = (Socks5Response) embedder.readInbound(); + assertEquals(((Socks5CmdResponse) msg).cmdStatus(), cmdStatus); if (host != null) { - assertEquals(((SocksV5CmdResponse) msg).host(), host); + assertEquals(((Socks5CmdResponse) msg).host(), host); } - assertEquals(((SocksV5CmdResponse) msg).port(), port); + assertEquals(((Socks5CmdResponse) msg).port(), port); } assertNull(embedder.readInbound()); } @@ -50,8 +50,8 @@ public class SocksV5CmdResponseDecoderTest { */ @Test public void testSocksCmdResponseDecoder() { - for (SocksV5CmdStatus cmdStatus : SocksV5CmdStatus.values()) { - for (SocksV5AddressType addressType : SocksV5AddressType.values()) { + for (Socks5CmdStatus cmdStatus : Socks5CmdStatus.values()) { + for (Socks5AddressType addressType : Socks5AddressType.values()) { testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, addressType, null, 0); } } @@ -62,7 +62,7 @@ public class SocksV5CmdResponseDecoderTest { */ @Test(expected = IllegalArgumentException.class) public void testInvalidAddress() { - testSocksCmdResponseDecoderWithDifferentParams(SocksV5CmdStatus.SUCCESS, SocksV5AddressType.IPv4, "1", 80); + testSocksCmdResponseDecoderWithDifferentParams(Socks5CmdStatus.SUCCESS, Socks5AddressType.IPv4, "1", 80); } /** @@ -70,14 +70,14 @@ public class SocksV5CmdResponseDecoderTest { */ @Test public void testSocksCmdResponseDecoderIncludingHost() { - for (SocksV5CmdStatus cmdStatus : SocksV5CmdStatus.values()) { - testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, SocksV5AddressType.IPv4, + for (Socks5CmdStatus cmdStatus : Socks5CmdStatus.values()) { + testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, Socks5AddressType.IPv4, "127.0.0.1", 80); - testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, SocksV5AddressType.DOMAIN, + testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, Socks5AddressType.DOMAIN, "testDomain.com", 80); - testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, SocksV5AddressType.IPv6, + testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, Socks5AddressType.IPv6, "2001:db8:85a3:42:1000:8a2e:370:7334", 80); - testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, SocksV5AddressType.IPv6, + testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, Socks5AddressType.IPv6, "1111:111:11:1:0:0:0:1", 80); } } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponseTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponseTest.java similarity index 71% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponseTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponseTest.java index 40699f2ab3..210f9c8008 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CmdResponseTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CmdResponseTest.java @@ -21,16 +21,16 @@ import org.junit.Test; import static org.junit.Assert.*; -public class SocksV5CmdResponseTest { +public class Socks5CmdResponseTest { @Test public void testConstructorParamsAreNotNull() { try { - new SocksV5CmdResponse(null, SocksV5AddressType.UNKNOWN); + new Socks5CmdResponse(null, Socks5AddressType.UNKNOWN); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } try { - new SocksV5CmdResponse(SocksV5CmdStatus.UNASSIGNED, null); + new Socks5CmdResponse(Socks5CmdStatus.UNASSIGNED, null); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } @@ -41,12 +41,12 @@ public class SocksV5CmdResponseTest { */ @Test public void testEmptyDomain() { - SocksV5CmdResponse socksV5CmdResponse = new SocksV5CmdResponse( - SocksV5CmdStatus.SUCCESS, SocksV5AddressType.DOMAIN); - assertNull(socksV5CmdResponse.host()); - assertEquals(0, socksV5CmdResponse.port()); + Socks5CmdResponse socks5CmdResponse = new Socks5CmdResponse( + Socks5CmdStatus.SUCCESS, Socks5AddressType.DOMAIN); + assertNull(socks5CmdResponse.host()); + assertEquals(0, socks5CmdResponse.port()); ByteBuf buffer = Unpooled.buffer(20); - socksV5CmdResponse.encodeAsByteBuf(buffer); + socks5CmdResponse.encodeAsByteBuf(buffer); byte[] expected = { 0x05, // version 0x00, // success reply @@ -65,12 +65,12 @@ public class SocksV5CmdResponseTest { */ @Test public void testIPv4Host() { - SocksV5CmdResponse socksV5CmdResponse = new SocksV5CmdResponse( - SocksV5CmdStatus.SUCCESS, SocksV5AddressType.IPv4, "127.0.0.1", 80); - assertEquals("127.0.0.1", socksV5CmdResponse.host()); - assertEquals(80, socksV5CmdResponse.port()); + Socks5CmdResponse socks5CmdResponse = new Socks5CmdResponse( + Socks5CmdStatus.SUCCESS, Socks5AddressType.IPv4, "127.0.0.1", 80); + assertEquals("127.0.0.1", socks5CmdResponse.host()); + assertEquals(80, socks5CmdResponse.port()); ByteBuf buffer = Unpooled.buffer(20); - socksV5CmdResponse.encodeAsByteBuf(buffer); + socks5CmdResponse.encodeAsByteBuf(buffer); byte[] expected = { 0x05, // version 0x00, // success reply @@ -91,12 +91,12 @@ public class SocksV5CmdResponseTest { */ @Test public void testEmptyBoundAddress() { - SocksV5CmdResponse socksV5CmdResponse = new SocksV5CmdResponse( - SocksV5CmdStatus.SUCCESS, SocksV5AddressType.DOMAIN, "", 80); - assertEquals("", socksV5CmdResponse.host()); - assertEquals(80, socksV5CmdResponse.port()); + Socks5CmdResponse socks5CmdResponse = new Socks5CmdResponse( + Socks5CmdStatus.SUCCESS, Socks5AddressType.DOMAIN, "", 80); + assertEquals("", socks5CmdResponse.host()); + assertEquals(80, socks5CmdResponse.port()); ByteBuf buffer = Unpooled.buffer(20); - socksV5CmdResponse.encodeAsByteBuf(buffer); + socks5CmdResponse.encodeAsByteBuf(buffer); byte[] expected = { 0x05, // version 0x00, // success reply @@ -114,7 +114,7 @@ public class SocksV5CmdResponseTest { */ @Test(expected = IllegalArgumentException.class) public void testInvalidBoundAddress() { - new SocksV5CmdResponse(SocksV5CmdStatus.SUCCESS, SocksV5AddressType.IPv4, "127.0.0", 1000); + new Socks5CmdResponse(Socks5CmdStatus.SUCCESS, Socks5AddressType.IPv4, "127.0.0", 1000); } private static void assertByteBufEquals(byte[] expected, ByteBuf actual) { @@ -127,13 +127,13 @@ public class SocksV5CmdResponseTest { @Test public void testValidPortRange() { try { - new SocksV5CmdResponse(SocksV5CmdStatus.SUCCESS, SocksV5AddressType.IPv4, "127.0.0", 0); + new Socks5CmdResponse(Socks5CmdStatus.SUCCESS, Socks5AddressType.IPv4, "127.0.0", 0); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } try { - new SocksV5CmdResponse(SocksV5CmdStatus.SUCCESS, SocksV5AddressType.IPv4, "127.0.0", 65536); + new Socks5CmdResponse(Socks5CmdStatus.SUCCESS, Socks5AddressType.IPv4, "127.0.0", 65536); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CommonTestUtils.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CommonTestUtils.java similarity index 77% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CommonTestUtils.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CommonTestUtils.java index c155a09644..be1212148b 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5CommonTestUtils.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5CommonTestUtils.java @@ -18,18 +18,22 @@ package io.netty.handler.codec.socksx.v5; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; -import io.netty.handler.codec.socksx.SocksMessage; -final class SocksV5CommonTestUtils { +final class Socks5CommonTestUtils { /** * A constructor to stop this class being constructed. */ - private SocksV5CommonTestUtils() { + private Socks5CommonTestUtils() { //NOOP } - @SuppressWarnings("deprecation") - public static void writeMessageIntoEmbedder(EmbeddedChannel embedder, SocksMessage msg) { + public static void writeMessageIntoEmbedder(EmbeddedChannel embedder, Socks5Request msg) { + ByteBuf buf = Unpooled.buffer(); + msg.encodeAsByteBuf(buf); + embedder.writeInbound(buf); + } + + public static void writeMessageIntoEmbedder(EmbeddedChannel embedder, Socks5Response msg) { ByteBuf buf = Unpooled.buffer(); msg.encodeAsByteBuf(buf); embedder.writeInbound(buf); diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5InitRequestTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5InitRequestTest.java similarity index 92% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5InitRequestTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5InitRequestTest.java index 3cd989566b..4f4f7212c9 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5InitRequestTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5InitRequestTest.java @@ -18,11 +18,11 @@ package io.netty.handler.codec.socksx.v5; import org.junit.Test; import static org.junit.Assert.assertTrue; -public class SocksV5InitRequestTest { +public class Socks5InitRequestTest { @Test public void testConstructorParamsAreNotNull() { try { - new SocksV5InitRequest(null); + new Socks5InitRequest(null); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } diff --git a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5InitResponseTest.java b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5InitResponseTest.java similarity index 91% rename from codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5InitResponseTest.java rename to codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5InitResponseTest.java index 10840a7395..6846343f5e 100755 --- a/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/SocksV5InitResponseTest.java +++ b/codec-socks/src/test/java/io/netty/handler/codec/socksx/v5/Socks5InitResponseTest.java @@ -18,11 +18,11 @@ package io.netty.handler.codec.socksx.v5; import org.junit.Test; import static org.junit.Assert.assertTrue; -public class SocksV5InitResponseTest { +public class Socks5InitResponseTest { @Test public void testConstructorParamsAreNotNull() { try { - new SocksV5InitResponse(null); + new Socks5InitResponse(null); } catch (Exception e) { assertTrue(e instanceof NullPointerException); } diff --git a/example/src/main/java/io/netty/example/socksproxy/SocksPortUnificationServerHandler.java b/example/src/main/java/io/netty/example/socksproxy/SocksPortUnificationServerHandler.java index cbb5dadd53..b219ec18db 100644 --- a/example/src/main/java/io/netty/example/socksproxy/SocksPortUnificationServerHandler.java +++ b/example/src/main/java/io/netty/example/socksproxy/SocksPortUnificationServerHandler.java @@ -19,10 +19,11 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.ByteToMessageDecoder; -import io.netty.handler.codec.socksx.SocksMessageEncoder; import io.netty.handler.codec.socksx.SocksProtocolVersion; -import io.netty.handler.codec.socksx.v4.SocksV4CmdRequestDecoder; -import io.netty.handler.codec.socksx.v5.SocksV5InitRequestDecoder; +import io.netty.handler.codec.socksx.v4.Socks4CmdRequestDecoder; +import io.netty.handler.codec.socksx.v4.Socks4MessageEncoder; +import io.netty.handler.codec.socksx.v5.Socks5InitRequestDecoder; +import io.netty.handler.codec.socksx.v5.Socks5MessageEncoder; import java.util.List; @@ -35,18 +36,21 @@ public class SocksPortUnificationServerHandler extends ByteToMessageDecoder { in.resetReaderIndex(); switch (version) { case SOCKS4a: - p.addLast(new SocksV4CmdRequestDecoder()); + p.addLast(new Socks4CmdRequestDecoder()); + p.addLast(Socks4MessageEncoder.INSTANCE); + break; case SOCKS5: - p.addLast(new SocksV5InitRequestDecoder()); + p.addLast(new Socks5InitRequestDecoder()); + p.addLast(Socks5MessageEncoder.INSTANCE); + break; case UNKNOWN: in.clear(); ctx.close(); return; } - p.addLast(SocksMessageEncoder.getInstance()); - p.addLast(SocksServerHandler.getInstance()); + p.addLast(SocksServerHandler.INSTANCE); p.remove(this); } } diff --git a/example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java b/example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java index 4d71d6d56c..6dbecda961 100644 --- a/example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java +++ b/example/src/main/java/io/netty/example/socksproxy/SocksServerConnectHandler.java @@ -24,17 +24,13 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOption; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.socks.SocksCmdRequest; -import io.netty.handler.codec.socks.SocksCmdResponse; -import io.netty.handler.codec.socks.SocksCmdStatus; -import io.netty.handler.codec.socksx.SocksMessage; import io.netty.handler.codec.socksx.SocksRequest; -import io.netty.handler.codec.socksx.v4.SocksV4CmdRequest; -import io.netty.handler.codec.socksx.v4.SocksV4CmdResponse; -import io.netty.handler.codec.socksx.v4.SocksV4CmdStatus; -import io.netty.handler.codec.socksx.v5.SocksV5CmdRequest; -import io.netty.handler.codec.socksx.v5.SocksV5CmdResponse; -import io.netty.handler.codec.socksx.v5.SocksV5CmdStatus; +import io.netty.handler.codec.socksx.v4.Socks4CmdRequest; +import io.netty.handler.codec.socksx.v4.Socks4CmdResponse; +import io.netty.handler.codec.socksx.v4.Socks4CmdStatus; +import io.netty.handler.codec.socksx.v5.Socks5CmdRequest; +import io.netty.handler.codec.socksx.v5.Socks5CmdResponse; +import io.netty.handler.codec.socksx.v5.Socks5CmdStatus; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; import io.netty.util.concurrent.Promise; @@ -46,8 +42,8 @@ public final class SocksServerConnectHandler extends SimpleChannelInboundHandler @Override public void channelRead0(final ChannelHandlerContext ctx, final SocksRequest message) throws Exception { - if (message instanceof SocksV4CmdRequest) { - final SocksV4CmdRequest request = (SocksV4CmdRequest) message; + if (message instanceof Socks4CmdRequest) { + final Socks4CmdRequest request = (Socks4CmdRequest) message; Promise promise = ctx.executor().newPromise(); promise.addListener( new GenericFutureListener>() { @@ -55,7 +51,7 @@ public final class SocksServerConnectHandler extends SimpleChannelInboundHandler public void operationComplete(final Future future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { - ctx.channel().writeAndFlush(new SocksV4CmdResponse(SocksV4CmdStatus.SUCCESS)) + ctx.channel().writeAndFlush(new Socks4CmdResponse(Socks4CmdStatus.SUCCESS)) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { @@ -66,7 +62,7 @@ public final class SocksServerConnectHandler extends SimpleChannelInboundHandler }); } else { ctx.channel().writeAndFlush( - new SocksV4CmdResponse(SocksV4CmdStatus.REJECTED_OR_FAILED) + new Socks4CmdResponse(Socks4CmdStatus.REJECTED_OR_FAILED) ); SocksServerUtils.closeOnFlush(ctx.channel()); } @@ -88,14 +84,14 @@ public final class SocksServerConnectHandler extends SimpleChannelInboundHandler } else { // Close the connection if the connection attempt has failed. ctx.channel().writeAndFlush( - new SocksV4CmdResponse(SocksV4CmdStatus.REJECTED_OR_FAILED) + new Socks4CmdResponse(Socks4CmdStatus.REJECTED_OR_FAILED) ); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); - } else if (message instanceof SocksV5CmdRequest) { - final SocksV5CmdRequest request = (SocksV5CmdRequest) message; + } else if (message instanceof Socks5CmdRequest) { + final Socks5CmdRequest request = (Socks5CmdRequest) message; Promise promise = ctx.executor().newPromise(); promise.addListener( new GenericFutureListener>() { @@ -104,7 +100,7 @@ public final class SocksServerConnectHandler extends SimpleChannelInboundHandler final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ctx.channel().writeAndFlush( - new SocksV5CmdResponse(SocksV5CmdStatus.SUCCESS, request.addressType()) + new Socks5CmdResponse(Socks5CmdStatus.SUCCESS, request.addressType()) ).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { @@ -116,7 +112,7 @@ public final class SocksServerConnectHandler extends SimpleChannelInboundHandler ); } else { ctx.channel().writeAndFlush( - new SocksV5CmdResponse(SocksV5CmdStatus.FAILURE, request.addressType())); + new Socks5CmdResponse(Socks5CmdStatus.FAILURE, request.addressType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } @@ -137,7 +133,7 @@ public final class SocksServerConnectHandler extends SimpleChannelInboundHandler } else { // Close the connection if the connection attempt has failed. ctx.channel().writeAndFlush( - new SocksV5CmdResponse(SocksV5CmdStatus.FAILURE, request.addressType())); + new Socks5CmdResponse(Socks5CmdStatus.FAILURE, request.addressType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } diff --git a/example/src/main/java/io/netty/example/socksproxy/SocksServerHandler.java b/example/src/main/java/io/netty/example/socksproxy/SocksServerHandler.java index fb27abf7d7..d3a6bdec86 100644 --- a/example/src/main/java/io/netty/example/socksproxy/SocksServerHandler.java +++ b/example/src/main/java/io/netty/example/socksproxy/SocksServerHandler.java @@ -19,26 +19,30 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.socksx.SocksRequest; -import io.netty.handler.codec.socksx.v4.SocksV4CmdRequest; -import io.netty.handler.codec.socksx.v4.SocksV4CmdType; -import io.netty.handler.codec.socksx.v5.SocksV5AuthScheme; -import io.netty.handler.codec.socksx.v5.SocksV5CmdRequestDecoder; -import io.netty.handler.codec.socksx.v5.SocksV5InitResponse; -import io.netty.handler.codec.socksx.v5.SocksV5Request; -import io.netty.handler.codec.socksx.v5.SocksV5AuthResponse; -import io.netty.handler.codec.socksx.v5.SocksV5AuthStatus; -import io.netty.handler.codec.socksx.v5.SocksV5CmdRequest; -import io.netty.handler.codec.socksx.v5.SocksV5CmdType; +import io.netty.handler.codec.socksx.v4.Socks4CmdRequest; +import io.netty.handler.codec.socksx.v4.Socks4CmdType; +import io.netty.handler.codec.socksx.v5.Socks5AuthScheme; +import io.netty.handler.codec.socksx.v5.Socks5CmdRequestDecoder; +import io.netty.handler.codec.socksx.v5.Socks5InitResponse; +import io.netty.handler.codec.socksx.v5.Socks5Request; +import io.netty.handler.codec.socksx.v5.Socks5AuthResponse; +import io.netty.handler.codec.socksx.v5.Socks5AuthStatus; +import io.netty.handler.codec.socksx.v5.Socks5CmdRequest; +import io.netty.handler.codec.socksx.v5.Socks5CmdType; @ChannelHandler.Sharable public final class SocksServerHandler extends SimpleChannelInboundHandler { + public static final SocksServerHandler INSTANCE = new SocksServerHandler(); + + private SocksServerHandler() { } + @Override public void channelRead0(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception { switch (socksRequest.protocolVersion()) { case SOCKS4a: - SocksV4CmdRequest socksV4CmdRequest = (SocksV4CmdRequest) socksRequest; - if (socksV4CmdRequest.cmdType() == SocksV4CmdType.CONNECT) { + Socks4CmdRequest socksV4CmdRequest = (Socks4CmdRequest) socksRequest; + if (socksV4CmdRequest.cmdType() == Socks4CmdType.CONNECT) { ctx.pipeline().addLast(new SocksServerConnectHandler()); ctx.pipeline().remove(this); ctx.fireChannelRead(socksRequest); @@ -47,22 +51,22 @@ public final class SocksServerHandler extends SimpleChannelInboundHandler