clone from github.com/sticnarf/tokio-socks

This commit is contained in:
2022-03-18 01:16:47 +08:00
parent e5544214bf
commit dc264fbcb2
19 changed files with 1685 additions and 2 deletions

74
tests/common.rs Normal file
View File

@@ -0,0 +1,74 @@
use once_cell::sync::OnceCell;
use std::{
io::{Read, Write},
net::{SocketAddr, TcpStream as StdTcpStream},
sync::Mutex,
};
use tokio::{
io::{copy, split, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
net::{TcpListener, UnixStream},
runtime::Runtime,
};
use tokio_socks::{
tcp::{Socks5Listener, Socks5Stream},
Error,
Result,
};
pub const UNIX_PROXY_ADDR: &'static str = "/tmp/proxy.s";
pub const PROXY_ADDR: &'static str = "127.0.0.1:41080";
pub const ECHO_SERVER_ADDR: &'static str = "localhost:10007";
pub const MSG: &[u8] = b"hello";
pub async fn echo_server() -> Result<()> {
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]> {
socket.write_all(MSG).await?;
let mut buf = [0; 5];
socket.read_exact(&mut buf).await?;
Ok(buf)
}
pub async fn test_connect<S: AsyncRead + AsyncWrite + Unpin>(socket: Socks5Stream<S>) -> Result<()> {
let res = reply_response(socket).await?;
assert_eq!(&res[..], MSG);
Ok(())
}
pub fn test_bind<S: 'static + AsyncRead + AsyncWrite + Unpin + Send>(listener: Socks5Listener<S>) -> Result<()> {
let bind_addr = listener.bind_addr().to_owned();
runtime().lock().unwrap().spawn(async move {
let stream = listener.accept().await.unwrap();
let (mut reader, mut writer) = split(stream);
copy(&mut reader, &mut writer).await.unwrap();
});
let mut tcp = StdTcpStream::connect(bind_addr)?;
tcp.write_all(MSG)?;
let mut buf = [0; 5];
tcp.read_exact(&mut buf[..])?;
assert_eq!(&buf[..], MSG);
Ok(())
}
pub async fn connect_unix() -> Result<UnixStream> {
UnixStream::connect(UNIX_PROXY_ADDR).await.map_err(Error::Io)
}
pub fn runtime() -> &'static Mutex<Runtime> {
static RUNTIME: OnceCell<Mutex<Runtime>> = OnceCell::new();
RUNTIME.get_or_init(|| {
let runtime = Runtime::new().expect("Unable to create runtime");
runtime.spawn(async { echo_server().await.expect("Unable to bind") });
Mutex::new(runtime)
})
}

35
tests/integration_tests.sh Executable file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env bash
set -x
dir="$(dirname "$(which "$0")")"
SOCK="/tmp/proxy.s"
PROXY_HOST="127.0.0.1:41080"
#socat tcp-listen:10007,fork exec:cat &
#echo $! > /tmp/socat-test.pid
if test -z "$@"; then
list="no_auth username_auth long_username_password_auth"
else
list="$@"
fi
socat UNIX-LISTEN:${SOCK},reuseaddr,fork TCP:${PROXY_HOST} &
for test in ${list}; do
3proxy ${dir}/${test}.cfg
sleep 1
cargo test --test ${test} -- --test-threads 1
test_exit_code=$?
pkill -F /tmp/3proxy-test.pid
if test "$test_exit_code" -ne 0; then
break
fi
done
#pkill -F /tmp/socat-test.pid
exit ${test_exit_code}

View File

@@ -0,0 +1,6 @@
daemon
users mylonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglogin:CL:longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongpassword
pidfile /tmp/3proxy-test.pid
auth strong
allow mylonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglogin
socks -p41080

View File

