1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.socksx.v5;
18
19 import io.netty.util.internal.ObjectUtil;
20
21
22
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
32
33 public static final Socks5AuthMethod UNACCEPTED = new Socks5AuthMethod(0xff, "UNACCEPTED");
34
35
36
37
38
39
40
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
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 }