socks5_impl/protocol/handshake/password_method/
response.rs

1#[cfg(feature = "tokio")]
2use crate::protocol::AsyncStreamOperation;
3use crate::protocol::StreamOperation;
4#[cfg(feature = "tokio")]
5use tokio::io::{AsyncRead, AsyncReadExt};
6
7#[repr(u8)]
8#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Default)]
9pub enum Status {
10    #[default]
11    Succeeded = 0x00,
12    Failed = 0xff,
13}
14
15impl From<Status> for u8 {
16    fn from(value: Status) -> Self {
17        value as u8
18    }
19}
20
21impl TryFrom<u8> for Status {
22    type Error = std::io::Error;
23
24    fn try_from(value: u8) -> Result<Self, Self::Error> {
25        let err = format!("Invalid sub-negotiation status {value:#x}");
26        match value {
27            0x00 => Ok(Status::Succeeded),
28            0xff => Ok(Status::Failed),
29            _ => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, err)),
30        }
31    }
32}
33
34impl std::fmt::Display for Status {
35    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
36        match self {
37            Status::Succeeded => write!(f, "Succeeded"),
38            Status::Failed => write!(f, "Failed"),
39        }
40    }
41}
42
43/// SOCKS5 password handshake response
44///
45/// ```plain
46/// +-----+--------+
47/// | VER | STATUS |
48/// +-----+--------+
49/// |  1  |   1    |
50/// +-----+--------+
51/// ```
52#[derive(Clone, Debug)]
53pub struct Response {
54    pub status: Status,
55}
56
57impl Response {
58    pub fn new(status: Status) -> Self {
59        Self { status }
60    }
61}
62
63impl StreamOperation for Response {
64    fn retrieve_from_stream<R: std::io::Read>(r: &mut R) -> std::io::Result<Self> {
65        let mut ver = [0; 1];
66        r.read_exact(&mut ver)?;
67        let ver = ver[0];
68
69        if ver != super::SUBNEGOTIATION_VERSION {
70            let err = format!("Unsupported sub-negotiation version {ver:#x}");
71            return Err(std::io::Error::new(std::io::ErrorKind::Unsupported, err));
72        }
73
74        let mut status = [0; 1];
75        r.read_exact(&mut status)?;
76        let status = Status::try_from(status[0])?;
77        Ok(Self { status })
78    }
79
80    fn write_to_buf<B: bytes::BufMut>(&self, buf: &mut B) {
81        buf.put_u8(super::SUBNEGOTIATION_VERSION);
82        buf.put_u8(self.status.into());
83    }
84
85    fn len(&self) -> usize {
86        2
87    }
88}
89
90#[cfg(feature = "tokio")]
91#[async_trait::async_trait]
92impl AsyncStreamOperation for Response {
93    async fn retrieve_from_async_stream<R>(r: &mut R) -> std::io::Result<Self>
94    where
95        R: AsyncRead + Unpin + Send + ?Sized,
96    {
97        let ver = r.read_u8().await?;
98
99        if ver != super::SUBNEGOTIATION_VERSION {
100            let err = format!("Unsupported sub-negotiation version {ver:#x}");
101            return Err(std::io::Error::new(std::io::ErrorKind::Unsupported, err));
102        }
103
104        let status = Status::try_from(r.read_u8().await?)?;
105        Ok(Self { status })
106    }
107}