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.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.encoder(CharsetUtil.US_ASCII);
31 private static final SocksSubnegotiationVersion SUBNEGOTIATION_VERSION = SocksSubnegotiationVersion.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("username");
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 username() {
63 return username;
64 }
65
66
67
68
69
70
71 public String password() {
72 return password;
73 }
74
75 @Override
76 public void encodeAsByteBuf(ByteBuf byteBuf) {
77 byteBuf.writeByte(SUBNEGOTIATION_VERSION.byteValue());
78 byteBuf.writeByte(username.length());
79 byteBuf.writeBytes(username.getBytes(CharsetUtil.US_ASCII));
80 byteBuf.writeByte(password.length());
81 byteBuf.writeBytes(password.getBytes(CharsetUtil.US_ASCII));
82 }
83 }