1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
31
32 public final class Socks5PrivateAuthRequestDecoder extends ByteToMessageDecoder {
33
34 private boolean decoded;
35
36 @Override
37 protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
38 try {
39 if (decoded) {
40 int readableBytes = in.readableBytes();
41 if (readableBytes > 0) {
42 out.add(in.readRetainedSlice(readableBytes));
43 }
44 return;
45 }
46
47
48 if (in.readableBytes() < 2) {
49 return;
50 }
51
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 tokenLength = in.getUnsignedByte(startOffset + 1);
59
60
61 if (in.readableBytes() < 2 + tokenLength) {
62 return;
63 }
64
65
66 in.skipBytes(2);
67
68
69 byte[] token = new byte[tokenLength];
70 in.readBytes(token);
71
72
73 out.add(new DefaultSocks5PrivateAuthRequest(token));
74
75
76 decoded = true;
77 } catch (Exception e) {
78 fail(out, e);
79 }
80 }
81
82 private void fail(List<Object> out, Exception cause) {
83 if (!(cause instanceof DecoderException)) {
84 cause = new DecoderException(cause);
85 }
86
87 decoded = true;
88
89 Socks5Message m = new
90 DefaultSocks5PrivateAuthRequest(EmptyArrays.EMPTY_BYTES);
91 m.setDecoderResult(DecoderResult.failure(cause));
92 out.add(m);
93 }
94 }