查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * An socks auth request.
26   *
27   * @see SocksAuthResponse
28   * @see SocksAuthRequestDecoder
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       * Returns username that needs to be authenticated
56       *
57       * @return username that needs to be authenticated
58       */
59      public String username() {
60          return username;
61      }
62  
63      /**
64       * Returns password that needs to be validated
65       *
66       * @return password that needs to be validated
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  }