1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.socksx.v4;
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
24
25
26 public class DefaultSocks4CommandResponse extends AbstractSocks4Message implements Socks4CommandResponse {
27
28 private final Socks4CommandStatus status;
29 private final String dstAddr;
30 private final int dstPort;
31
32
33
34
35
36
37 public DefaultSocks4CommandResponse(Socks4CommandStatus status) {
38 this(status, null, 0);
39 }
40
41
42
43
44
45
46
47
48 public DefaultSocks4CommandResponse(Socks4CommandStatus status, String dstAddr, int dstPort) {
49 if (dstAddr != null) {
50 if (!NetUtil.isValidIpV4Address(dstAddr)) {
51 throw new IllegalArgumentException(
52 "dstAddr: " + dstAddr + " (expected: a valid IPv4 address)");
53 }
54 }
55 if (dstPort < 0 || dstPort > 65535) {
56 throw new IllegalArgumentException("dstPort: " + dstPort + " (expected: 0~65535)");
57 }
58
59 this.status = ObjectUtil.checkNotNull(status, "cmdStatus");
60 this.dstAddr = dstAddr;
61 this.dstPort = dstPort;
62 }
63
64 @Override
65 public Socks4CommandStatus status() {
66 return status;
67 }
68
69 @Override
70 public String dstAddr() {
71 return dstAddr;
72 }
73
74 @Override
75 public int dstPort() {
76 return dstPort;
77 }
78
79 @Override
80 public String toString() {
81 StringBuilder buf = new StringBuilder(96);
82 buf.append(StringUtil.simpleClassName(this));
83
84 DecoderResult decoderResult = decoderResult();
85 if (!decoderResult.isSuccess()) {
86 buf.append("(decoderResult: ");
87 buf.append(decoderResult);
88 buf.append(", dstAddr: ");
89 } else {
90 buf.append("(dstAddr: ");
91 }
92 buf.append(dstAddr());
93 buf.append(", dstPort: ");
94 buf.append(dstPort());
95 buf.append(')');
96
97 return buf.toString();
98 }
99 }