View Javadoc
1   /*
2    * Copyright 2013 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  
17  package io.netty.handler.codec.socksx.v5;
18  
19  import io.netty.util.internal.ObjectUtil;
20  
21  /**
22   * The authentication method of SOCKS5.
23   */
24  public class Socks5AuthMethod implements Comparable<Socks5AuthMethod> {
25  
26      public static final Socks5AuthMethod NO_AUTH = new Socks5AuthMethod(0x00, "NO_AUTH");
27      public static final Socks5AuthMethod GSSAPI = new Socks5AuthMethod(0x01, "GSSAPI");
28      public static final Socks5AuthMethod PASSWORD = new Socks5AuthMethod(0x02, "PASSWORD");
29  
30      /**
31       * Indicates that the server does not accept any authentication methods the client proposed.
32       */
33      public static final Socks5AuthMethod UNACCEPTED = new Socks5AuthMethod(0xff, "UNACCEPTED");
34  
35      public static Socks5AuthMethod valueOf(byte b) {
36          switch (b) {
37          case 0x00:
38              return NO_AUTH;
39          case 0x01:
40              return GSSAPI;
41          case 0x02:
42              return PASSWORD;
43          case (byte) 0xFF:
44              return UNACCEPTED;
45          }
46  
47          return new Socks5AuthMethod(b);
48      }
49  
50      private final byte byteValue;
51      private final String name;
52      private String text;
53  
54      public Socks5AuthMethod(int byteValue) {
55          this(byteValue, "UNKNOWN");
56      }
57  
58      public Socks5AuthMethod(int byteValue, String name) {
59          this.name = ObjectUtil.checkNotNull(name, "name");
60          this.byteValue = (byte) byteValue;
61      }
62  
63      public byte byteValue() {
64          return byteValue;
65      }
66  
67      @Override
68      public int hashCode() {
69          return byteValue;
70      }
71  
72      @Override
73      public boolean equals(Object obj) {
74          if (!(obj instanceof Socks5AuthMethod)) {
75              return false;
76          }
77  
78          return byteValue == ((Socks5AuthMethod) obj).byteValue;
79      }
80  
81      @Override
82      public int compareTo(Socks5AuthMethod o) {
83          return byteValue - o.byteValue;
84      }
85  
86      @Override
87      public String toString() {
88          String text = this.text;
89          if (text == null) {
90              this.text = text = name + '(' + (byteValue & 0xFF) + ')';
91          }
92          return text;
93      }
94  }