1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.socks;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.util.NetUtil;
20 import org.jboss.netty.util.internal.DetectionUtil;
21
22 import java.net.IDN;
23
24
25
26
27
28
29
30 public final class SocksCmdRequest extends SocksRequest {
31 private final CmdType cmdType;
32 private final AddressType addressType;
33 private final String host;
34 private final int port;
35
36 public SocksCmdRequest(CmdType cmdType, AddressType addressType, String host, int port) {
37 super(SocksRequestType.CMD);
38 if (DetectionUtil.javaVersion() < 6) {
39 throw new IllegalStateException("Only supported with Java version 6+");
40 }
41 if (cmdType == null) {
42 throw new NullPointerException("cmdType");
43 }
44 if (addressType == null) {
45 throw new NullPointerException("addressType");
46 }
47 if (host == null) {
48 throw new NullPointerException("host");
49 }
50 switch (addressType) {
51 case IPv4:
52 if (!NetUtil.isValidIpV4Address(host)) {
53 throw new IllegalArgumentException(host + " is not a valid IPv4 address");
54 }
55 break;
56 case DOMAIN:
57 if (IDN.toASCII(host).length() > 255) {
58 throw new IllegalArgumentException(host + " IDN: " + IDN.toASCII(host) + " exceeds 255 char limit");
59 }
60 break;
61 case IPv6:
62 if (!NetUtil.isValidIpV6Address(host)) {
63 throw new IllegalArgumentException(host + " is not a valid IPv6 address");
64 }
65 break;
66 case UNKNOWN:
67 break;
68 }
69 if (port <= 0 || port >= 65536) {
70 throw new IllegalArgumentException(port + " is not in bounds 0 < x < 65536");
71 }
72 this.cmdType = cmdType;
73 this.addressType = addressType;
74 this.host = IDN.toASCII(host);
75 this.port = port;
76 }
77
78
79
80
81 public CmdType getCmdType() {
82 return cmdType;
83 }
84
85
86
87
88 public AddressType getAddressType() {
89 return addressType;
90 }
91
92
93
94
95 public String getHost() {
96 return IDN.toUnicode(host);
97 }
98
99
100
101
102 public int getPort() {
103 return port;
104 }
105
106 @Override
107 public void encodeAsByteBuf(ChannelBuffer channelBuffer) throws Exception {
108 channelBuffer.writeByte(getProtocolVersion().getByteValue());
109 channelBuffer.writeByte(cmdType.getByteValue());
110 channelBuffer.writeByte(0x00);
111 channelBuffer.writeByte(addressType.getByteValue());
112 switch (addressType) {
113 case IPv4: {
114 channelBuffer.writeBytes(NetUtil.createByteArrayFromIpAddressString(host));
115 channelBuffer.writeShort(port);
116 break;
117 }
118
119 case DOMAIN: {
120 channelBuffer.writeByte(host.length());
121 channelBuffer.writeBytes(host.getBytes("US-ASCII"));
122 channelBuffer.writeShort(port);
123 break;
124 }
125
126 case IPv6: {
127 channelBuffer.writeBytes(NetUtil.createByteArrayFromIpAddressString(host));
128 channelBuffer.writeShort(port);
129 break;
130 }
131 }
132 }
133 }