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.buffer.ByteBufUtil;
20 import io.netty.channel.ChannelHandlerContext;
21 import io.netty.handler.codec.ReplayingDecoder;
22 import io.netty.handler.codec.socks.SocksCmdResponseDecoder.State;
23 import io.netty.util.CharsetUtil;
24 import io.netty.util.NetUtil;
25 import io.netty.util.internal.UnstableApi;
26
27 import java.util.List;
28
29
30
31
32
33 public class SocksCmdResponseDecoder extends ReplayingDecoder<State> {
34
35 private SocksCmdStatus cmdStatus;
36 private SocksAddressType addressType;
37
38 public SocksCmdResponseDecoder() {
39 super(State.CHECK_PROTOCOL_VERSION);
40 }
41
42 @Override
43 protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
44 switch (state()) {
45 case CHECK_PROTOCOL_VERSION: {
46 if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) {
47 out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE);
48 break;
49 }
50 checkpoint(State.READ_CMD_HEADER);
51 }
52 case READ_CMD_HEADER: {
53 cmdStatus = SocksCmdStatus.valueOf(byteBuf.readByte());
54 byteBuf.skipBytes(1);
55 addressType = SocksAddressType.valueOf(byteBuf.readByte());
56 checkpoint(State.READ_CMD_ADDRESS);
57 }
58 case READ_CMD_ADDRESS: {
59 switch (addressType) {
60 case IPv4: {
61 String host = NetUtil.intToIpAddress(ByteBufUtil.readIntBE(byteBuf));
62 int port = ByteBufUtil.readUnsignedShortBE(byteBuf);
63 out.add(new SocksCmdResponse(cmdStatus, addressType, host, port));
64 break;
65 }
66 case DOMAIN: {
67 int fieldLength = byteBuf.readByte();
68 String host = byteBuf.readString(fieldLength, CharsetUtil.US_ASCII);
69 int port = ByteBufUtil.readUnsignedShortBE(byteBuf);
70 out.add(new SocksCmdResponse(cmdStatus, addressType, host, port));
71 break;
72 }
73 case IPv6: {
74 byte[] bytes = new byte[16];
75 byteBuf.readBytes(bytes);
76 String host = SocksCommonUtils.ipv6toStr(bytes);
77 int port = ByteBufUtil.readUnsignedShortBE(byteBuf);
78 out.add(new SocksCmdResponse(cmdStatus, addressType, host, port));
79 break;
80 }
81 case UNKNOWN: {
82 out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE);
83 break;
84 }
85 default: {
86 throw new Error();
87 }
88 }
89 break;
90 }
91 default: {
92 throw new Error();
93 }
94 }
95 ctx.pipeline().remove(this);
96 }
97
98 @UnstableApi
99 public enum State {
100 CHECK_PROTOCOL_VERSION,
101 READ_CMD_HEADER,
102 READ_CMD_ADDRESS
103 }
104 }