I try to implement a socks5 server using my own traits and the following code overflows the stack for some reason trying to read just 2 bytes
pub async fn handshake<T>(mut rw: T) -> Result<(SocketAddr, UnamePasswd), SocksError>
where
T: SimpleAsyncWrite + SimpleAsyncRead + Unpin + Sync + Send,
{
Self::check_auth_methods(&mut rw).await?;
let (uname, passwd) = Self::uname_passwd(&mut rw).await?;
let addr = Self::get_connection_addr(&mut rw).await?;
Ok((addr, UnamePasswd { uname, passwd }))
}
async fn check_auth_methods<T>(mut rw: T) -> Result<(), SocksError>
where
T: SimpleAsyncWrite + SimpleAsyncRead + Unpin,
{
//{{{
let mut ver_nmethods = [0u8; 2];
dbg!("here") //prints
rw.read(&mut ver_nmethods).await?;//overflows
if ver_nmethods[0] != 0x05 {
return Err(SocksError::InvalidVersion(ver_nmethods[0]));
}
let mut methods = vec![0; ver_nmethods[1].into()];
rw.read(&mut methods).await?;
methods
.iter()
.find(|m| **m == 0x02) //uname password auth 0x02
.ok_or(SocksError::NoUnamePass)?;
rw.write(&[0x05, 0x02]).await?;
Ok(())
} //}}}
#[async_trait]
pub trait SimpleAsyncRead {
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>;
}
#[async_trait]
pub trait SimpleAsyncWrite {
async fn write(&mut self, buf: &[u8]) -> io::Result<()>;
}
#[async_trait]
impl<T: SimpleAsyncRead + Sync + Send> SimpleAsyncRead for &mut T {
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
SimpleAsyncRead::read(self, buf).await
}
}
#[async_trait]
impl<T: SimpleAsyncWrite + Sync + Send> SimpleAsyncWrite for &mut T {
async fn write(&mut self, buf: &[u8]) -> io::Result<()> {
SimpleAsyncWrite::write(self, buf).await
}
}
#[async_trait]
impl SimpleAsyncWrite for TcpStream {
async fn write(&mut self, buf: &[u8]) -> io::Result<()> {
self.write_all(buf).await
}
}
#[async_trait]
impl SimpleAsyncRead for TcpStream {
async fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.read_exact(buf).await
}
}