Remove io.netty.handler.codec.socks

.. because we have deprecated it in 4.1 already.
This commit is contained in:
Trustin Lee 2015-02-10 11:38:21 +09:00
parent e29399ebc4
commit 5f8f7ac9e3
41 changed files with 0 additions and 2505 deletions

View File

@ -1,44 +0,0 @@
/*
* Copyright 2013 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.socks;
public enum SocksAddressType {
IPv4((byte) 0x01),
DOMAIN((byte) 0x03),
IPv6((byte) 0x04),
UNKNOWN((byte) 0xff);
private final byte b;
SocksAddressType(byte b) {
this.b = b;
}
public static SocksAddressType valueOf(byte b) {
for (SocksAddressType code : values()) {
if (code.b == b) {
return code;
}
}
return UNKNOWN;
}
public byte byteValue() {
return b;
}
}

View File

@ -1,83 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import java.nio.charset.CharsetEncoder;
/**
* An socks auth request.
*
* @see SocksAuthResponse
* @see SocksAuthRequestDecoder
*/
public final class SocksAuthRequest extends SocksRequest {
private static final CharsetEncoder asciiEncoder = CharsetUtil.getEncoder(CharsetUtil.US_ASCII);
private static final SocksSubnegotiationVersion SUBNEGOTIATION_VERSION = SocksSubnegotiationVersion.AUTH_PASSWORD;
private final String username;
private final String password;
public SocksAuthRequest(String username, String password) {
super(SocksRequestType.AUTH);
if (username == null) {
throw new NullPointerException("username");
}
if (password == null) {
throw new NullPointerException("username");
}
if (!asciiEncoder.canEncode(username) || !asciiEncoder.canEncode(password)) {
throw new IllegalArgumentException(" username: " + username + " or password: " + password +
" values should be in pure ascii");
}
if (username.length() > 255) {
throw new IllegalArgumentException(username + " exceeds 255 char limit");
}
if (password.length() > 255) {
throw new IllegalArgumentException(password + " exceeds 255 char limit");
}
this.username = username;
this.password = password;
}
/**
* Returns username that needs to be authenticated
*
* @return username that needs to be authenticated
*/
public String username() {
return username;
}
/**
* Returns password that needs to be validated
*
* @return password that needs to be validated
*/
public String password() {
return password;
}
@Override
void encodeAsByteBuf(ByteBuf byteBuf) {
byteBuf.writeByte(SUBNEGOTIATION_VERSION.byteValue());
byteBuf.writeByte(username.length());
byteBuf.writeBytes(username.getBytes(CharsetUtil.US_ASCII));
byteBuf.writeByte(password.length());
byteBuf.writeBytes(password.getBytes(CharsetUtil.US_ASCII));
}
}

View File

@ -1,72 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksAuthRequestDecoder.State;
import io.netty.util.CharsetUtil;
import java.util.List;
/**
* Decodes {@link ByteBuf}s into {@link SocksAuthRequest}.
* Before returning SocksRequest decoder removes itself from pipeline.
*/
public class SocksAuthRequestDecoder extends ReplayingDecoder<State> {
private SocksSubnegotiationVersion version;
private int fieldLength;
private String username;
private String password;
private SocksRequest msg = SocksCommonUtils.UNKNOWN_SOCKS_REQUEST;
public SocksAuthRequestDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksSubnegotiationVersion.valueOf(byteBuf.readByte());
if (version != SocksSubnegotiationVersion.AUTH_PASSWORD) {
break;
}
checkpoint(State.READ_USERNAME);
}
case READ_USERNAME: {
fieldLength = byteBuf.readByte();
username = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
checkpoint(State.READ_PASSWORD);
}
case READ_PASSWORD: {
fieldLength = byteBuf.readByte();
password = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
msg = new SocksAuthRequest(username, password);
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {
CHECK_PROTOCOL_VERSION,
READ_USERNAME,
READ_PASSWORD
}
}

View File

@ -1,52 +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.socks;
import io.netty.buffer.ByteBuf;
/**
* An socks auth response.
*
* @see SocksAuthRequest
* @see SocksAuthResponseDecoder
*/
public final class SocksAuthResponse extends SocksResponse {
private static final SocksSubnegotiationVersion SUBNEGOTIATION_VERSION = SocksSubnegotiationVersion.AUTH_PASSWORD;
private final SocksAuthStatus authStatus;
public SocksAuthResponse(SocksAuthStatus authStatus) {
super(SocksResponseType.AUTH);
if (authStatus == null) {
throw new NullPointerException("authStatus");
}
this.authStatus = authStatus;
}
/**
* Returns the {@link SocksAuthStatus} of this {@link SocksAuthResponse}
*
* @return The {@link SocksAuthStatus} of this {@link SocksAuthResponse}
*/
public SocksAuthStatus authStatus() {
return authStatus;
}
@Override
void encodeAsByteBuf(ByteBuf byteBuf) {
byteBuf.writeByte(SUBNEGOTIATION_VERSION.byteValue());
byteBuf.writeByte(authStatus.byteValue());
}
}

View File

