socks5_impl/protocol/handshake/
response.rs1#[cfg(feature = "tokio")]
2use crate::AsyncStreamOperation;
3use crate::{
4 StreamOperation,
5 protocol::{AuthMethod, Version},
6};
7#[cfg(feature = "tokio")]
8use tokio::io::{AsyncRead, AsyncReadExt};
9
10#[derive(Clone, Debug)]
20pub struct Response {
21 pub method: AuthMethod,
22}
23
24impl Response {
25 pub fn new(method: AuthMethod) -> Self {
26 Self { method }
27 }
28}
29
30impl StreamOperation for Response {
31 fn retrieve_from_stream<R: std::io::Read>(r: &mut R) -> std::io::Result<Self> {
32 let mut ver = [0; 1];
33 r.read_exact(&mut ver)?;
34 let ver = Version::try_from(ver[0])?;
35
36 if ver != Version::V5 {
37 let err = format!("Unsupported SOCKS version {0:#x}", u8::from(ver));
38 return Err(std::io::Error::new(std::io::ErrorKind::Unsupported, err));
39 }
40
41 let mut method = [0; 1];
42 r.read_exact(&mut method)?;
43 let method = AuthMethod::from(method[0]);
44
45 Ok(Self { method })
46 }
47
48 fn write_to_buf<B: bytes::BufMut>(&self, buf: &mut B) {
49 buf.put_u8(Version::V5.into());
50 buf.put_u8(u8::from(self.method));
51 }
52
53 fn len(&self) -> usize {
54 2
55 }
56}
57
58#[cfg(feature = "tokio")]
59#[async_trait::async_trait]
60impl AsyncStreamOperation for Response {
61 async fn retrieve_from_async_stream<R>(r: &mut R) -> std::io::Result<Self>
62 where
63 R: AsyncRead + Unpin + Send + ?Sized,
64 {
65 let ver = Version::try_from(r.read_u8().await?)?;
66
67 if ver != Version::V5 {
68 let err = format!("Unsupported SOCKS version {0:#x}", u8::from(ver));
69 return Err(std::io::Error::new(std::io::ErrorKind::Unsupported, err));
70 }
71
72 let method = AuthMethod::from(r.read_u8().await?);
73
74 Ok(Self { method })
75 }
76}