View Javadoc
1   /*
2    * Copyright 2025 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.socksx.v5;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.DecoderException;
21  import io.netty.handler.codec.DecoderResult;
22  import io.netty.handler.codec.ByteToMessageDecoder;
23  import io.netty.util.internal.EmptyArrays;
24  
25  import java.util.List;
26  
27  /**
28   * Decodes a single {@link Socks5PrivateAuthRequest} from the inbound {@link ByteBuf}s.
29   * On successful decode, this decoder will forward the received data to the next handler, so that
30   * other handler can remove or replace this decoder later. On failed decode, this decoder will
31   * discard the received data, so that other handler closes the connection later.
32   */
33  public final class Socks5PrivateAuthRequestDecoder extends ByteToMessageDecoder {
34  
35      private enum State {
36          INIT,
37          SUCCESS,
38          FAILURE
39      }
40  
41      private State state = State.INIT;
42  
43      @Override
44      protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
45          try {
46              if (state == State.SUCCESS) {
47                  int readableBytes = in.readableBytes();
48                  if (readableBytes > 0) {
49                      out.add(in.readRetainedSlice(readableBytes));
50                  }
51                  return;
52              }
53              if (state == State.FAILURE) {
54                  in.skipBytes(in.readableBytes());
55                  return;
56              }
57  
58              // Check if we have enough data to decode the message
59              if (in.readableBytes() < 2) {
60                  return;
61              }
62  
63              final int startOffset = in.readerIndex();
64              final byte version = in.getByte(startOffset);
65              if (version != 1) {
66                  throw new DecoderException("unsupported subnegotiation version: " + version + " (expected: 1)");
67              }
68  
69              final int tokenLength = in.getUnsignedByte(startOffset + 1);
70  
71              // Check if the full message is available
72              if (in.readableBytes() < 2 + tokenLength) {
73                  return;
74              }
75  
76              // Read the version and token length
77              in.skipBytes(2);
78  
79              // Read the token
80              byte[] token = new byte[tokenLength];
81              in.readBytes(token);
82  
83              // Add the decoded token to the output list
84              out.add(new DefaultSocks5PrivateAuthRequest(token));
85  
86              // Mark as decoded to handle remaining bytes in future calls
87              state = State.SUCCESS;
88          } catch (Exception e) {
89              fail(in, out, e);
90          }
91      }
92  
93      private void fail(ByteBuf in, List<Object> out, Exception cause) {
94          if (!(cause instanceof DecoderException)) {
95              cause = new DecoderException(cause);
96          }
97  
98          state = State.FAILURE;
99          in.skipBytes(in.readableBytes());
100 
101         Socks5Message m = new
102             DefaultSocks5PrivateAuthRequest(EmptyArrays.EMPTY_BYTES);
103         m.setDecoderResult(DecoderResult.failure(cause));
104         out.add(m);
105     }
106 }