socks5_impl/protocol/handshake/password_method/
mod.rs

1mod request;
2mod response;
3
4pub use self::{
5    request::Request,
6    response::{Response, Status},
7};
8
9pub const SUBNEGOTIATION_VERSION: u8 = 0x01;
10
11/// Required for a username + password authentication.
12#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
13#[derive(Default, Debug, Eq, PartialEq, Clone, Hash)]
14pub struct UserKey {
15    pub username: String,
16    pub password: String,
17}
18
19impl std::fmt::Display for UserKey {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        use percent_encoding::{NON_ALPHANUMERIC, percent_encode};
22        match (self.username.is_empty(), self.password.is_empty()) {
23            (true, true) => write!(f, ""),
24            (true, false) => write!(f, ":{}", percent_encode(self.password.as_bytes(), NON_ALPHANUMERIC)),
25            (false, true) => write!(f, "{}", percent_encode(self.username.as_bytes(), NON_ALPHANUMERIC)),
26            (false, false) => {
27                let username = percent_encode(self.username.as_bytes(), NON_ALPHANUMERIC).to_string();
28                let password = percent_encode(self.password.as_bytes(), NON_ALPHANUMERIC).to_string();
29                write!(f, "{username}:{password}")
30            }
31        }
32    }
33}
34
35impl UserKey {
36    /// Constructs `UserKey` with the specified username and a password.
37    pub fn new<U, P>(username: U, password: P) -> Self
38    where
39        U: Into<String>,
40        P: Into<String>,
41    {
42        Self {
43            username: username.into(),
44            password: password.into(),
45        }
46    }
47
48    pub fn username_arr(&self) -> Vec<u8> {
49        self.username.as_bytes().to_vec()
50    }
51
52    pub fn password_arr(&self) -> Vec<u8> {
53        self.password.as_bytes().to_vec()
54    }
55}
56
57#[test]
58fn test_user_key() {
59    let user_key = UserKey::new("username", "pass@word");
60    assert_eq!(user_key.to_string(), "username:pass%40word");
61    let user_key = UserKey::new("username", "");
62    assert_eq!(user_key.to_string(), "username");
63    let user_key = UserKey::new("", "password");
64    assert_eq!(user_key.to_string(), ":password");
65    let user_key = UserKey::new("", "");
66    assert_eq!(user_key.to_string(), "");
67}