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.SocksInitResponseDecoder.State;
22 import io.netty.util.internal.UnstableApi;
23
24 import java.util.List;
25
26
27
28
29
30 public class SocksInitResponseDecoder extends ReplayingDecoder<State> {
31
32 public SocksInitResponseDecoder() {
33 super(State.CHECK_PROTOCOL_VERSION);
34 }
35
36 @Override
37 protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
38 switch (state()) {
39 case CHECK_PROTOCOL_VERSION: {
40 if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) {
41 out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE);
42 break;
43 }
44 checkpoint(State.READ_PREFERRED_AUTH_TYPE);
45 }
46 case READ_PREFERRED_AUTH_TYPE: {
47 SocksAuthScheme authScheme = SocksAuthScheme.valueOf(byteBuf.readByte());
48 out.add(new SocksInitResponse(authScheme));
49 break;
50 }
51 default: {
52 throw new Error();
53 }
54 }
55 ctx.pipeline().remove(this);
56 }
57
58 @UnstableApi
59 public enum State {
60 CHECK_PROTOCOL_VERSION,
61 READ_PREFERRED_AUTH_TYPE
62 }
63 }