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    *   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  package org.jboss.netty.handler.codec.socks;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.channel.Channel;
20  import org.jboss.netty.channel.ChannelHandlerContext;
21  import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
22  import org.jboss.netty.util.CharsetUtil;
23  
24  /**
25   * Decodes {@link ChannelBuffer}s into {@link SocksAuthRequest}.
26   * Before returning SocksRequest decoder removes itself from pipeline.
27   */
28  public class SocksAuthRequestDecoder extends ReplayingDecoder<SocksAuthRequestDecoder.State> {
29      private static final String name = "SOCKS_AUTH_REQUEST_DECODER";
30  
31      /**
32       * @deprecated Will be removed at the next minor version bump.
33       */
34      @Deprecated
35      public static String getName() {
36          return name;
37      }
38  
39      private SocksMessage.SubnegotiationVersion version;
40      private int fieldLength;
41      private String username;
42      private String password;
43      private SocksRequest msg = SocksCommonUtils.UNKNOWN_SOCKS_REQUEST;
44  
45      public SocksAuthRequestDecoder() {
46          super(State.CHECK_PROTOCOL_VERSION);
47      }
48  
49      @Override
50      protected Object decode(ChannelHandlerContext ctx, Channel channel,
51                              ChannelBuffer buffer, State state) throws Exception {
52          switch (state) {
53              case CHECK_PROTOCOL_VERSION: {
54                  version = SocksMessage.SubnegotiationVersion.fromByte(buffer.readByte());
55                  if (version != SocksMessage.SubnegotiationVersion.AUTH_PASSWORD) {
56                      break;
57                  }
58                  checkpoint(State.READ_USERNAME);
59              }
60              case READ_USERNAME: {
61                  fieldLength = buffer.readByte();
62                  username = buffer.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
63                  checkpoint(State.READ_PASSWORD);
64              }
65              case READ_PASSWORD: {
66                  fieldLength = buffer.readByte();
67                  password = buffer.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
68                  msg = new SocksAuthRequest(username, password);
69              }
70          }
71          ctx.getPipeline().remove(this);
72          return msg;
73      }
74  
75      enum State {
76          CHECK_PROTOCOL_VERSION,
77          READ_USERNAME,
78          READ_PASSWORD
79      }
80  }