diff --git a/Cargo.toml b/Cargo.toml index 2530b67..4c043e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,12 +24,11 @@ required-features = ["tor"] [dependencies] futures = "0.3" -tokio = { version="0.2", features = ["io-util", "stream", "tcp"] } -bytes = "0.4" +tokio = { version = "0.3", features = ["io-util", "net"] } either = "1" thiserror = "1.0" [dev-dependencies] -tokio = { version = "0.2", features = ["io-util", "rt-threaded", "uds", "dns"] } +tokio = { version = "0.3", features = ["io-util", "rt-multi-thread", "net"] } once_cell = "1.2.0" hyper = "0.13" diff --git a/src/tcp.rs b/src/tcp.rs index c0f4781..783b9fe 100644 --- a/src/tcp.rs +++ b/src/tcp.rs @@ -14,7 +14,7 @@ use std::{ pin::Pin, }; use tokio::{ - io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}, + io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf}, net::TcpStream, }; @@ -671,11 +671,7 @@ where S: AsyncRead + AsyncWrite + Unpin impl AsyncRead for Socks5Stream where T: AsyncRead + Unpin { - unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit]) -> bool { - AsyncRead::prepare_uninitialized_buffer(&self.socket, buf) - } - - fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll> { + fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll> { AsyncRead::poll_read(Pin::new(&mut self.socket), cx, buf) } } diff --git a/tests/common.rs b/tests/common.rs index 2c2d887..171a6e1 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1,4 +1,3 @@ -use futures::{future, StreamExt}; use once_cell::sync::OnceCell; use std::{ io::{Read, Write}, @@ -22,21 +21,14 @@ pub const ECHO_SERVER_ADDR: &'static str = "localhost:10007"; pub const MSG: &[u8] = b"hello"; pub async fn echo_server() -> Result<()> { - let mut listener = TcpListener::bind(&SocketAddr::from(([0, 0, 0, 0], 10007))).await?; - listener - .incoming() - .for_each(|tcp_stream| { - if let Ok(mut stream) = tcp_stream { - tokio::spawn(async move { - let (mut reader, mut writer) = stream.split(); - copy(&mut reader, &mut writer).await.unwrap(); - }); - } - - future::ready(()) - }) - .await; - Ok(()) + let listener = TcpListener::bind(&SocketAddr::from(([0, 0, 0, 0], 10007))).await?; + loop { + let (mut stream, _) = listener.accept().await?; + tokio::spawn(async move { + let (mut reader, mut writer) = stream.split(); + copy(&mut reader, &mut writer).await.unwrap(); + }); + } } pub async fn reply_response(mut socket: Socks5Stream) -> Result<[u8; 5]> { diff --git a/tests/long_username_password_auth.rs b/tests/long_username_password_auth.rs index 344bf81..ec83e95 100644 --- a/tests/long_username_password_auth.rs +++ b/tests/long_username_password_auth.rs @@ -8,7 +8,7 @@ use tokio_socks::{ #[test] fn connect_long_username_password() -> Result<()> { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); let conn = runtime.block_on(Socks5Stream::connect_with_password( PROXY_ADDR, ECHO_SERVER_ADDR, "mylonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglogin", "longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongpassword"))?; @@ -18,7 +18,7 @@ fn connect_long_username_password() -> Result<()> { #[test] fn bind_long_username_password() -> Result<()> { let bind = { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); runtime.block_on(Socks5Listener::bind_with_password( PROXY_ADDR, ECHO_SERVER_ADDR, @@ -31,7 +31,7 @@ fn bind_long_username_password() -> Result<()> { #[test] fn connect_with_socket_long_username_password() -> Result<()> { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); let socket = runtime.block_on(connect_unix())?; let conn = runtime.block_on(Socks5Stream::connect_with_password_and_socket( socket, ECHO_SERVER_ADDR, "mylonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglogin", @@ -42,7 +42,7 @@ fn connect_with_socket_long_username_password() -> Result<()> { #[test] fn bind_with_socket_long_username_password() -> Result<()> { let bind = { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); let socket = runtime.block_on(connect_unix())?; runtime.block_on(Socks5Listener::bind_with_password_and_socket( socket, diff --git a/tests/no_auth.rs b/tests/no_auth.rs index c0fc581..ceca550 100644 --- a/tests/no_auth.rs +++ b/tests/no_auth.rs @@ -9,7 +9,7 @@ use tokio_socks::{ #[test] fn connect_no_auth() -> Result<()> { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); let conn = runtime.block_on(Socks5Stream::connect(PROXY_ADDR, ECHO_SERVER_ADDR))?; runtime.block_on(test_connect(conn)) } @@ -17,7 +17,7 @@ fn connect_no_auth() -> Result<()> { #[test] fn bind_no_auth() -> Result<()> { let bind = { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); runtime.block_on(Socks5Listener::bind(PROXY_ADDR, ECHO_SERVER_ADDR)) }?; test_bind(bind) @@ -25,7 +25,7 @@ fn bind_no_auth() -> Result<()> { #[test] fn connect_with_socket_no_auth() -> Result<()> { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); let socket = runtime.block_on(connect_unix())?; let conn = runtime.block_on(Socks5Stream::connect_with_socket(socket, ECHO_SERVER_ADDR))?; runtime.block_on(test_connect(conn)) @@ -34,7 +34,7 @@ fn connect_with_socket_no_auth() -> Result<()> { #[test] fn bind_with_socket_no_auth() -> Result<()> { let bind = { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); let socket = runtime.block_on(connect_unix())?; runtime.block_on(Socks5Listener::bind_with_socket(socket, ECHO_SERVER_ADDR)) }?; diff --git a/tests/username_auth.rs b/tests/username_auth.rs index 3694772..3f53e1f 100644 --- a/tests/username_auth.rs +++ b/tests/username_auth.rs @@ -8,7 +8,7 @@ use tokio_socks::{ #[test] fn connect_username_auth() -> Result<()> { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); let conn = runtime.block_on(Socks5Stream::connect_with_password( PROXY_ADDR, ECHO_SERVER_ADDR, @@ -21,7 +21,7 @@ fn connect_username_auth() -> Result<()> { #[test] fn bind_username_auth() -> Result<()> { let bind = { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); runtime.block_on(Socks5Listener::bind_with_password( PROXY_ADDR, ECHO_SERVER_ADDR, @@ -34,7 +34,7 @@ fn bind_username_auth() -> Result<()> { #[test] fn connect_with_socket_username_auth() -> Result<()> { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); let socket = runtime.block_on(connect_unix())?; let conn = runtime.block_on(Socks5Stream::connect_with_password_and_socket( socket, @@ -48,7 +48,7 @@ fn connect_with_socket_username_auth() -> Result<()> { #[test] fn bind_with_socket_username_auth() -> Result<()> { let bind = { - let mut runtime = runtime().lock().unwrap(); + let runtime = runtime().lock().unwrap(); let socket = runtime.block_on(connect_unix())?; runtime.block_on(Socks5Listener::bind_with_password_and_socket( socket,