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.util.CharsetUtil;
20
21 import java.nio.charset.CharsetEncoder;
22
23
24
25
26
27
28
29 public final class SocksAuthRequest extends SocksRequest {
30 private static final CharsetEncoder asciiEncoder = CharsetUtil.getEncoder(CharsetUtil.US_ASCII);
31 private static final SubnegotiationVersion SUBNEGOTIATION_VERSION = SubnegotiationVersion.AUTH_PASSWORD;
32 private final String username;
33 private final String password;
34
35 public SocksAuthRequest(String username, String password) {
36 super(SocksRequestType.AUTH);
37 if (username == null) {
38 throw new NullPointerException("username");
39 }
40 if (password == null) {
41 throw new NullPointerException("password");
42 }
43 if (!asciiEncoder.canEncode(username) || !asciiEncoder.canEncode(password)) {
44 throw new IllegalArgumentException(
45 "username: " + username + " or password: **** values should be in pure ascii");
46 }
47 if (username.length() > 255) {
48 throw new IllegalArgumentException("username: " + username + " exceeds 255 char limit");
49 }
50 if (password.length() > 255) {
51 throw new IllegalArgumentException("password: **** exceeds 255 char limit");
52 }
53 this.username = username;
54 this.password = password;
55 }
56
57
58
59
60
61
62 public String getUsername() {
63 return username;
64 }
65
66
67
68
69
70
71 public String getPassword() {
72 return password;
73 }
74
75 @Override
76 public void encodeAsByteBuf(ChannelBuffer channelBuffer) throws Exception {
77 channelBuffer.writeByte(SUBNEGOTIATION_VERSION.getByteValue());
78 channelBuffer.writeByte(username.length());
79 channelBuffer.writeBytes(username.getBytes("US-ASCII"));
80 channelBuffer.writeByte(password.length());
81 channelBuffer.writeBytes(password.getBytes("US-ASCII"));
82 }
83 }