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 DefaultSocks5CommandResponse extends AbstractSocks5Message implements Socks5CommandResponse {
29
30 private final Socks5CommandStatus status;
31 private final Socks5AddressType bndAddrType;
32 private final String bndAddr;
33 private final int bndPort;
34
35 public DefaultSocks5CommandResponse(Socks5CommandStatus status, Socks5AddressType bndAddrType) {
36 this(status, bndAddrType, null, 0);
37 }
38
39 public DefaultSocks5CommandResponse(
40 Socks5CommandStatus status, Socks5AddressType bndAddrType, String bndAddr, int bndPort) {
41
42 ObjectUtil.checkNotNull(status, "status");
43 ObjectUtil.checkNotNull(bndAddrType, "bndAddrType");
44
45 if (bndAddr != null) {
46 if (bndAddrType == Socks5AddressType.IPv4) {
47 if (!NetUtil.isValidIpV4Address(bndAddr)) {
48 throw new IllegalArgumentException("bndAddr: " + bndAddr + " (expected: a valid IPv4 address)");
49 }
50 } else if (bndAddrType == Socks5AddressType.DOMAIN) {
51 bndAddr = IDN.toASCII(bndAddr);
52 if (bndAddr.length() > 255) {
53 throw new IllegalArgumentException("bndAddr: " + bndAddr + " (expected: less than 256 chars)");
54 }
55 } else if (bndAddrType == Socks5AddressType.IPv6) {
56 if (!NetUtil.isValidIpV6Address(bndAddr)) {
57 throw new IllegalArgumentException("bndAddr: " + bndAddr + " (expected: a valid IPv6 address)");
58 }
59 }
60 }
61
62 if (bndPort < 0 || bndPort > 65535) {
63 throw new IllegalArgumentException("bndPort: " + bndPort + " (expected: 0~65535)");
64 }
65 this.status = status;
66 this.bndAddrType = bndAddrType;
67 this.bndAddr = bndAddr;
68 this.bndPort = bndPort;
69 }
70
71 @Override
72 public Socks5CommandStatus status() {
73 return status;
74 }
75
76 @Override
77 public Socks5AddressType bndAddrType() {
78 return bndAddrType;
79 }
80
81 @Override
82 public String bndAddr() {
83 return bndAddr;
84 }
85
86 @Override
87 public int bndPort() {
88 return bndPort;
89 }
90
91 @Override
92 public String toString() {
93 StringBuilder buf = new StringBuilder(128);
94 buf.append(StringUtil.simpleClassName(this));
95
96 DecoderResult decoderResult = decoderResult();
97 if (!decoderResult.isSuccess()) {
98 buf.append("(decoderResult: ");
99 buf.append(decoderResult);
100 buf.append(", status: ");
101 } else {
102 buf.append("(status: ");
103 }
104 buf.append(status());
105 buf.append(", bndAddrType: ");
106 buf.append(bndAddrType());
107 buf.append(", bndAddr: ");
108 buf.append(bndAddr());
109 buf.append(", bndPort: ");
110 buf.append(bndPort());
111 buf.append(')');
112
113 return buf.toString();
114 }
115 }