@ -1,63 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksAuthResponseDecoder.State;
import java.util.List;
/**
* Decodes {@link ByteBuf}s into {@link SocksAuthResponse}.
* Before returning SocksResponse decoder removes itself from pipeline.
*/
public class SocksAuthResponseDecoder extends ReplayingDecoder<State> {
private SocksSubnegotiationVersion version;
private SocksAuthStatus authStatus;
private SocksResponse msg = SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE;
public SocksAuthResponseDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
}
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> out)
throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksSubnegotiationVersion.valueOf(byteBuf.readByte());
if (version != SocksSubnegotiationVersion.AUTH_PASSWORD) {
break;
}
checkpoint(State.READ_AUTH_RESPONSE);
}
case READ_AUTH_RESPONSE: {
authStatus = SocksAuthStatus.valueOf(byteBuf.readByte());
msg = new SocksAuthResponse(authStatus);
}
}
channelHandlerContext.pipeline().remove(this);
out.add(msg);
}
enum State {
CHECK_PROTOCOL_VERSION,
READ_AUTH_RESPONSE
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright 2013 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.socks;
public enum SocksAuthScheme {
NO_AUTH((byte) 0x00),
AUTH_GSSAPI((byte) 0x01),
AUTH_PASSWORD((byte) 0x02),
UNKNOWN((byte) 0xff);
private final byte b;
SocksAuthScheme(byte b) {
this.b = b;
}
public static SocksAuthScheme valueOf(byte b) {
for (SocksAuthScheme code : values()) {
if (code.b == b) {
return code;
}
}
return UNKNOWN;
}
public byte byteValue() {
return b;
}
}

View File

@ -1,42 +0,0 @@
/*
* Copyright 2013 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.socks;
public enum SocksAuthStatus {
SUCCESS((byte) 0x00),
FAILURE((byte) 0xff);
private final byte b;
SocksAuthStatus(byte b) {
this.b = b;
}
public static SocksAuthStatus valueOf(byte b) {
for (SocksAuthStatus code : values()) {
if (code.b == b) {
return code;
}
}
return FAILURE;
}
public byte byteValue() {
return b;
}
}

View File

@ -1,138 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import io.netty.util.NetUtil;
import java.net.IDN;
/**
* An socks cmd request.
*
* @see SocksCmdResponse
* @see SocksCmdRequestDecoder
*/
public final class SocksCmdRequest extends SocksRequest {
private final SocksCmdType cmdType;
private final SocksAddressType addressType;
private final String host;
private final int port;
public SocksCmdRequest(SocksCmdType cmdType, SocksAddressType addressType, String host, int port) {
super(SocksRequestType.CMD);
if (cmdType == null) {
throw new NullPointerException("cmdType");
}
if (addressType == null) {
throw new NullPointerException("addressType");
}
if (host == null) {
throw new NullPointerException("host");
}
switch (addressType) {
case IPv4:
if (!NetUtil.isValidIpV4Address(host)) {
throw new IllegalArgumentException(host + " is not a valid IPv4 address");
}
break;
case DOMAIN:
if (IDN.toASCII(host).length() > 255) {
throw new IllegalArgumentException(host + " IDN: " + IDN.toASCII(host) + " exceeds 255 char limit");
}
break;
case IPv6:
if (!NetUtil.isValidIpV6Address(host)) {
throw new IllegalArgumentException(host + " is not a valid IPv6 address");
}
break;
case UNKNOWN:
break;
}
if (port <= 0 || port >= 65536) {
throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536");
}
this.cmdType = cmdType;
this.addressType = addressType;
this.host = IDN.toASCII(host);
this.port = port;
}
/**
* Returns the {@link SocksCmdType} of this {@link SocksCmdRequest}
*
* @return The {@link SocksCmdType} of this {@link SocksCmdRequest}
*/
public SocksCmdType cmdType() {
return cmdType;
}
/**
* Returns the {@link SocksAddressType} of this {@link SocksCmdRequest}
*
* @return The {@link SocksAddressType} of this {@link SocksCmdRequest}
*/
public SocksAddressType addressType() {
return addressType;
}
/**
* Returns host that is used as a parameter in {@link SocksCmdType}
*
* @return host that is used as a parameter in {@link SocksCmdType}
*/
public String host() {
return IDN.toUnicode(host);
}
/**
* Returns port that is used as a parameter in {@link SocksCmdType}
*
* @return port that is used as a parameter in {@link SocksCmdType}
*/
public int port() {
return port;
}
@Override
void encodeAsByteBuf(ByteBuf byteBuf) {
byteBuf.writeByte(protocolVersion().byteValue());
byteBuf.writeByte(cmdType.byteValue());
byteBuf.writeByte(0x00);
byteBuf.writeByte(addressType.byteValue());
switch (addressType) {
case IPv4: {
byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(host));
byteBuf.writeShort(port);
break;
}
case DOMAIN: {
byteBuf.writeByte(host.length());
byteBuf.writeBytes(host.getBytes(CharsetUtil.US_ASCII));
byteBuf.writeShort(port);
break;
}
case IPv6: {
byteBuf.writeBytes(NetUtil.createByteArrayFromIpAddressString(host));
byteBuf.writeShort(port);
break;
}
}
}
}

View File

@ -1,97 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksCmdRequestDecoder.State;
import io.netty.util.CharsetUtil;
import java.util.List;
/**
* Decodes {@link ByteBuf}s into {@link SocksCmdRequest}.
* Before returning SocksRequest decoder removes itself from pipeline.
*/
public class SocksCmdRequestDecoder extends ReplayingDecoder<State> {
private SocksProtocolVersion version;
private int fieldLength;
private SocksCmdType cmdType;
private SocksAddressType addressType;
@SuppressWarnings("UnusedDeclaration")
private byte reserved;
private String host;
private int port;
private SocksRequest msg = SocksCommonUtils.UNKNOWN_SOCKS_REQUEST;
public SocksCmdRequestDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksProtocolVersion.valueOf(byteBuf.readByte());
if (version != SocksProtocolVersion.SOCKS5) {
break;
}
checkpoint(State.READ_CMD_HEADER);
}
case READ_CMD_HEADER: {
cmdType = SocksCmdType.valueOf(byteBuf.readByte());
reserved = byteBuf.readByte();
addressType = SocksAddressType.valueOf(byteBuf.readByte());
checkpoint(State.READ_CMD_ADDRESS);
}
case READ_CMD_ADDRESS: {
switch (addressType) {
case IPv4: {
host = SocksCommonUtils.intToIp(byteBuf.readInt());
port = byteBuf.readUnsignedShort();
msg = new SocksCmdRequest(cmdType, addressType, host, port);
break;
}
case DOMAIN: {
fieldLength = byteBuf.readByte();
host = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
port = byteBuf.readUnsignedShort();
msg = new SocksCmdRequest(cmdType, addressType, host, port);
break;
}
case IPv6: {
host = SocksCommonUtils.ipv6toStr(byteBuf.readBytes(16).array());
port = byteBuf.readUnsignedShort();
msg = new SocksCmdRequest(cmdType, addressType, host, port);
break;
}
case UNKNOWN:
break;
}
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {
CHECK_PROTOCOL_VERSION,
READ_CMD_HEADER,
READ_CMD_ADDRESS
}
}

