socks5_impl/protocol/handshake/
auth_method.rs

1/// A proxy authentication method.
2#[repr(u8)]
3#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Default)]
4pub enum AuthMethod {
5    /// No authentication required.
6    #[default]
7    NoAuth = 0x00,
8    /// GSS API.
9    GssApi = 0x01,
10    /// A username + password authentication.
11    UserPass = 0x02,
12    /// IANA reserved 0x03..=0x7f.
13    IanaReserved(u8),
14    /// A private authentication method 0x80..=0xfe.
15    Private(u8),
16    /// X'FF' NO ACCEPTABLE METHODS
17    NoAcceptableMethods = 0xff,
18}
19
20impl From<u8> for AuthMethod {
21    fn from(value: u8) -> Self {
22        match value {
23            0x00 => AuthMethod::NoAuth,
24            0x01 => AuthMethod::GssApi,
25            0x02 => AuthMethod::UserPass,
26            0x03..=0x7f => AuthMethod::IanaReserved(value),
27            0x80..=0xfe => AuthMethod::Private(value),
28            0xff => AuthMethod::NoAcceptableMethods,
29        }
30    }
31}
32
33impl From<AuthMethod> for u8 {
34    fn from(value: AuthMethod) -> Self {
35        From::<&AuthMethod>::from(&value)
36    }
37}
38
39impl From<&AuthMethod> for u8 {
40    fn from(value: &AuthMethod) -> Self {
41        match value {
42            AuthMethod::NoAuth => 0x00,
43            AuthMethod::GssApi => 0x01,
44            AuthMethod::UserPass => 0x02,
45            AuthMethod::IanaReserved(value) => *value,
46            AuthMethod::Private(value) => *value,
47            AuthMethod::NoAcceptableMethods => 0xff,
48        }
49    }
50}
51
52impl std::fmt::Display for AuthMethod {
53    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
54        match self {
55            AuthMethod::NoAuth => write!(f, "NoAuth"),
56            AuthMethod::GssApi => write!(f, "GssApi"),
57            AuthMethod::UserPass => write!(f, "UserPass"),
58            AuthMethod::IanaReserved(value) => write!(f, "IanaReserved({value:#x})"),
59            AuthMethod::Private(value) => write!(f, "Private({value:#x})"),
60            AuthMethod::NoAcceptableMethods => write!(f, "NoAcceptableMethods"),
61        }
62    }
63}