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  import io.netty.util.internal.ObjectUtil;
21  
22  import java.nio.charset.CharsetEncoder;
23  
24  
25  
26  
27  
28  
29  
30  public final class SocksAuthRequest extends SocksRequest {
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          ObjectUtil.checkNotNull(username, "username");
38          ObjectUtil.checkNotNull(password, "password");
39          final CharsetEncoder asciiEncoder = CharsetUtil.encoder(CharsetUtil.US_ASCII);
40          if (!asciiEncoder.canEncode(username) || !asciiEncoder.canEncode(password)) {
41              throw new IllegalArgumentException(
42                      "username: " + username + " or password: **** values should be in pure ascii");
43          }
44          if (username.length() > 255) {
45              throw new IllegalArgumentException("username: " + username + " exceeds 255 char limit");
46          }
47          if (password.length() > 255) {
48              throw new IllegalArgumentException("password: **** exceeds 255 char limit");
49          }
50          this.username = username;
51          this.password = password;
52      }
53  
54      
55  
56  
57  
58  
59      public String username() {
60          return username;
61      }
62  
63      
64  
65  
66  
67  
68      public String password() {
69          return password;
70      }
71  
72      @Override
73      public void encodeAsByteBuf(ByteBuf byteBuf) {
74          byteBuf.writeByte(SUBNEGOTIATION_VERSION.byteValue());
75          byteBuf.writeByte(username.length());
76          byteBuf.writeCharSequence(username, CharsetUtil.US_ASCII);
77          byteBuf.writeByte(password.length());
78          byteBuf.writeCharSequence(password, CharsetUtil.US_ASCII);
79      }
80  }