View File

@ -1,177 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import io.netty.util.NetUtil;
import java.net.IDN;
/**
* A socks cmd response.
*
* @see SocksCmdRequest
* @see SocksCmdResponseDecoder
*/
public final class SocksCmdResponse extends SocksResponse {
private final SocksCmdStatus cmdStatus;
private final SocksAddressType addressType;
private final String host;
private final int port;
// All arrays are initialized on construction time to 0/false/null remove array Initialization
private static final byte[] DOMAIN_ZEROED = {0x00};
private static final byte[] IPv4_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00};
private static final byte[] IPv6_HOSTNAME_ZEROED = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
public SocksCmdResponse(SocksCmdStatus cmdStatus, SocksAddressType addressType) {
this(cmdStatus, addressType, null, 0);
}
/**
* Constructs new response and includes provided host and port as part of it.
*
* @param cmdStatus status of the response
* @param addressType type of host parameter
* @param host host (BND.ADDR field) is address that server used when connecting to the target host.
* When null a value of 4/8 0x00 octets will be used for IPv4/IPv6 and a single 0x00 byte will be
* used for domain addressType. Value is converted to ASCII using {@link IDN#toASCII(String)}.
* @param port port (BND.PORT field) that the server assigned to connect to the target host
* @throws NullPointerException in case cmdStatus or addressType are missing
* @throws IllegalArgumentException in case host or port cannot be validated
* @see IDN#toASCII(String)
*/
public SocksCmdResponse(SocksCmdStatus cmdStatus, SocksAddressType addressType, String host, int port) {
super(SocksResponseType.CMD);
if (cmdStatus == null) {
throw new NullPointerException("cmdStatus");
}
if (addressType == null) {
throw new NullPointerException("addressType");
}
if (host != null) {
switch (addressType) {
case IPv4:
if (!NetUtil.isValidIpV4Address(host)) {
throw new IllegalArgumentException(host + " is not a valid IPv4 address");
}
break;
case DOMAIN:
if (IDN.toASCII(host).length() > 255) {
throw new IllegalArgumentException(host + " IDN: " +
IDN.toASCII(host) + " exceeds 255 char limit");
}
break;
case IPv6:
if (!NetUtil.isValidIpV6Address(host)) {
throw new IllegalArgumentException(host + " is not a valid IPv6 address");
}
break;
case UNKNOWN:
break;
}
host = IDN.toASCII(host);
}
if (port < 0 || port > 65535) {
throw new IllegalArgumentException(port + " is not in bounds 0 <= x <= 65535");
}
this.cmdStatus = cmdStatus;
this.addressType = addressType;
this.host = host;
this.port = port;
}
/**
* Returns the {@link SocksCmdStatus} of this {@link SocksCmdResponse}
*
* @return The {@link SocksCmdStatus} of this {@link SocksCmdResponse}
*/
public SocksCmdStatus cmdStatus() {
return cmdStatus;
}
/**
* Returns the {@link SocksAddressType} of this {@link SocksCmdResponse}
*
* @return The {@link SocksAddressType} of this {@link SocksCmdResponse}
*/
public SocksAddressType addressType() {
return addressType;
}
/**
* Returns host that is used as a parameter in {@link io.netty.handler.codec.socks.SocksCmdType}.
* Host (BND.ADDR field in response) is address that server used when connecting to the target host.
* This is typically different from address which client uses to connect to the SOCKS server.
*
* @return host that is used as a parameter in {@link io.netty.handler.codec.socks.SocksCmdType}
* or null when there was no host specified during response construction
*/
public String host() {
if (host != null) {
return IDN.toUnicode(host);
} else {
return null;
}
}
/**
* Returns port that is used as a parameter in {@link io.netty.handler.codec.socks.SocksCmdType}.
* Port (BND.PORT field in response) is port that the server assigned to connect to the target host.
*
* @return port that is used as a parameter in {@link io.netty.handler.codec.socks.SocksCmdType}
*/
public int port() {
return port;
}
@Override
void encodeAsByteBuf(ByteBuf byteBuf) {
byteBuf.writeByte(protocolVersion().byteValue());
byteBuf.writeByte(cmdStatus.byteValue());
byteBuf.writeByte(0x00);
byteBuf.writeByte(addressType.byteValue());
switch (addressType) {
case IPv4: {
byte[] hostContent = host == null ?
IPv4_HOSTNAME_ZEROED : NetUtil.createByteArrayFromIpAddressString(host);
byteBuf.writeBytes(hostContent);
byteBuf.writeShort(port);
break;
}
case DOMAIN: {
byte[] hostContent = host == null ?
DOMAIN_ZEROED : host.getBytes(CharsetUtil.US_ASCII);
byteBuf.writeByte(hostContent.length); // domain length
byteBuf.writeBytes(hostContent); // domain value
byteBuf.writeShort(port); // port value
break;
}
case IPv6: {
byte[] hostContent = host == null
? IPv6_HOSTNAME_ZEROED : NetUtil.createByteArrayFromIpAddressString(host);
byteBuf.writeBytes(hostContent);
byteBuf.writeShort(port);
break;
}
}
}
}

View File

