Skip to content

Commit

Permalink
fix: small bug + test for bypass
Browse files Browse the repository at this point in the history
  • Loading branch information
erikziyunchi committed Nov 12, 2023
1 parent 2bd0e6c commit 664db3e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 18 deletions.
8 changes: 4 additions & 4 deletions crates/wasm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use super::*;
// A Config currently contains the local + remote ip & port
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
pub local_address: String,
pub local_port: u32,
pub remote_address: String,
pub remote_port: u32,
pub local_address: String,
pub local_port: u32,
pub bypass: bool,
}

Expand All @@ -20,10 +20,10 @@ impl Default for Config {
impl Config {
pub fn new() -> Self {
Config {
local_address: String::from("127.0.0.1"),
local_port: 8080,
remote_address: String::from("example.com"),
remote_port: 8082,
local_address: String::from("127.0.0.1"),
local_port: 8080,
bypass: false,
}
}
Expand Down
22 changes: 14 additions & 8 deletions examples/water_bins/ss_client_wasm_v1/src/water.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ pub fn _process_config(fd: i32) {
}
};

let mut global_dialer = match DIALER.lock() {
Ok(dialer) => dialer,
let mut global_conn = match CONN.lock() {
Ok(conn) => conn,
Err(e) => {
eprintln!("[WASM] > ERROR: {}", e);
return;
}
};

// global_dialer.file_conn.config = config.clone();
global_dialer.config = config;
global_conn.config = config;

info!("[WASM] > _process_config: {:?}", global_conn.config);
}
Err(e) => {
eprintln!(
Expand All @@ -60,15 +61,15 @@ pub fn _process_config(fd: i32) {
/// WASM Entry point here
#[export_name = "v1_listen"]
fn client_start() {
let global_dialer = match DIALER.lock() {
Ok(dialer) => dialer,
let bypass = match CONN.lock() {
Ok(conn) => conn.config.bypass,
Err(e) => {
eprintln!("[WASM] > ERROR: {}", e);
return;
}
};

_start_listen(global_dialer.config.bypass).unwrap();
_start_listen(bypass).unwrap();
}

#[tokio::main(flavor = "current_thread")]
Expand Down Expand Up @@ -189,9 +190,9 @@ async fn _connect_bypass(
}

pub fn _dial_remote(target: &Address) -> Result<TcpStream, std::io::Error> {
// NOTE: dial to target directly
let mut tcp_dialer = Dialer::new();

// NOTE: only support ip:port for now, add DNS resolver helper from Host later
match target {
Address::SocketAddress(addr) => {
tcp_dialer.config.remote_address = addr.ip().to_string();
Expand Down Expand Up @@ -243,6 +244,11 @@ pub fn _listener_creation() -> Result<i32, std::io::Error> {
}
};

info!(
"[WASM] creating listener at {}:{}",
global_conn.config.local_address, global_conn.config.local_port
);

// FIXME: hardcoded the filename for now, make it a config later
let stream = StreamConfigV1::init(
global_conn.config.local_address.clone(),
Expand Down
Binary file modified examples/water_bins/ss_client_wasm_v1/ss_client_wasm.wasm
Binary file not shown.
Binary file modified tests/test_wasm/ss_client_wasm.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion tests/tests/spinning_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ fn test_cross_lang_wasm_relay() -> Result<(), Box<dyn std::error::Error>> {
"remote_address": "127.0.0.1",
"remote_port": 8088,
"local_address": "127.0.0.1",
"local_port": 8080
"local_port": 8080,
"bypass": false
}
"#;
// Create a directory inside of `std::env::temp_dir()`.
Expand Down
119 changes: 114 additions & 5 deletions tests/tests/ss_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ use water::*;

use tracing::Level;

use tempfile::tempdir;

use std::thread;
use std::{
net::{SocketAddr, ToSocketAddrs},
fs::File,
io::{Error, ErrorKind, Read, Write},
net::{IpAddr, SocketAddr, ToSocketAddrs},
str,
};
use tokio::{
Expand Down Expand Up @@ -113,7 +117,7 @@ impl Socks5TestServer {
// "#;

#[tokio::test]
async fn wasm_managed_shadowsocks_async() {
async fn wasm_managed_shadowsocks_async() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();

// ==== setup official Shadowsocks server ====
Expand All @@ -123,14 +127,32 @@ async fn wasm_managed_shadowsocks_async() {
const PASSWORD: &str = "Test!23";
const METHOD: CipherKind = CipherKind::CHACHA20_POLY1305;

let cfg_str = r#"
{
"remote_address": "127.0.0.1",
"remote_port": 8088,
"local_address": "127.0.0.1",
"local_port": 8080,
"bypass": false
}
"#;
// Create a directory inside of `std::env::temp_dir()`.
let dir = tempdir()?;
let file_path = dir.path().join("temp-config.txt");
let mut file = File::create(&file_path)?;
writeln!(file, "{}", cfg_str)?;

let svr = Socks5TestServer::new(SERVER_ADDR, LOCAL_ADDR, PASSWORD, METHOD, false);
svr.run().await;

// ==== setup WASM Shadowsocks client ====
let conf = config::WATERConfig::init(
String::from("./test_wasm/ss_client_wasm.wasm"),
String::from("v1_listen"),
String::from("./test_data/config.json"),
// Currently using a temp file to pass config to WASM client
// can be easily configed here -- but can also use config.json
String::from(file_path.to_string_lossy()),
// String::from("./test_data/config.json"),
config::WaterBinType::Runner,
true,
)
Expand Down Expand Up @@ -167,18 +189,103 @@ async fn wasm_managed_shadowsocks_async() {

let http_status = b"HTTP/1.0 200 OK\r\n";
assert!(buf.starts_with(http_status));

Ok(())
}

#[tokio::test]
async fn wasm_managed_shadowsocks_bypass_async() -> Result<(), Box<dyn std::error::Error>> {
let cfg_str = r#"
{
"remote_address": "127.0.0.1",
"remote_port": 0,
"local_address": "127.0.0.1",
"local_port": 8888,
"bypass": true
}
"#;
// Create a directory inside of `std::env::temp_dir()`.
let dir = tempdir()?;
let file_path = dir.path().join("temp-config.txt");
let mut file = File::create(&file_path)?;
writeln!(file, "{}", cfg_str)?;

// ==== setup WASM Shadowsocks client ====
let conf = config::WATERConfig::init(
String::from("./test_wasm/ss_client_wasm.wasm"),
String::from("v1_listen"),
String::from(file_path.to_string_lossy()),
config::WaterBinType::Runner,
true,
)
.unwrap();

let mut water_client = runtime::client::WATERClient::new(conf).unwrap();

// ==== spawn a thread to run WASM Shadowsocks client ====
thread::spawn(move || {
water_client.execute().unwrap();
});

// Give some time for the WASM client to start
thread::sleep(Duration::from_millis(1000));

let wasm_ss_client_addr = SocketAddr::new("127.0.0.1".parse().unwrap(), 8888);

// ==== test WASM Shadowsocks client ====
// currently only support connect by ip,
// this is the ip of detectportal.firefox.com
let ip: IpAddr = "143.244.220.150".parse().unwrap();
let port = 80;

let mut c = Socks5TcpClient::connect(
Address::SocketAddress(SocketAddr::new(ip, port)),
wasm_ss_client_addr,
)
.await
.unwrap();

let req = b"GET /success.txt HTTP/1.0\r\nHost: detectportal.firefox.com\r\nAccept: */*\r\n\r\n";
c.write_all(req).await.unwrap();
c.flush().await.unwrap();

let mut r = BufReader::new(c);

let mut buf = Vec::new();
r.read_until(b'\n', &mut buf).await.unwrap();

let http_status = b"HTTP/1.0 200 OK\r\n";
assert!(buf.starts_with(http_status));

Ok(())
}

// Here is a test that runs the ss_client that has to be ended with signal
// #[test]
fn execute_wasm_shadowsocks_client() {
fn execute_wasm_shadowsocks_client() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();

let cfg_str = r#"
{
"remote_address": "138.197.211.159",
"remote_port": 5201,
"local_address": "127.0.0.1",
"local_port": 8080,
"bypass": true
}
"#;

// Create a directory inside of `std::env::temp_dir()`.
let dir = tempdir()?;
let file_path = dir.path().join("temp-config.txt");
let mut file = File::create(&file_path)?;
writeln!(file, "{}", cfg_str)?;

// ==== setup WASM Shadowsocks client ====
let conf = config::WATERConfig::init(
String::from("./test_wasm/ss_client_wasm.wasm"),
String::from("v1_listen"),
String::from("./test_data/config.json"),
String::from(file_path.to_string_lossy()),
config::WaterBinType::Runner,
false,
)
Expand All @@ -187,4 +294,6 @@ fn execute_wasm_shadowsocks_client() {
let mut water_client = runtime::client::WATERClient::new(conf).unwrap();

water_client.execute().unwrap();

Ok(())
}

0 comments on commit 664db3e

Please sign in to comment.