View Javadoc
1   /*
2    * Copyright 2014 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  
17  package io.netty.handler.codec.socksx.v5;
18  
19  import io.netty.buffer.ByteBuf;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.handler.codec.DecoderException;
22  import io.netty.handler.codec.DecoderResult;
23  import io.netty.handler.codec.ReplayingDecoder;
24  import io.netty.handler.codec.socksx.SocksVersion;
25  import io.netty.handler.codec.socksx.v5.Socks5InitialResponseDecoder.State;
26  import io.netty.util.internal.UnstableApi;
27  
28  import java.util.List;
29  
30  /**
31   * Decodes a single {@link Socks5InitialResponse} from the inbound {@link ByteBuf}s.
32   * On successful decode, this decoder will forward the received data to the next handler, so that
33   * other handler can remove or replace this decoder later.  On failed decode, this decoder will
34   * discard the received data, so that other handler closes the connection later.
35   */
36  public class Socks5InitialResponseDecoder extends ReplayingDecoder<State> {
37  
38      @UnstableApi
39      public enum State {
40          INIT,
41          SUCCESS,
42          FAILURE
43      }
44  
45      public Socks5InitialResponseDecoder() {
46          super(State.INIT);
47      }
48  
49      @Override
50      protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
51          try {
52              switch (state()) {
53              case INIT: {
54                  final byte version = in.readByte();
55                  if (version != SocksVersion.SOCKS5.byteValue()) {
56                      throw new DecoderException(
57                              "unsupported version: " + version + " (expected: " + SocksVersion.SOCKS5.byteValue() + ')');
58                  }
59  
60                  final Socks5AuthMethod authMethod = Socks5AuthMethod.valueOf(in.readByte());
61                  out.add(new DefaultSocks5InitialResponse(authMethod));
62                  checkpoint(State.SUCCESS);
63              }
64              case SUCCESS: {
65                  int readableBytes = actualReadableBytes();
66                  if (readableBytes > 0) {
67                      out.add(in.readRetainedSlice(readableBytes));
68                  }
69                  break;
70              }
71              case FAILURE: {
72                  in.skipBytes(actualReadableBytes());
73                  break;
74              }
75              }
76          } catch (Exception e) {
77              fail(out, e);
78          }
79      }
80  
81      private void fail(List<Object> out, Exception cause) {
82          if (!(cause instanceof DecoderException)) {
83              cause = new DecoderException(cause);
84          }
85  
86          checkpoint(State.FAILURE);
87  
88          Socks5Message m = new DefaultSocks5InitialResponse(Socks5AuthMethod.UNACCEPTED);
89          m.setDecoderResult(DecoderResult.failure(cause));
90          out.add(m);
91      }
92  }