1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.socks;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.channel.Channel;
20 import org.jboss.netty.channel.ChannelHandlerContext;
21 import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
22 import org.jboss.netty.util.CharsetUtil;
23
24
25
26
27
28 public class SocksAuthRequestDecoder extends ReplayingDecoder<SocksAuthRequestDecoder.State> {
29 private static final String name = "SOCKS_AUTH_REQUEST_DECODER";
30
31
32
33
34 @Deprecated
35 public static String getName() {
36 return name;
37 }
38
39 private SocksMessage.SubnegotiationVersion version;
40 private int fieldLength;
41 private String username;
42 private String password;
43 private SocksRequest msg = SocksCommonUtils.UNKNOWN_SOCKS_REQUEST;
44
45 public SocksAuthRequestDecoder() {
46 super(State.CHECK_PROTOCOL_VERSION);
47 }
48
49 @Override
50 protected Object decode(ChannelHandlerContext ctx, Channel channel,
51 ChannelBuffer buffer, State state) throws Exception {
52 switch (state) {
53 case CHECK_PROTOCOL_VERSION: {
54 version = SocksMessage.SubnegotiationVersion.fromByte(buffer.readByte());
55 if (version != SocksMessage.SubnegotiationVersion.AUTH_PASSWORD) {
56 break;
57 }
58 checkpoint(State.READ_USERNAME);
59 }
60 case READ_USERNAME: {
61 fieldLength = buffer.readByte();
62 username = buffer.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
63 checkpoint(State.READ_PASSWORD);
64 }
65 case READ_PASSWORD: {
66 fieldLength = buffer.readByte();
67 password = buffer.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
68 msg = new SocksAuthRequest(username, password);
69 }
70 }
71 ctx.getPipeline().remove(this);
72 return msg;
73 }
74
75 enum State {
76 CHECK_PROTOCOL_VERSION,
77 READ_USERNAME,
78 READ_PASSWORD
79 }
80 }