View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.socksx.v4;
17  
18  import io.netty.handler.codec.DecoderResult;
19  import io.netty.util.internal.ObjectUtil;
20  import io.netty.util.internal.StringUtil;
21  
22  import java.net.IDN;
23  
24  /**
25   * The default {@link Socks4CommandRequest}.
26   */
27  public class DefaultSocks4CommandRequest extends AbstractSocks4Message implements Socks4CommandRequest {
28  
29      private final Socks4CommandType type;
30      private final String dstAddr;
31      private final int dstPort;
32      private final String userId;
33  
34      /**
35       * Creates a new instance.
36       *
37       * @param type the type of the request
38       * @param dstAddr the {@code DSTIP} field of the request
39       * @param dstPort the {@code DSTPORT} field of the request
40       */
41      public DefaultSocks4CommandRequest(Socks4CommandType type, String dstAddr, int dstPort) {
42          this(type, dstAddr, dstPort, "");
43      }
44  
45      /**
46       * Creates a new instance.
47       *
48       * @param type the type of the request
49       * @param dstAddr the {@code DSTIP} field of the request
50       * @param dstPort the {@code DSTPORT} field of the request
51       * @param userId the {@code USERID} field of the request
52       */
53      public DefaultSocks4CommandRequest(Socks4CommandType type, String dstAddr, int dstPort, String userId) {
54          if (dstPort <= 0 || dstPort >= 65536) {
55              throw new IllegalArgumentException("dstPort: " + dstPort + " (expected: 1~65535)");
56          }
57          this.type = ObjectUtil.checkNotNull(type, "type");
58          this.dstAddr = IDN.toASCII(
59                  ObjectUtil.checkNotNull(dstAddr, "dstAddr"));
60          this.userId = ObjectUtil.checkNotNull(userId, "userId");
61          this.dstPort = dstPort;
62      }
63  
64      @Override
65      public Socks4CommandType type() {
66          return type;
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 userId() {
81          return userId;
82      }
83  
84      @Override
85      public String toString() {
86          StringBuilder buf = new StringBuilder(128);
87          buf.append(StringUtil.simpleClassName(this));
88  
89          DecoderResult decoderResult = decoderResult();
90          if (!decoderResult.isSuccess()) {
91              buf.append("(decoderResult: ");
92              buf.append(decoderResult);
93              buf.append(", type: ");
94          } else {
95              buf.append("(type: ");
96          }
97          buf.append(type());
98          buf.append(", dstAddr: ");
99          buf.append(dstAddr());
100         buf.append(", dstPort: ");
101         buf.append(dstPort());
102         buf.append(", userId: ");
103         buf.append(userId());
104         buf.append(')');
105 
106         return buf.toString();
107     }
108 }