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      /**
36       * Returns whether the authentication method code is in the private methods range (0x80-0xFE)
37       * as defined by <a href="https://www.ietf.org/rfc/rfc1928.txt">RFC 1928 section 3</a>.
38       *
39       * @param b The authentication method code
40       * @return true if the code is in the private methods range
41       */
42      public static boolean isPrivateMethod(byte b) {
43          int ubyte = b & 0xFF;
44          return ubyte >= 0x80 && ubyte <= 0xFE;
45      }
46  
47      public static Socks5AuthMethod valueOf(byte b) {
48          switch (b) {
49          case 0x00:
50              return NO_AUTH;
51          case 0x01:
52              return GSSAPI;
53          case 0x02:
54              return PASSWORD;
55          case (byte) 0xFF:
56              return UNACCEPTED;
57          }
58  
59          // Handle all private methods (0x80-0xFE)
60          if (isPrivateMethod(b)) {
61              return new Socks5AuthMethod(b, "PRIVATE_" + (b & 0xFF));
62          }
63  
64          return new Socks5AuthMethod(b);
65      }
66  
67      private final byte byteValue;
68      private final String name;
69      private String text;
70  
71      public Socks5AuthMethod(int byteValue) {
72          this(byteValue, "UNKNOWN");
73      }
74  
75      public Socks5AuthMethod(int byteValue, String name) {
76          this.name = ObjectUtil.checkNotNull(name, "name");
77          this.byteValue = (byte) byteValue;
78      }
79  
80      public byte byteValue() {
81          return byteValue;
82      }
83  
84      @Override
85      public int hashCode() {
86          return byteValue;
87      }
88  
89      @Override
90      public boolean equals(Object obj) {
91          if (!(obj instanceof Socks5AuthMethod)) {
92              return false;
93          }
94  
95          return byteValue == ((Socks5AuthMethod) obj).byteValue;
96      }
97  
98      @Override
99      public int compareTo(Socks5AuthMethod o) {
100         return byteValue - o.byteValue;
101     }
102 
103     @Override
104     public String toString() {
105         String text = this.text;
106         if (text == null) {
107             this.text = text = name + '(' + (byteValue & 0xFF) + ')';
108         }
109         return text;
110     }
111 }