gerevs/protocol/
methods.rs

1const NO_AUTH_REQUIRED: u8 = 0x00;
2const GSSAPI: u8 = 0x01;
3const USERNAME_PASSWORD: u8 = 0x02;
4
5const IANA_ASSIGNED_LOWER: u8 = 0x03;
6const IANA_ASSIGNED_UPPER: u8 = 0x7F;
7
8const PRIVATE_METHOD_LOWER: u8 = 0x80;
9const PRIVATE_METHOD_UPPER: u8 = 0xFE;
10
11const NO_ACCEPTABLE_METHODS: u8 = 0xFF;
12
13#[derive(Debug, PartialEq, Eq, Clone, Copy)]
14#[repr(u8)]
15/// The `AuthMethod` enum represents the various authentication methods supported by the SOCKS5 protocol.
16pub enum AuthMethod {
17    /// No authentication is required. This method allows clients to connect without providing any credentials.
18    NoAuthRequired,
19
20    /// GSSAPI (Generic Security Services Application Program Interface) authentication method.
21    Gssapi,
22
23    /// Username and password authentication method. This method requires clients to provide a username and password.
24    UsernamePassword,
25
26    /// IANA (Internet Assigned Numbers Authority) assigned authentication methods, represented by a `u8` value.
27    IanaAssigned(u8),
28
29    /// Private authentication methods, represented by a `u8` value. These are methods that are not standardized and can be defined by private agreements.
30    PrivateMethods(u8),
31
32    /// Indicates that no acceptable authentication methods are available. This method is used to indicate that the server does not accept any of the methods proposed by the client.
33    NoAcceptableMethods,
34}
35
36impl AuthMethod {
37    pub(crate) fn from_u8(value: u8) -> Self {
38        match value {
39            NO_AUTH_REQUIRED => AuthMethod::NoAuthRequired,
40            GSSAPI => AuthMethod::Gssapi,
41            USERNAME_PASSWORD => AuthMethod::UsernamePassword,
42            (IANA_ASSIGNED_LOWER..=IANA_ASSIGNED_UPPER) => AuthMethod::IanaAssigned(value),
43            PRIVATE_METHOD_LOWER..=PRIVATE_METHOD_UPPER => AuthMethod::PrivateMethods(value),
44            _ => unreachable!("u8 range handled fully"),
45        }
46    }
47    pub(crate) fn to_u8(self) -> u8 {
48        match self {
49            AuthMethod::NoAuthRequired => NO_AUTH_REQUIRED,
50            AuthMethod::Gssapi => GSSAPI,
51            AuthMethod::UsernamePassword => USERNAME_PASSWORD,
52            AuthMethod::IanaAssigned(value) => value,
53            AuthMethod::PrivateMethods(value) => value,
54            AuthMethod::NoAcceptableMethods => NO_ACCEPTABLE_METHODS,
55        }
56    }
57}