@ -1,96 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksCmdResponseDecoder.State;
import io.netty.util.CharsetUtil;
import java.util.List;
/**
* Decodes {@link ByteBuf}s into {@link SocksCmdResponse}.
* Before returning SocksResponse decoder removes itself from pipeline.
*/
public class SocksCmdResponseDecoder extends ReplayingDecoder<State> {
private SocksProtocolVersion version;
private int fieldLength;
private SocksCmdStatus cmdStatus;
private SocksAddressType addressType;
private byte reserved;
private String host;
private int port;
private SocksResponse msg = SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE;
public SocksCmdResponseDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksProtocolVersion.valueOf(byteBuf.readByte());
if (version != SocksProtocolVersion.SOCKS5) {
break;
}
checkpoint(State.READ_CMD_HEADER);
}
case READ_CMD_HEADER: {
cmdStatus = SocksCmdStatus.valueOf(byteBuf.readByte());
reserved = byteBuf.readByte();
addressType = SocksAddressType.valueOf(byteBuf.readByte());
checkpoint(State.READ_CMD_ADDRESS);
}
case READ_CMD_ADDRESS: {
switch (addressType) {
case IPv4: {
host = SocksCommonUtils.intToIp(byteBuf.readInt());
port = byteBuf.readUnsignedShort();
msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
break;
}
case DOMAIN: {
fieldLength = byteBuf.readByte();
host = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
port = byteBuf.readUnsignedShort();
msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
break;
}
case IPv6: {
host = SocksCommonUtils.ipv6toStr(byteBuf.readBytes(16).array());
port = byteBuf.readUnsignedShort();
msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
break;
}
case UNKNOWN:
break;
}
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {
CHECK_PROTOCOL_VERSION,
READ_CMD_HEADER,
READ_CMD_ADDRESS
}
}

View File

@ -1,49 +0,0 @@
/*
* Copyright 2013 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.socks;
public enum SocksCmdStatus {
SUCCESS((byte) 0x00),
FAILURE((byte) 0x01),
FORBIDDEN((byte) 0x02),
NETWORK_UNREACHABLE((byte) 0x03),
HOST_UNREACHABLE((byte) 0x04),
REFUSED((byte) 0x05),
TTL_EXPIRED((byte) 0x06),
COMMAND_NOT_SUPPORTED((byte) 0x07),
ADDRESS_NOT_SUPPORTED((byte) 0x08),
UNASSIGNED((byte) 0xff);
private final byte b;
SocksCmdStatus(byte b) {
this.b = b;
}
public static SocksCmdStatus valueOf(byte b) {
for (SocksCmdStatus code : values()) {
if (code.b == b) {
return code;
}
}
return UNASSIGNED;
}
public byte byteValue() {
return b;
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright 2013 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.socks;
public enum SocksCmdType {
CONNECT((byte) 0x01),
BIND((byte) 0x02),
UDP((byte) 0x03),
UNKNOWN((byte) 0xff);
private final byte b;
SocksCmdType(byte b) {
this.b = b;
}
public static SocksCmdType valueOf(byte b) {
for (SocksCmdType code : values()) {
if (code.b == b) {
return code;
}
}
return UNKNOWN;
}
public byte byteValue() {
return b;
}
}

View File

@ -1,106 +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.socks;
import io.netty.util.internal.StringUtil;
final class SocksCommonUtils {
public static final SocksRequest UNKNOWN_SOCKS_REQUEST = new UnknownSocksRequest();
public static final SocksResponse UNKNOWN_SOCKS_RESPONSE = new UnknownSocksResponse();
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;
private static final int XOR_DEFAULT_VALUE = 0xff;
/**
* A constructor to stop this class being constructed.
*/
private SocksCommonUtils() {
// NOOP
}
public static String intToIp(int i) {
return String.valueOf(i >> FIRST_ADDRESS_OCTET_SHIFT & XOR_DEFAULT_VALUE) + '.' +
(i >> SECOND_ADDRESS_OCTET_SHIFT & XOR_DEFAULT_VALUE) + '.' +
(i >> THIRD_ADDRESS_OCTET_SHIFT & XOR_DEFAULT_VALUE) + '.' +
(i & XOR_DEFAULT_VALUE);
}
private static final char[] ipv6conseqZeroFiller = {':', ':'};
private static final char ipv6hextetSeparator = ':';
/**
* Convert numeric IPv6 to compressed format, where
* the longest sequence of 0's (with 2 or more 0's) is replaced with "::"
*/
public static String ipv6toCompressedForm(byte[] src) {
assert src.length == 16;
//Find the longest sequence of 0's
//start of compressed region (hextet index)
int cmprHextet = -1;
//length of compressed region
int cmprSize = 0;
for (int hextet = 0; hextet < 8;) {
int curByte = hextet * 2;
int size = 0;
while (curByte < src.length && src[curByte] == 0
&& src[curByte + 1] == 0) {
curByte += 2;
size++;
}
if (size > cmprSize) {
cmprHextet = hextet;
cmprSize = size;
}
hextet = curByte / 2 + 1;
}
if (cmprHextet == -1 || cmprSize < 2) {
//No compression can be applied
return ipv6toStr(src);
}
StringBuilder sb = new StringBuilder(39);
ipv6toStr(sb, src, 0, cmprHextet);
sb.append(ipv6conseqZeroFiller);
ipv6toStr(sb, src, cmprHextet + cmprSize, 8);
return sb.toString();
}
/**
* Converts numeric IPv6 to standard (non-compressed) format.
*/
public static String ipv6toStr(byte[] src) {
assert src.length == 16;
StringBuilder sb = new StringBuilder(39);
ipv6toStr(sb, src, 0, 8);
return sb.toString();
}
private static void ipv6toStr(StringBuilder sb, byte[] src, int fromHextet, int toHextet) {
int i;
toHextet --;
for (i = fromHextet; i < toHextet; i++) {
appendHextet(sb, src, i);
sb.append(ipv6hextetSeparator);
}
appendHextet(sb, src, i);
}
private static void appendHextet(StringBuilder sb, byte[] src, int i) {
StringUtil.toHexString(sb, src, i << 1, 2);
}
}

View File

