use crate::protocol::{Address, Reply, SOCKS_VERSION};
#[cfg(feature = "tokio")]
use std::io::{Error, ErrorKind, Result};
#[cfg(feature = "tokio")]
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
#[derive(Clone, Debug)]
pub struct Response {
pub reply: Reply,
pub address: Address,
}
impl Response {
pub fn new(reply: Reply, address: Address) -> Self {
Self { reply, address }
}
#[cfg(feature = "tokio")]
pub async fn from_stream<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
let ver = r.read_u8().await?;
if ver != SOCKS_VERSION {
return Err(Error::new(
ErrorKind::Unsupported,
format!("Unsupported SOCKS version {0:#x}", ver),
));
}
let mut buf = [0; 2];
r.read_exact(&mut buf).await?;
let reply = Reply::try_from(buf[0])?;
let address = Address::from_stream(r).await?;
Ok(Self { reply, address })
}
#[cfg(feature = "tokio")]
pub async fn write_to<W: AsyncWrite + Unpin>(&self, w: &mut W) -> Result<()> {
let mut buf = bytes::BytesMut::with_capacity(self.serialized_len());
self.write_to_buf(&mut buf);
w.write_all(&buf).await
}
pub fn write_to_buf<B: bytes::BufMut>(&self, buf: &mut B) {
buf.put_u8(SOCKS_VERSION);
buf.put_u8(u8::from(self.reply));
buf.put_u8(0x00);
self.address.write_to_buf(buf);
}
pub fn serialized_len(&self) -> usize {
3 + self.address.serialized_len()
}
}