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