Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates on config, tests, and docs #24

Merged
merged 7 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/wasm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

use super::*;

/// A Config currently contains the local + remote ip & port + bypass flag
/// A Config currently contains the local + remote ip & port
erikziyunchi marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
pub remote_address: String,
pub remote_port: u32,
pub local_address: String,
pub local_port: u32,
pub bypass: bool,
}

impl Default for Config {
Expand All @@ -29,7 +28,6 @@ impl Config {
remote_port: 8082,
local_address: String::from("127.0.0.1"),
local_port: 8080,
bypass: false,
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/water/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Library docs will be availale soon on `docs.rs`, for now you can run `cargo doc
// "remote_address": "127.0.0.1",
// "remote_port": 8080,
// "local_address": "127.0.0.1",
// "local_port": 8088,
// "bypass": false
// "local_port": 8088
// }

// construct the config
Expand Down
8 changes: 7 additions & 1 deletion examples/clients/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ you should see `> CONNECTED` in the terminal of running WASM, then you can conne
{
"server": "127.0.0.1",
"server_port": 8388,
"password": "Test!23",

// match the password in the config passed into WATER-cli [remove the comment when copying]
gaukas marked this conversation as resolved.
Show resolved Hide resolved
"password": "WATERisAwesome!",
erikziyunchi marked this conversation as resolved.
Show resolved Hide resolved

// for now, this is a global variable set in the ShadowSocks WATM, will add the config for it later
// the global var is set here: https://github.com/erikziyunchi/water-rs/blob/48716579a3ff69a5de5e4f69c47ff2a80470d96d/examples/water_bins/ss_client_wasm_v1/src/lib.rs#L2
// [remove these comment lines when copying]
erikziyunchi marked this conversation as resolved.
Show resolved Hide resolved
"method": "chacha20-ietf-poly1305"
}
```
Expand Down
1 change: 1 addition & 0 deletions examples/clients/cli/demo_configs/ss_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"remote_port": 8388,
"local_address": "127.0.0.1",
"local_port": 8080,
"password": "WATERisAwesome!",
"bypass": false
}
3 changes: 1 addition & 2 deletions examples/clients/cli/demo_configs/v0_listener_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"remote_address": "127.0.0.1",
"remote_port": 8088,
"local_address": "127.0.0.1",
"local_port": 8888,
"bypass": false
"local_port": 8888
}
3 changes: 1 addition & 2 deletions examples/clients/cli/demo_configs/v0_relay_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"remote_address": "127.0.0.1",
"remote_port": 8888,
"local_address": "127.0.0.1",
"local_port": 8080,
"bypass": false
"local_port": 8080
}
Binary file modified examples/clients/cli/demo_wasm/echo_client.wasm
100644 → 100755
Binary file not shown.
Binary file modified examples/water_bins/echo_client/echo_client.wasm
Binary file not shown.
3 changes: 1 addition & 2 deletions tests/test_data/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"remote_address": "127.0.0.1",
"remote_port": 8080,
"local_address": "127.0.0.1",
"local_port": 8088,
"bypass": false
"local_port": 8088
}
Binary file modified tests/test_wasm/echo_client.wasm
Binary file not shown.
7 changes: 4 additions & 3 deletions tests/tests/echo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ fn test_echo() -> Result<(), Box<dyn std::error::Error>> {
"remote_address": "127.0.0.1",
"remote_port": 8080,
"local_address": "127.0.0.1",
"local_port": 8088,
"bypass": false
"local_port": 8088
}
"#;
// Create a directory inside of `std::env::temp_dir()`.
Expand All @@ -30,7 +29,6 @@ fn test_echo() -> Result<(), Box<dyn std::error::Error>> {

let test_message = b"hello";
let handle = std::thread::spawn(|| {
// let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
let listener = TcpListener::bind(("127.0.0.1", 8080)).unwrap();
let (mut socket, _) = listener.accept().unwrap();
let mut buf = [0; 1024];
Expand All @@ -46,6 +44,9 @@ fn test_echo() -> Result<(), Box<dyn std::error::Error>> {
});

let conf = config::WATERConfig::init(
// Source code of echo_client.wasm:
// https://github.com/erikziyunchi/water-rs/tree/main/examples/water_bins/echo_client
//
String::from("./test_wasm/echo_client.wasm"),
String::from("_water_init"),
String::from(file_path.to_string_lossy()),
Expand Down
44 changes: 4 additions & 40 deletions tests/tests/ss_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ async fn wasm_managed_shadowsocks_async() -> Result<(), Box<dyn std::error::Erro

// ==== setup WASM Shadowsocks client ====
let conf = config::WATERConfig::init(
// Source code of ss_client_wasm.wasm:
// https://github.com/erikziyunchi/water-rs/tree/main/examples/water_bins/ss_client_wasm_v1
//
String::from("./test_wasm/ss_client_wasm.wasm"),
String::from("v1_listen"),
// Currently using a temp file to pass config to WASM client
Expand Down Expand Up @@ -223,7 +226,7 @@ async fn wasm_managed_shadowsocks_bypass_async() -> Result<(), Box<dyn std::erro

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

// ==== spawn a thread to run WASM Shadowsocks client ====
// spawn a thread to run WASM Shadowsocks client
thread::spawn(move || {
water_client.execute().unwrap();
});
Expand All @@ -236,7 +239,6 @@ async fn wasm_managed_shadowsocks_bypass_async() -> Result<(), Box<dyn std::erro
// ==== test WASM Shadowsocks client ====
// currently only support connect by ip,
// get the ip of detectportal.firefox.com

let mut addrs = "detectportal.firefox.com:80".to_socket_addrs()?;
let ip = addrs
.find(|addr| addr.is_ipv4())
Expand Down Expand Up @@ -266,41 +268,3 @@ async fn wasm_managed_shadowsocks_bypass_async() -> Result<(), Box<dyn std::erro

Ok(())
}

// Here is a test that runs the ss_client that has to be ended with signal
// #[test]
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(file_path.to_string_lossy()),
config::WaterBinType::Runner,
false,
)
.unwrap();

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

water_client.execute().unwrap();

Ok(())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! This is the test file for testing the plain.wasm which is a v0_plus WATM module that has been tested with the Go engine.
//! But with the Relay mode
//! This is the test file for testing the plain.wasm in Relay mode which is a v0_plus WATM module that has been tested with the Go engine.

#![allow(dead_code)]

Expand All @@ -25,8 +24,7 @@ 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,
"bypass": false
"local_port": 8080
}
"#;
// Create a directory inside of `std::env::temp_dir()`.
Expand Down Expand Up @@ -58,8 +56,8 @@ fn test_cross_lang_wasm_relay() -> Result<(), Box<dyn std::error::Error>> {
// More details for the Go-side of running plain.wasm check here:
// https://github.com/gaukas/water/tree/master/examples/v0/plain
//
// More details for the implementation of plain.wasm check this PR:
// https://github.com/erikziyunchi/water-rs/pull/10
// Source code of plain.wasm:
// https://github.com/erikziyunchi/water-rs/tree/main/examples/water_bins/plain_v0
//
String::from("./test_wasm/plain.wasm"),
String::from("_water_worker"),
Expand Down Expand Up @@ -118,77 +116,3 @@ fn test_cross_lang_wasm_relay() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

// A test that do nothing but just spin up the relay for 20 seconds
// #[test]
fn spin_cross_lang_wasm_relay() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();

let cfg_str = r#"
{
"remote_address": "127.0.0.1",
"remote_port": 5201,
"local_address": "127.0.0.1",
"local_port": 8082,
"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 conf = config::WATERConfig::init(
// plain.wasm is in v0 and fully compatible with the Go engine
// More details for the Go-side of running plain.wasm check here:
// https://github.com/gaukas/water/tree/master/examples/v0/plain
//
// More details for the implementation of plain.wasm check this PR:
// https://github.com/erikziyunchi/water-rs/pull/10
//
String::from("./test_wasm/plain.wasm"),
String::from("_water_worker"),
String::from(file_path.to_string_lossy()),
config::WaterBinType::Relay,
true,
)
.unwrap();

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

water_client.listen().unwrap();

water_client.associate().unwrap();
water_client.cancel_with().unwrap();

let mut handle_water = water_client.run_worker().unwrap();

for _i in 0..5 {
match handle_water.join().unwrap() {
Ok(_) => {}
Err(e) => {
eprintln!("Running _water_worker ERROR: {}", e);
return Err(Box::new(Error::new(
ErrorKind::Other,
"Failed to join _water_worker thread",
)));
}
};

let mut new_water = water_client.keep_listen().unwrap();
// no need to call relay again, since relay() is also creating the listener
new_water.associate().unwrap();
new_water.cancel_with().unwrap();
handle_water = new_water.run_worker().unwrap();
}

std::thread::sleep(std::time::Duration::from_secs(20));

water_client.cancel().unwrap();

drop(file);
dir.close()?;

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ fn test_cross_lang_wasm_dialer() -> Result<(), Box<dyn std::error::Error>> {
"remote_address": "127.0.0.1",
"remote_port": 8080,
"local_address": "127.0.0.1",
"local_port": 8088,
"bypass": false
"local_port": 8088
}
"#;
// Create a directory inside of `std::env::temp_dir()`.
Expand All @@ -40,7 +39,6 @@ fn test_cross_lang_wasm_dialer() -> Result<(), Box<dyn std::error::Error>> {

let test_message = b"hello";
let handle = std::thread::spawn(|| {
// let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
let listener = TcpListener::bind(("127.0.0.1", 8080)).unwrap();
let (mut socket, _) = listener.accept().unwrap();
let mut buf = [0; 1024];
Expand All @@ -60,8 +58,8 @@ fn test_cross_lang_wasm_dialer() -> Result<(), Box<dyn std::error::Error>> {
// More details for the Go-side of running plain.wasm check here:
// https://github.com/gaukas/water/tree/master/examples/v0/plain
//
// More details for the implementation of plain.wasm check this PR:
// https://github.com/erikziyunchi/water-rs/pull/10
// Source code of plain.wasm:
// https://github.com/erikziyunchi/water-rs/tree/main/examples/water_bins/plain_v0
//
String::from("./test_wasm/plain.wasm"),
String::from("_water_worker"),
Expand Down Expand Up @@ -110,8 +108,7 @@ fn test_cross_lang_wasm_listener() -> Result<(), Box<dyn std::error::Error>> {
"remote_address": "127.0.0.1",
"remote_port": 8088,
"local_address": "127.0.0.1",
"local_port": 8082,
"bypass": false
"local_port": 8082
}
"#;
// Create a directory inside of `std::env::temp_dir()`.
Expand All @@ -123,13 +120,6 @@ fn test_cross_lang_wasm_listener() -> Result<(), Box<dyn std::error::Error>> {
let test_message = b"hello";

let conf = config::WATERConfig::init(
// plain.wasm is in v0 and fully compatible with the Go engine
// More details for the Go-side of running plain.wasm check here:
// https://github.com/gaukas/water/tree/master/examples/v0/plain
//
// More details for the implementation of plain.wasm check this PR:
// https://github.com/erikziyunchi/water-rs/pull/10
//
String::from("./test_wasm/plain.wasm"),
String::from("_water_worker"),
String::from(file_path.to_string_lossy()),
Expand Down Expand Up @@ -185,15 +175,15 @@ fn test_cross_lang_wasm_listener() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

/// Testing the listener mode but with multiple incoming connections
#[test]
fn test_cross_lang_wasm_multi_listener() -> Result<(), Box<dyn std::error::Error>> {
let cfg_str = r#"
{
"remote_address": "127.0.0.1",
"remote_port": 8088,
"local_address": "127.0.0.1",
"local_port": 10088,
"bypass": false
"local_port": 10088
}
"#;
// Create a directory inside of `std::env::temp_dir()`.
Expand All @@ -203,13 +193,6 @@ fn test_cross_lang_wasm_multi_listener() -> Result<(), Box<dyn std::error::Error
writeln!(file, "{}", cfg_str)?;

let conf = config::WATERConfig::init(
// plain.wasm is in v0 and fully compatible with the Go engine
// More details for the Go-side of running plain.wasm check here:
// https://github.com/gaukas/water/tree/master/examples/v0/plain
//
// More details for the implementation of plain.wasm check this PR:
// https://github.com/erikziyunchi/water-rs/pull/10
//
String::from("./test_wasm/plain.wasm"),
String::from("_water_worker"),
String::from(file_path.to_string_lossy()),
Expand Down
Loading