Skip to content

Commit

Permalink
Merge pull request #27 from messense/tokio-0-3
Browse files Browse the repository at this point in the history
Upgrade tokio to 0.3
  • Loading branch information
sticnarf authored Oct 27, 2020
2 parents 002a16d + 1b46320 commit 415e690
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 37 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 2 additions & 6 deletions src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
pin::Pin,
};
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf},
net::TcpStream,
};

Expand Down Expand Up @@ -671,11 +671,7 @@ where S: AsyncRead + AsyncWrite + Unpin
impl<T> AsyncRead for Socks5Stream<T>
where T: AsyncRead + Unpin
{
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
AsyncRead::prepare_uninitialized_buffer(&self.socket, buf)
}

fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<io::Result<()>> {
AsyncRead::poll_read(Pin::new(&mut self.socket), cx, buf)
}
}
Expand Down
24 changes: 8 additions & 16 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use futures::{future, StreamExt};
use once_cell::sync::OnceCell;
use std::{
io::{Read, Write},
Expand All @@ -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<S: AsyncRead + AsyncWrite + Unpin>(mut socket: Socks5Stream<S>) -> Result<[u8; 5]> {
Expand Down
8 changes: 4 additions & 4 deletions tests/long_username_password_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))?;
Expand All @@ -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,
Expand All @@ -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",
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tests/no_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ 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))
}

#[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)
}

#[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))
Expand All @@ -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))
}?;
Expand Down
8 changes: 4 additions & 4 deletions tests/username_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 415e690

Please sign in to comment.