@ -1,57 +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.socks;
import io.netty.buffer.ByteBuf;
import java.util.Collections;
import java.util.List;
/**
* An socks init request.
*
* @see SocksInitResponse
* @see SocksInitRequestDecoder
*/
public final class SocksInitRequest extends SocksRequest {
private final List<SocksAuthScheme> authSchemes;
public SocksInitRequest(List<SocksAuthScheme> authSchemes) {
super(SocksRequestType.INIT);
if (authSchemes == null) {
throw new NullPointerException("authSchemes");
}
this.authSchemes = authSchemes;
}
/**
* Returns the List<{@link SocksAuthScheme}> of this {@link SocksInitRequest}
*
* @return The List<{@link SocksAuthScheme}> of this {@link SocksInitRequest}
*/
public List<SocksAuthScheme> authSchemes() {
return Collections.unmodifiableList(authSchemes);
}
@Override
void encodeAsByteBuf(ByteBuf byteBuf) {
byteBuf.writeByte(protocolVersion().byteValue());
byteBuf.writeByte(authSchemes.size());
for (SocksAuthScheme authScheme : authSchemes) {
byteBuf.writeByte(authScheme.byteValue());
}
}
}

View File

@ -1,69 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksInitRequestDecoder.State;
import java.util.ArrayList;
import java.util.List;
/**
* Decodes {@link ByteBuf}s into {@link SocksInitRequest}.
* Before returning SocksRequest decoder removes itself from pipeline.
*/
public class SocksInitRequestDecoder extends ReplayingDecoder<State> {
private final List<SocksAuthScheme> authSchemes = new ArrayList<SocksAuthScheme>();
private SocksProtocolVersion version;
private byte authSchemeNum;
private SocksRequest msg = SocksCommonUtils.UNKNOWN_SOCKS_REQUEST;
public SocksInitRequestDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksProtocolVersion.valueOf(byteBuf.readByte());
if (version != SocksProtocolVersion.SOCKS5) {
break;
}
checkpoint(State.READ_AUTH_SCHEMES);
}
case READ_AUTH_SCHEMES: {
authSchemes.clear();
authSchemeNum = byteBuf.readByte();
for (int i = 0; i < authSchemeNum; i++) {
authSchemes.add(SocksAuthScheme.valueOf(byteBuf.readByte()));
}
msg = new SocksInitRequest(authSchemes);
break;
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {
CHECK_PROTOCOL_VERSION,
READ_AUTH_SCHEMES
}
}

View File

@ -1,51 +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.socks;
import io.netty.buffer.ByteBuf;
/**
* An socks init response.
*
* @see SocksInitRequest
* @see SocksInitResponseDecoder
*/
public final class SocksInitResponse extends SocksResponse {
private final SocksAuthScheme authScheme;
public SocksInitResponse(SocksAuthScheme authScheme) {
super(SocksResponseType.INIT);
if (authScheme == null) {
throw new NullPointerException("authScheme");
}
this.authScheme = authScheme;
}
/**
* Returns the {@link SocksAuthScheme} of this {@link SocksInitResponse}
*
* @return The {@link SocksAuthScheme} of this {@link SocksInitResponse}
*/
public SocksAuthScheme authScheme() {
return authScheme;
}
@Override
void encodeAsByteBuf(ByteBuf byteBuf) {
byteBuf.writeByte(protocolVersion().byteValue());
byteBuf.writeByte(authScheme.byteValue());
}
}

View File

@ -1,64 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksInitResponseDecoder.State;
import java.util.List;
/**
* Decodes {@link ByteBuf}s into {@link SocksInitResponse}.
* Before returning SocksResponse decoder removes itself from pipeline.
*/
public class SocksInitResponseDecoder extends ReplayingDecoder<State> {
private SocksProtocolVersion version;
private SocksAuthScheme authScheme;
private SocksResponse msg = SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE;
public SocksInitResponseDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksProtocolVersion.valueOf(byteBuf.readByte());
if (version != SocksProtocolVersion.SOCKS5) {
break;
}
checkpoint(State.READ_PREFFERED_AUTH_TYPE);
}
case READ_PREFFERED_AUTH_TYPE: {
authScheme = SocksAuthScheme.valueOf(byteBuf.readByte());
msg = new SocksInitResponse(authScheme);
break;
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {
CHECK_PROTOCOL_VERSION,
READ_PREFFERED_AUTH_TYPE
}
}

View File

@ -1,61 +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.socks;
import io.netty.buffer.ByteBuf;
/**
* An abstract class that defines a SocksMessage, providing common properties for
* {@link SocksRequest} and {@link SocksResponse}.
*
* @see SocksRequest
* @see SocksResponse
*/
public abstract class SocksMessage {
private final SocksMessageType type;
private final SocksProtocolVersion protocolVersion = SocksProtocolVersion.SOCKS5;
protected SocksMessage(SocksMessageType type) {
if (type == null) {
throw new NullPointerException("type");
}
this.type = type;
}
/**
* Returns the {@link SocksMessageType} of this {@link SocksMessage}
*
* @return The {@link SocksMessageType} of this {@link SocksMessage}
*/
public SocksMessageType type() {
return type;
}
/**
* Returns the {@link SocksProtocolVersion} of this {@link SocksMessage}
*
* @return The {@link SocksProtocolVersion} of this {@link SocksMessage}
*/
public SocksProtocolVersion protocolVersion() {
return protocolVersion;
}
/**
* Encode socks message into its byte representation and write it into byteBuf
*/
abstract void encodeAsByteBuf(ByteBuf byteBuf);
}

View File

@ -1,35 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
/**
* Encodes an {@link SocksMessage} into a {@link ByteBuf}.
* {@link MessageToByteEncoder} implementation.
* Use this with {@link SocksInitRequest}, {@link SocksInitResponse}, {@link SocksAuthRequest},
* {@link SocksAuthResponse}, {@link SocksCmdRequest} and {@link SocksCmdResponse}
*/
@ChannelHandler.Sharable
public class SocksMessageEncoder extends MessageToByteEncoder<SocksMessage> {
@Override
protected void encode(ChannelHandlerContext ctx, SocksMessage msg, ByteBuf out) throws Exception {
msg.encodeAsByteBuf(out);
}
}

