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    *   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  
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.v5.Socks5PasswordAuthRequestDecoder.State;
25  import io.netty.util.CharsetUtil;
26  
27  import java.util.List;
28  
29  /**
30   * Decodes a single {@link Socks5PasswordAuthRequest} from the inbound {@link ByteBuf}s.
31   * On successful decode, this decoder will forward the received data to the next handler, so that
32   * other handler can remove or replace this decoder later.  On failed decode, this decoder will
33   * discard the received data, so that other handler closes the connection later.
34   */
35  public class Socks5PasswordAuthRequestDecoder extends ReplayingDecoder<State> {
36  
37      enum State {
38          INIT,
39          SUCCESS,
40          FAILURE
41      }
42  
43      public Socks5PasswordAuthRequestDecoder() {
44          super(State.INIT);
45      }
46  
47      @Override
48      protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
49          try {
50              switch (state()) {
51              case INIT: {
52                  final int startOffset = in.readerIndex();
53                  final byte version = in.getByte(startOffset);
54                  if (version != 1) {
55                      throw new DecoderException("unsupported subnegotiation version: " + version + " (expected: 1)");
56                  }
57  
58                  final int usernameLength = in.getUnsignedByte(startOffset + 1);
59                  final int passwordLength = in.getUnsignedByte(startOffset + 2 + usernameLength);
60                  final int totalLength = usernameLength + passwordLength + 3;
61  
62                  in.skipBytes(totalLength);
63                  out.add(new DefaultSocks5PasswordAuthRequest(
64                          in.toString(startOffset + 2, usernameLength, CharsetUtil.US_ASCII),
65                          in.toString(startOffset + 3 + usernameLength, passwordLength, CharsetUtil.US_ASCII)));
66  
67                  checkpoint(State.SUCCESS);
68              }
69              case SUCCESS: {
70                  int readableBytes = actualReadableBytes();
71                  if (readableBytes > 0) {
72                      out.add(in.readSlice(readableBytes).retain());
73                  }
74                  break;
75              }
76              case FAILURE: {
77                  in.skipBytes(actualReadableBytes());
78                  break;
79              }
80              }
81          } catch (Exception e) {
82              fail(out, e);
83          }
84      }
85  
86      private void fail(List<Object> out, Throwable cause) {
87          if (!(cause instanceof DecoderException)) {
88              cause = new DecoderException(cause);
89          }
90  
91          checkpoint(State.FAILURE);
92  
93          Socks5Message m = new DefaultSocks5PasswordAuthRequest("", "");
94          m.setDecoderResult(DecoderResult.failure(cause));
95          out.add(m);
96      }
97  }