socks5_impl/protocol/handshake/password_method/
mod.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
64
65
66
67
68
mod request;
mod response;

use serde::{Deserialize, Serialize};

pub use self::{
    request::Request,
    response::{Response, Status},
};

pub const SUBNEGOTIATION_VERSION: u8 = 0x01;

/// Required for a username + password authentication.
#[derive(Default, Debug, Eq, PartialEq, Clone, Hash, Deserialize, Serialize)]
pub struct UserKey {
    pub username: String,
    pub password: String,
}

impl std::fmt::Display for UserKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
        match (self.username.is_empty(), self.password.is_empty()) {
            (true, true) => write!(f, ""),
            (true, false) => write!(f, ":{}", percent_encode(self.password.as_bytes(), NON_ALPHANUMERIC)),
            (false, true) => write!(f, "{}", percent_encode(self.username.as_bytes(), NON_ALPHANUMERIC)),
            (false, false) => {
                let username = percent_encode(self.username.as_bytes(), NON_ALPHANUMERIC).to_string();
                let password = percent_encode(self.password.as_bytes(), NON_ALPHANUMERIC).to_string();
                write!(f, "{}:{}", username, password)
            }
        }
    }
}

impl UserKey {
    /// Constructs `UserKey` with the specified username and a password.
    pub fn new<U, P>(username: U, password: P) -> Self
    where
        U: Into<String>,
        P: Into<String>,
    {
        Self {
            username: username.into(),
            password: password.into(),
        }
    }

    pub fn username_arr(&self) -> Vec<u8> {
        self.username.as_bytes().to_vec()
    }

    pub fn password_arr(&self) -> Vec<u8> {
        self.password.as_bytes().to_vec()
    }
}

#[test]
fn test_user_key() {
    let user_key = UserKey::new("username", "pass@word");
    assert_eq!(user_key.to_string(), "username:pass%40word");
    let user_key = UserKey::new("username", "");
    assert_eq!(user_key.to_string(), "username");
    let user_key = UserKey::new("", "password");
    assert_eq!(user_key.to_string(), ":password");
    let user_key = UserKey::new("", "");
    assert_eq!(user_key.to_string(), "");
}