View File

@ -1,23 +0,0 @@
/*
* Copyright 2013 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.socks;
public enum SocksMessageType {
REQUEST,
RESPONSE,
UNKNOWN
}

View File

@ -1,42 +0,0 @@
/*
* Copyright 2013 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.socks;
public enum SocksProtocolVersion {
SOCKS4a((byte) 0x04),
SOCKS5((byte) 0x05),
UNKNOWN((byte) 0xff);
private final byte b;
SocksProtocolVersion(byte b) {
this.b = b;
}
public static SocksProtocolVersion valueOf(byte b) {
for (SocksProtocolVersion code : values()) {
if (code.b == b) {
return code;
}
}
return UNKNOWN;
}
public byte byteValue() {
return b;
}
}

View File

@ -1,46 +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.socks;
/**
* An abstract class that defines a SocksRequest, providing common properties for
* {@link SocksInitRequest}, {@link SocksAuthRequest}, {@link SocksCmdRequest} and {@link UnknownSocksRequest}.
*
* @see SocksInitRequest
* @see SocksAuthRequest
* @see SocksCmdRequest
* @see UnknownSocksRequest
*/
public abstract class SocksRequest extends SocksMessage {
private final SocksRequestType requestType;
protected SocksRequest(SocksRequestType requestType) {
super(SocksMessageType.REQUEST);
if (requestType == null) {
throw new NullPointerException("requestType");
}
this.requestType = requestType;
}
/**
* Returns socks request type
*
* @return socks request type
*/
public SocksRequestType requestType() {
return requestType;
}
}

View File

@ -1,27 +0,0 @@
/*
* Copyright 2013 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.socks;
/**
* Type of socks request
*/
public enum SocksRequestType {
INIT,
AUTH,
CMD,
UNKNOWN
}

View File

@ -1,46 +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.socks;
/**
* An abstract class that defines a SocksResponse, providing common properties for
* {@link SocksInitResponse}, {@link SocksAuthResponse}, {@link SocksCmdResponse} and {@link UnknownSocksResponse}.
*
* @see SocksInitResponse
* @see SocksAuthResponse
* @see SocksCmdResponse
* @see UnknownSocksResponse
*/
public abstract class SocksResponse extends SocksMessage {
private final SocksResponseType responseType;
protected SocksResponse(SocksResponseType responseType) {
super(SocksMessageType.RESPONSE);
if (responseType == null) {
throw new NullPointerException("responseType");
}
this.responseType = responseType;
}
/**
* Returns socks response type
*
* @return socks response type
*/
public SocksResponseType responseType() {
return responseType;
}
}

View File

@ -1,27 +0,0 @@
/*
* Copyright 2013 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.socks;
/**
* Type of socks response
*/
public enum SocksResponseType {
INIT,
AUTH,
CMD,
UNKNOWN
}

View File

@ -1,42 +0,0 @@
/*
* Copyright 2013 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.socks;
public enum SocksSubnegotiationVersion {
AUTH_PASSWORD((byte) 0x01),
UNKNOWN((byte) 0xff);
private final byte b;
SocksSubnegotiationVersion(byte b) {
this.b = b;
}
public static SocksSubnegotiationVersion valueOf(byte b) {
for (SocksSubnegotiationVersion code : values()) {
if (code.b == b) {
return code;
}
}
return UNKNOWN;
}
public byte byteValue() {
return b;
}
}

View File

@ -1,37 +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.socks;
import io.netty.buffer.ByteBuf;
/**
* An unknown socks request.
*
* @see SocksInitRequestDecoder
* @see SocksAuthRequestDecoder
* @see SocksCmdRequestDecoder
*/
public final class UnknownSocksRequest extends SocksRequest {
public UnknownSocksRequest() {
super(SocksRequestType.UNKNOWN);
}
@Override
void encodeAsByteBuf(ByteBuf byteBuf) {
// NOOP
}
}

View File

@ -1,37 +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.socks;
import io.netty.buffer.ByteBuf;
/**
* An unknown socks response.
*
* @see SocksInitResponseDecoder
* @see SocksAuthResponseDecoder
* @see SocksCmdResponseDecoder
*/
public final class UnknownSocksResponse extends SocksResponse {
public UnknownSocksResponse() {
super(SocksResponseType.UNKNOWN);
}
@Override
void encodeAsByteBuf(ByteBuf byteBuf) {
// NOOP
}
}

View File

@ -1,20 +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.
*/
/**
* Encoder, decoder and their related message types for Socks.
*/
package io.netty.handler.codec.socks;

View File

@ -1,38 +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.socks;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test;
import static org.junit.Assert.*;
public class SocksAuthRequestDecoderTest {
@Test
public void testAuthRequestDecoder() {
String username = "test";
String password = "test";
SocksAuthRequest msg = new SocksAuthRequest(username, password);
SocksAuthRequestDecoder decoder = new SocksAuthRequestDecoder();
EmbeddedChannel embedder = new EmbeddedChannel(decoder);
SocksCommonTestUtils.writeMessageIntoEmbedder(embedder, msg);
msg = embedder.readInbound();
assertEquals(msg.username(), username);
assertEquals(msg.username(), password);
assertNull(embedder.readInbound());
}
}

View File

