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.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   * The default {@link Socks5CommandRequest}.
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 }