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