1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.socksx.v5;
17
18 import io.netty.handler.codec.DecoderResult;
19 import io.netty.util.NetUtil;
20 import io.netty.util.internal.ObjectUtil;
21 import io.netty.util.internal.StringUtil;
22
23 import java.net.IDN;
24
25
26
27
28 public final class DefaultSocks5CommandRequest extends AbstractSocks5Message implements Socks5CommandRequest {
29
30 private final Socks5CommandType type;
31 private final Socks5AddressType dstAddrType;
32 private final String dstAddr;
33 private final int dstPort;
34
35 public DefaultSocks5CommandRequest(
36 Socks5CommandType type, Socks5AddressType dstAddrType, String dstAddr, int dstPort) {
37
38 this.type = ObjectUtil.checkNotNull(type, "type");
39 ObjectUtil.checkNotNull(dstAddrType, "dstAddrType");
40 ObjectUtil.checkNotNull(dstAddr, "dstAddr");
41
42 if (dstAddrType == Socks5AddressType.IPv4) {
43 if (!NetUtil.isValidIpV4Address(dstAddr)) {
44 throw new IllegalArgumentException("dstAddr: " + dstAddr + " (expected: a valid IPv4 address)");
45 }
46 } else if (dstAddrType == Socks5AddressType.DOMAIN) {
47 dstAddr = IDN.toASCII(dstAddr);
48 if (dstAddr.length() > 255) {
49 throw new IllegalArgumentException("dstAddr: " + dstAddr + " (expected: less than 256 chars)");
50 }
51 } else if (dstAddrType == Socks5AddressType.IPv6) {
52 if (!NetUtil.isValidIpV6Address(dstAddr)) {
53 throw new IllegalArgumentException("dstAddr: " + dstAddr + " (expected: a valid IPv6 address");
54 }
55 }
56
57 if (dstPort < 0 || dstPort > 65535) {
58 throw new IllegalArgumentException("dstPort: " + dstPort + " (expected: 0~65535)");
59 }
60
61 this.dstAddrType = dstAddrType;
62 this.dstAddr = dstAddr;
63 this.dstPort = dstPort;
64 }
65
66 @Override
67 public Socks5CommandType type() {
68 return type;
69 }
70
71 @Override
72 public Socks5AddressType dstAddrType() {
73 return dstAddrType;
74 }
75
76 @Override
77 public String dstAddr() {
78 return dstAddr;
79 }
80
81 @Override
82 public int dstPort() {
83 return dstPort;
84 }
85
86 @Override
87 public String toString() {
88 StringBuilder buf = new StringBuilder(128);
89 buf.append(StringUtil.simpleClassName(this));
90
91 DecoderResult decoderResult = decoderResult();
92 if (!decoderResult.isSuccess()) {
93 buf.append("(decoderResult: ");
94 buf.append(decoderResult);
95 buf.append(", type: ");
96 } else {
97 buf.append("(type: ");
98 }
99 buf.append(type());
100 buf.append(", dstAddrType: ");
101 buf.append(dstAddrType());
102 buf.append(", dstAddr: ");
103 buf.append(dstAddr());
104 buf.append(", dstPort: ");
105 buf.append(dstPort());
106 buf.append(')');
107
108 return buf.toString();
109 }
110 }