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