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.socks;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.ReplayingDecoder;
21  import io.netty.handler.codec.socks.SocksCmdRequestDecoder.State;
22  import io.netty.util.NetUtil;
23  import io.netty.util.internal.UnstableApi;
24  
25  import java.util.List;
26  
27  /**
28   * Decodes {@link ByteBuf}s into {@link SocksCmdRequest}.
29   * Before returning SocksRequest decoder removes itself from pipeline.
30   */
31  public class SocksCmdRequestDecoder extends ReplayingDecoder<State> {
32  
33      private SocksCmdType cmdType;
34      private SocksAddressType addressType;
35  
36      public SocksCmdRequestDecoder() {
37          super(State.CHECK_PROTOCOL_VERSION);
38      }
39  
40      @Override
41      protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
42          switch (state()) {
43              case CHECK_PROTOCOL_VERSION: {
44                  if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) {
45                      out.add(SocksCommonUtils.UNKNOWN_SOCKS_REQUEST);
46                      break;
47                  }
48                  checkpoint(State.READ_CMD_HEADER);
49              }
50              case READ_CMD_HEADER: {
51                  cmdType = SocksCmdType.valueOf(byteBuf.readByte());
52                  byteBuf.skipBytes(1); // reserved
53                  addressType = SocksAddressType.valueOf(byteBuf.readByte());
54                  checkpoint(State.READ_CMD_ADDRESS);
55              }
56              case READ_CMD_ADDRESS: {
57                  switch (addressType) {
58                      case IPv4: {
59                          String host = NetUtil.intToIpAddress(byteBuf.readInt());
60                          int port = byteBuf.readUnsignedShort();
61                          out.add(new SocksCmdRequest(cmdType, addressType, host, port));
62                          break;
63                      }
64                      case DOMAIN: {
65                          int fieldLength = byteBuf.readByte();
66                          String host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
67                          int port = byteBuf.readUnsignedShort();
68                          out.add(new SocksCmdRequest(cmdType, addressType, host, port));
69                          break;
70                      }
71                      case IPv6: {
72                          byte[] bytes = new byte[16];
73                          byteBuf.readBytes(bytes);
74                          String host = SocksCommonUtils.ipv6toStr(bytes);
75                          int port = byteBuf.readUnsignedShort();
76                          out.add(new SocksCmdRequest(cmdType, addressType, host, port));
77                          break;
78                      }
79                      case UNKNOWN: {
80                          out.add(SocksCommonUtils.UNKNOWN_SOCKS_REQUEST);
81                          break;
82                      }
83                      default: {
84                          throw new Error();
85                      }
86                  }
87                  break;
88              }
89              default: {
90                  throw new Error();
91              }
92          }
93          ctx.pipeline().remove(this);
94      }
95  
96      @UnstableApi
97      public enum State {
98          CHECK_PROTOCOL_VERSION,
99          READ_CMD_HEADER,
100         READ_CMD_ADDRESS
101     }
102 }