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