1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.socks;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.handler.codec.ReplayingDecoder;
21 import io.netty.handler.codec.socks.SocksInitRequestDecoder.State;
22 import io.netty.util.internal.UnstableApi;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28
29
30
31 public class SocksInitRequestDecoder extends ReplayingDecoder<State> {
32
33 public SocksInitRequestDecoder() {
34 super(State.CHECK_PROTOCOL_VERSION);
35 }
36
37 @Override
38 protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
39 switch (state()) {
40 case CHECK_PROTOCOL_VERSION: {
41 if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) {
42 out.add(SocksCommonUtils.UNKNOWN_SOCKS_REQUEST);
43 break;
44 }
45 checkpoint(State.READ_AUTH_SCHEMES);
46 }
47 case READ_AUTH_SCHEMES: {
48 final int authSchemeNum = byteBuf.readUnsignedByte();
49 if (authSchemeNum == 0) {
50
51 out.add(SocksCommonUtils.UNKNOWN_SOCKS_REQUEST);
52 break;
53 }
54 final List<SocksAuthScheme> authSchemes = new ArrayList<SocksAuthScheme>(authSchemeNum);
55 for (int i = 0; i < authSchemeNum; i++) {
56 authSchemes.add(SocksAuthScheme.valueOf(byteBuf.readByte()));
57 }
58 out.add(new SocksInitRequest(authSchemes));
59 break;
60 }
61 default: {
62 throw new Error("Unexpected request decoder type: " + state());
63 }
64 }
65 ctx.pipeline().remove(this);
66 }
67
68 @UnstableApi
69 public enum State {
70 CHECK_PROTOCOL_VERSION,
71 READ_AUTH_SCHEMES
72 }
73 }