@@ -0,0 +1,55 @@
mod common;
use common::{connect_unix, runtime, test_bind, test_connect, ECHO_SERVER_ADDR, PROXY_ADDR};
use tokio_socks::{
tcp::{Socks5Listener, Socks5Stream},
Result,
};
#[test]
fn connect_long_username_password() -> Result<()> {
let runtime = runtime().lock().unwrap();
let conn = runtime.block_on(Socks5Stream::connect_with_password(
PROXY_ADDR, ECHO_SERVER_ADDR, "mylonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglogin",
"longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongpassword"))?;
runtime.block_on(test_connect(conn))
}
#[test]
fn bind_long_username_password() -> Result<()> {
let bind = {
let runtime = runtime().lock().unwrap();
runtime.block_on(Socks5Listener::bind_with_password(
PROXY_ADDR,
ECHO_SERVER_ADDR,
"mylonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglogin",
"longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongpassword"
))
}?;
test_bind(bind)
}
#[test]
fn connect_with_socket_long_username_password() -> Result<()> {
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",
"longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongpassword"))?;
runtime.block_on(test_connect(conn))
}
#[test]
fn bind_with_socket_long_username_password() -> Result<()> {
let bind = {
let runtime = runtime().lock().unwrap();
let socket = runtime.block_on(connect_unix())?;
runtime.block_on(Socks5Listener::bind_with_password_and_socket(
socket,
ECHO_SERVER_ADDR,
"mylonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglogin",
"longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglongpassword"
))
}?;
test_bind(bind)
}

4
tests/no_auth.cfg Normal file
View File

@@ -0,0 +1,4 @@
daemon
pidfile /tmp/3proxy-test.pid
auth none
socks -p41080

42
tests/no_auth.rs Normal file
View File

@@ -0,0 +1,42 @@
mod common;
use crate::common::{runtime, test_bind};
use common::{connect_unix, test_connect, ECHO_SERVER_ADDR, PROXY_ADDR};
use tokio_socks::{
tcp::{Socks5Listener, Socks5Stream},
Result,
};
#[test]
fn connect_no_auth() -> Result<()> {
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 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 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))
}
#[test]
fn bind_with_socket_no_auth() -> Result<()> {
let bind = {
let runtime = runtime().lock().unwrap();
let socket = runtime.block_on(connect_unix())?;
runtime.block_on(Socks5Listener::bind_with_socket(socket, ECHO_SERVER_ADDR))
}?;
test_bind(bind)
}

6
tests/username_auth.cfg Normal file
View File

@@ -0,0 +1,6 @@
daemon
users mylogin:CL:mypassword
pidfile /tmp/3proxy-test.pid
auth strong
allow mylogin
socks -p41080

61
tests/username_auth.rs Normal file
View File

@@ -0,0 +1,61 @@
mod common;
use common::{connect_unix, runtime, test_bind, test_connect, ECHO_SERVER_ADDR, PROXY_ADDR};
use tokio_socks::{
tcp::{Socks5Listener, Socks5Stream},
Result,
};
#[test]
fn connect_username_auth() -> Result<()> {
let runtime = runtime().lock().unwrap();
let conn = runtime.block_on(Socks5Stream::connect_with_password(
PROXY_ADDR,
ECHO_SERVER_ADDR,
"mylogin",
"mypassword",
))?;
runtime.block_on(test_connect(conn))
}
#[test]
fn bind_username_auth() -> Result<()> {
let bind = {
let runtime = runtime().lock().unwrap();
runtime.block_on(Socks5Listener::bind_with_password(
PROXY_ADDR,
ECHO_SERVER_ADDR,
"mylogin",
"mypassword",
))
}?;
test_bind(bind)
}
#[test]
fn connect_with_socket_username_auth() -> Result<()> {
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,
"mylogin",
"mypassword",
))?;
runtime.block_on(test_connect(conn))
}
#[test]
fn bind_with_socket_username_auth() -> Result<()> {
let bind = {
let runtime = runtime().lock().unwrap();
let socket = runtime.block_on(connect_unix())?;
runtime.block_on(Socks5Listener::bind_with_password_and_socket(
socket,
ECHO_SERVER_ADDR,
"mylogin",
"mypassword",
))
}?;
test_bind(bind)
}