@ -1,81 +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.socks;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class SocksAuthRequestTest {
@Test
public void testConstructorParamsAreNotNull() {
try {
new SocksAuthRequest(null, "");
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
try {
new SocksAuthRequest("", null);
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
}
@Test
public void testUsernameOrPasswordIsNotAscii() {
try {
new SocksAuthRequest("παράδειγμα.δοκιμή", "password");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
try {
new SocksAuthRequest("username", "παράδειγμα.δοκιμή");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
@Test
public void testUsernameOrPasswordLengthIsLessThan255Chars() {
try {
new SocksAuthRequest(
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword",
"password");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
try {
new SocksAuthRequest("password",
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword" +
"passwordpasswordpasswordpasswordpasswordpasswordpassword");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
}

View File

@ -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.socks;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test;
import static org.junit.Assert.*;
public class SocksAuthResponseDecoderTest {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksAuthResponseDecoderTest.class);
private static void testSocksAuthResponseDecoderWithDifferentParams(SocksAuthStatus authStatus) {
logger.debug("Testing SocksAuthResponseDecoder with authStatus: " + authStatus);
SocksAuthResponse msg = new SocksAuthResponse(authStatus);
SocksAuthResponseDecoder decoder = new SocksAuthResponseDecoder();
EmbeddedChannel embedder = new EmbeddedChannel(decoder);
SocksCommonTestUtils.writeMessageIntoEmbedder(embedder, msg);
msg = embedder.readInbound();
assertSame(msg.authStatus(), authStatus);
assertNull(embedder.readInbound());
}
@Test
public void testSocksCmdResponseDecoder() {
for (SocksAuthStatus authStatus: SocksAuthStatus.values()) {
testSocksAuthResponseDecoderWithDifferentParams(authStatus);
}
}
}

View File

@ -1,31 +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.socks;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class SocksAuthResponseTest {
@Test
public void testConstructorParamsAreNotNull() {
try {
new SocksAuthResponse(null);
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
}
}

View File

@ -1,109 +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.socks;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test;
import sun.net.util.IPAddressUtil;
import static org.junit.Assert.*;
public class SocksCmdRequestDecoderTest {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksCmdRequestDecoderTest.class);
private static void testSocksCmdRequestDecoderWithDifferentParams(SocksCmdType cmdType,
SocksAddressType addressType,
String host,
int port) {
logger.debug("Testing cmdType: " + cmdType + " addressType: " + addressType + " host: " + host +
" port: " + port);
SocksCmdRequest msg = new SocksCmdRequest(cmdType, addressType, host, port);
SocksCmdRequestDecoder decoder = new SocksCmdRequestDecoder();
EmbeddedChannel embedder = new EmbeddedChannel(decoder);
SocksCommonTestUtils.writeMessageIntoEmbedder(embedder, msg);
if (msg.addressType() == SocksAddressType.UNKNOWN) {
assertTrue(embedder.readInbound() instanceof UnknownSocksRequest);
} else {
msg = embedder.readInbound();
assertSame(msg.cmdType(), cmdType);
assertSame(msg.addressType(), addressType);
assertEquals(msg.host(), host);
assertEquals(msg.port(), port);
}
assertNull(embedder.readInbound());
}
@Test
public void testCmdRequestDecoderIPv4() {
String[] hosts = {"127.0.0.1", };
int[] ports = {1, 32769, 65535 };
for (SocksCmdType cmdType : SocksCmdType.values()) {
for (String host : hosts) {
for (int port : ports) {
testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksAddressType.IPv4, host, port);
}
}
}
}
@Test
public void testCmdRequestDecoderIPv6() {
String[] hosts = {SocksCommonUtils.ipv6toStr(IPAddressUtil.textToNumericFormatV6("::1"))};
int[] ports = {1, 32769, 65535};
for (SocksCmdType cmdType : SocksCmdType.values()) {
for (String host : hosts) {
for (int port : ports) {
testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksAddressType.IPv6, host, port);
}
}
}
}
@Test
public void testCmdRequestDecoderDomain() {
String[] hosts = {"google.com" ,
"مثال.إختبار",
"παράδειγμα.δοκιμή",
"مثال.آزمایشی",
"пример.испытание",
"בײַשפּיל.טעסט",
"例子.测试",
"例子.測試",
"उदाहरण.परीक्षा",
"例え.テスト",
"실례.테스트",
"உதாரணம்.பரிட்சை"};
int[] ports = {1, 32769, 65535};
for (SocksCmdType cmdType : SocksCmdType.values()) {
for (String host : hosts) {
for (int port : ports) {
testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksAddressType.DOMAIN, host, port);
}
}
}
}
@Test
public void testCmdRequestDecoderUnknown() {
String host = "google.com";
int port = 80;
for (SocksCmdType cmdType : SocksCmdType.values()) {
testSocksCmdRequestDecoderWithDifferentParams(cmdType, SocksAddressType.UNKNOWN, host, port);
}
}
}

View File

@ -1,91 +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.socks;
import org.junit.Test;
import static org.junit.Assert.*;
public class SocksCmdRequestTest {
@Test
public void testConstructorParamsAreNotNull() {
try {
new SocksCmdRequest(null, SocksAddressType.UNKNOWN, "", 1);
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
try {
new SocksCmdRequest(SocksCmdType.UNKNOWN, null, "", 1);
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
try {
new SocksCmdRequest(SocksCmdType.UNKNOWN, SocksAddressType.UNKNOWN, null, 1);
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
}
@Test
public void testIPv4CorrectAddress() {
try {
new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.IPv4, "54.54.1111.253", 1);
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
@Test
public void testIPv6CorrectAddress() {
try {
new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.IPv6, "xxx:xxx:xxx", 1);
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
@Test
public void testIDNNotExceeds255CharsLimit() {
try {
new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.DOMAIN,
"παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" +
"παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" +
"παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή" +
"παράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμήπαράδειγμα.δοκιμή", 1);
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
@Test
public void testValidPortRange() {
try {
new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.DOMAIN,
"παράδειγμα.δοκιμήπαράδει", 0);
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
try {
new SocksCmdRequest(SocksCmdType.BIND, SocksAddressType.DOMAIN,
"παράδειγμα.δοκιμήπαράδει", 65536);
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
}

View File

@ -1,84 +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.socks;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test;
import static org.junit.Assert.*;
public class SocksCmdResponseDecoderTest {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(SocksCmdResponseDecoderTest.class);
private static void testSocksCmdResponseDecoderWithDifferentParams(
SocksCmdStatus cmdStatus, SocksAddressType addressType, String host, int port) {
logger.debug("Testing cmdStatus: " + cmdStatus + " addressType: " + addressType);
SocksResponse msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
SocksCmdResponseDecoder decoder = new SocksCmdResponseDecoder();
EmbeddedChannel embedder = new EmbeddedChannel(decoder);
SocksCommonTestUtils.writeMessageIntoEmbedder(embedder, msg);
if (addressType == SocksAddressType.UNKNOWN) {
assertTrue(embedder.readInbound() instanceof UnknownSocksResponse);
} else {
msg = embedder.readInbound();
assertEquals(((SocksCmdResponse) msg).cmdStatus(), cmdStatus);
if (host != null) {
assertEquals(((SocksCmdResponse) msg).host(), host);
}
assertEquals(((SocksCmdResponse) msg).port(), port);
}
assertNull(embedder.readInbound());
}
/**
* Verifies that sent socks messages are decoded correctly.
*/
@Test
public void testSocksCmdResponseDecoder() {
for (SocksCmdStatus cmdStatus : SocksCmdStatus.values()) {
for (SocksAddressType addressType : SocksAddressType.values()) {
testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, addressType, null, 0);
}
}
}
/**
* Verifies that invalid bound host will fail with IllegalArgumentException during encoding.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidAddress() {
testSocksCmdResponseDecoderWithDifferentParams(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "1", 80);
}
/**
* Verifies that send socks messages are decoded correctly when bound host and port are set.
*/
@Test
public void testSocksCmdResponseDecoderIncludingHost() {
for (SocksCmdStatus cmdStatus : SocksCmdStatus.values()) {
testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, SocksAddressType.IPv4,
"127.0.0.1", 80);
testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, SocksAddressType.DOMAIN,
"testDomain.com", 80);
testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, SocksAddressType.IPv6,
"2001:db8:85a3:42:1000:8a2e:370:7334", 80);
testSocksCmdResponseDecoderWithDifferentParams(cmdStatus, SocksAddressType.IPv6,
"1111:111:11:1:0:0:0:1", 80);
}
}
}

View File

@ -1,140 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Test;
import static org.junit.Assert.*;
public class SocksCmdResponseTest {
@Test
public void testConstructorParamsAreNotNull() {
try {
new SocksCmdResponse(null, SocksAddressType.UNKNOWN);
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
try {
new SocksCmdResponse(SocksCmdStatus.UNASSIGNED, null);
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
}
/**
* Verifies content of the response when domain is not specified.
*/
@Test
public void testEmptyDomain() {
SocksCmdResponse socksCmdResponse = new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.DOMAIN);
assertNull(socksCmdResponse.host());
assertEquals(0, socksCmdResponse.port());
ByteBuf buffer = Unpooled.buffer(20);
socksCmdResponse.encodeAsByteBuf(buffer);
byte[] expected = {
0x05, // version
0x00, // success reply
0x00, // reserved
0x03, // address type domain
0x01, // length of domain
0x00, // domain value
0x00, // port value
0x00
};
assertByteBufEquals(expected, buffer);
}
/**
* Verifies content of the response when IPv4 address is specified.
*/
@Test
public void testIPv4Host() {
SocksCmdResponse socksCmdResponse = new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4,
"127.0.0.1", 80);
assertEquals("127.0.0.1", socksCmdResponse.host());
assertEquals(80, socksCmdResponse.port());
ByteBuf buffer = Unpooled.buffer(20);
socksCmdResponse.encodeAsByteBuf(buffer);
byte[] expected = {
0x05, // version
0x00, // success reply
0x00, // reserved
0x01, // address type IPv4
0x7F, // address 127.0.0.1
0x00,
0x00,
0x01,
0x00, // port
0x50
};
assertByteBufEquals(expected, buffer);
}
/**
* Verifies that empty domain is allowed Response.
*/
@Test
public void testEmptyBoundAddress() {
SocksCmdResponse socksCmdResponse = new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.DOMAIN,
"", 80);
assertEquals("", socksCmdResponse.host());
assertEquals(80, socksCmdResponse.port());
ByteBuf buffer = Unpooled.buffer(20);
socksCmdResponse.encodeAsByteBuf(buffer);
byte[] expected = {
0x05, // version
0x00, // success reply
0x00, // reserved
0x03, // address type domain
0x00, // domain length
0x00, // port
0x50
};
assertByteBufEquals(expected, buffer);
}
/**
* Verifies that Response cannot be constructed with invalid IP.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidBoundAddress() {
new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "127.0.0", 1000);
}
private static void assertByteBufEquals(byte[] expected, ByteBuf actual) {
byte[] actualBytes = new byte[actual.readableBytes()];
actual.readBytes(actualBytes);
assertEquals("Generated response has incorrect length", expected.length, actualBytes.length);
assertArrayEquals("Generated response differs from expected", expected, actualBytes);
}
@Test
public void testValidPortRange() {
try {
new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "127.0.0", 0);
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
try {
new SocksCmdResponse(SocksCmdStatus.SUCCESS, SocksAddressType.IPv4, "127.0.0", 65536);
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
}

View File

@ -1,35 +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.socks;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
final class SocksCommonTestUtils {
/**
* A constructor to stop this class being constructed.
*/
private SocksCommonTestUtils() {
//NOOP
}
public static void writeMessageIntoEmbedder(EmbeddedChannel embedder, SocksMessage msg) {
ByteBuf buf = Unpooled.buffer();
msg.encodeAsByteBuf(buf);
embedder.writeInbound(buf);
}
}

View File

@ -1,30 +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.socks;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class SocksInitRequestTest {
@Test
public void testConstructorParamsAreNotNull() {
try {
new SocksInitRequest(null);
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
}
}

View File

@ -1,30 +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.socks;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class SocksInitResponseTest {
@Test
public void testConstructorParamsAreNotNull() {
try {
new SocksInitResponse(null);
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
}
}