Skip to content

Commit

Permalink
Overall clean-up on socksx package
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
trustin committed Aug 14, 2014
1 parent 1a05004 commit fc1429c
Show file tree
Hide file tree
Showing 67 changed files with 667 additions and 654 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@
* Encoder, decoder and their related message types for Socks.
*/
package io.netty.handler.codec.socks;
// TODO: Combine decoders into one.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,10 +51,4 @@ public SocksMessageType type() {
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,24 @@
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");
}
Expand All @@ -56,48 +54,48 @@ public SocksV4CmdRequest(String userId, SocksV4CmdType cmdType, String host, int
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,27 @@
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<State> {
private static final String name = "SOCKS_CMD_REQUEST_DECODER";
public class Socks4CmdRequestDecoder extends ReplayingDecoder<State> {

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);
}

Expand All @@ -56,9 +54,9 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o
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: {
Expand All @@ -67,17 +65,17 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o
}
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
/**
* 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);
}

Expand All @@ -44,13 +44,13 @@ public SocksV4CmdResponse(SocksV4CmdStatus cmdStatus) {
* @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");
}
Expand All @@ -68,20 +68,20 @@ public SocksV4CmdResponse(SocksV4CmdStatus cmdStatus, String host, int port) {
}

/**
* 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() {
Expand All @@ -93,10 +93,10 @@ public String host() {
}

/**
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,23 @@
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<State> {
private static final String name = "SOCKS_CMD_RESPONSE_DECODER";
public class Socks4CmdResponseDecoder extends ReplayingDecoder<State> {

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);
}

Expand All @@ -52,13 +48,13 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> o
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);
Expand Down
Loading

0 comments on commit fc1429c

Please sign in to comment.