socks5_impl/protocol/handshake/
auth_method.rs

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

impl From<u8> for AuthMethod {
    fn from(value: u8) -> Self {
        match value {
            0x00 => AuthMethod::NoAuth,
            0x01 => AuthMethod::GssApi,
            0x02 => AuthMethod::UserPass,
            0x03..=0x7f => AuthMethod::IanaReserved(value),
            0x80..=0xfe => AuthMethod::Private(value),
            0xff => AuthMethod::NoAcceptableMethods,
        }
    }
}

impl From<AuthMethod> for u8 {
    fn from(value: AuthMethod) -> Self {
        From::<&AuthMethod>::from(&value)
    }
}

impl From<&AuthMethod> for u8 {
    fn from(value: &AuthMethod) -> Self {
        match value {
            AuthMethod::NoAuth => 0x00,
            AuthMethod::GssApi => 0x01,
            AuthMethod::UserPass => 0x02,
            AuthMethod::IanaReserved(value) => *value,
            AuthMethod::Private(value) => *value,
            AuthMethod::NoAcceptableMethods => 0xff,
        }
    }
}

impl std::fmt::Display for AuthMethod {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            AuthMethod::NoAuth => write!(f, "NoAuth"),
            AuthMethod::GssApi => write!(f, "GssApi"),
            AuthMethod::UserPass => write!(f, "UserPass"),
            AuthMethod::IanaReserved(value) => write!(f, "IanaReserved({0:#x})", value),
            AuthMethod::Private(value) => write!(f, "Private({0:#x})", value),
            AuthMethod::NoAcceptableMethods => write!(f, "NoAcceptableMethods"),
        }
    }
}