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 3 commits
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
49 changes: 48 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Lint
run: cargo clippy --workspace --all-targets --verbose --all-features

build:
test_amd64:
name: Test and Build WASM
needs: format
# needs: [format, lint]
Expand Down Expand Up @@ -68,5 +68,52 @@ jobs:
env:
RUSTFLAGS: --cfg tokio_unstable

- name: Test
run: cargo test --verbose --workspace --all-features

test_linux-non-amd64:
name: rust (linux/${{ matrix.arch }})
needs: test_amd64
gaukas marked this conversation as resolved.
Show resolved Hide resolved
strategy:
matrix:
rust: [ "stable" ] # specify the Rust versions you want to test
arch: [ "arm64", "riscv64" ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- name: Install QEMU
uses: docker/setup-qemu-action@v3
- name: Build wasm32-wasi Target
run: bash ./scripts/build_wasm_targets.sh
env:
RUSTFLAGS: --cfg tokio_unstable
- name: Test
run: cargo test --verbose --workspace --all-features
erikziyunchi marked this conversation as resolved.
Show resolved Hide resolved

test_macos-arm64:
name: rust (macos/arm64)
needs: test_amd64
strategy:
matrix:
rust: [ "stable" ]
runs-on: flyci-macos-large-latest-m1 # generously provided by www.flyci.net
steps:
- uses: actions/checkout@v4
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- name: Build wasm32-wasi Target
run: bash ./scripts/build_wasm_targets.sh
env:
RUSTFLAGS: --cfg tokio_unstable
- name: Test
run: cargo test --verbose --workspace --all-features
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
File renamed without changes.
4 changes: 2 additions & 2 deletions crates/wasm/Cargo.toml → crates/watm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "water-wasm"
name = "water-watm"
version = "0.1.0"
authors.workspace = true
description.workspace = true
edition.workspace = true

[lib]
name = "water_wasm"
name = "water_watm"
path = "src/lib.rs"
crate-type = ["cdylib", "lib"]

Expand Down
4 changes: 1 addition & 3 deletions crates/wasm/src/config.rs → crates/watm/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
#[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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions crates/wasm_v0/Cargo.toml → crates/watm_v0/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "water-wasm-v0"
name = "water-watm-v0"
version = "0.1.0"
authors.workspace = true
description.workspace = true
edition.workspace = true

[lib]
name = "water_wasm_v0"
name = "water_watm_v0"
path = "src/lib.rs"
crate-type = ["cdylib", "lib"]

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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/clients/cli/demo_wasm/plain.wasm
Binary file not shown.
Binary file removed examples/clients/cli/demo_wasm/proxy.wasm
Binary file not shown.
Binary file added examples/clients/cli/demo_wasm/reverse.wasm
Binary file not shown.
Binary file modified examples/clients/cli/demo_wasm/ss_client_wasm.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/water_bins/echo_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ url = { version = "2.2.2", features = ["serde"] }
libc = "0.2.147"

# water wasm lib import
water-wasm = { path = "../../../crates/wasm/", version = "0.1.0" }
water-watm = { path = "../../../crates/watm/", version = "0.1.0" }
Binary file modified examples/water_bins/echo_client/echo_client.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/water_bins/echo_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bincode::{self};
use lazy_static::lazy_static;
use tracing::{info, Level};

use water_wasm::*;
use water_watm::*;

pub mod async_socks5_listener;

Expand Down
2 changes: 1 addition & 1 deletion examples/water_bins/plain_v0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ tracing = "0.1"
tracing-subscriber = "0.3.17"

# water wasm lib import
water-wasm-v0 = { path = "../../../crates/wasm_v0/", version = "0.1.0" }
water-watm-v0 = { path = "../../../crates/watm_v0/", version = "0.1.0" }
Binary file modified examples/water_bins/plain_v0/plain.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/water_bins/plain_v0/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use water_wasm_v0::*;
use water_watm_v0::*;

use lazy_static::lazy_static;
use std::ops::DerefMut;
Expand Down
5 changes: 4 additions & 1 deletion examples/water_bins/reverse_v0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.33.0", default-features = false, features = ["fs", "net", "rt", "macros", "io-util", "io-std", "time", "sync"] }

tracing = "0.1"
tracing-subscriber = "0.3.17"

# water wasm lib import
water-wasm-v0 = { path = "../../../crates/wasm_v0/", version = "0.1.0" }
water-watm-v0 = { path = "../../../crates/watm_v0/", version = "0.1.0" }
Binary file modified examples/water_bins/reverse_v0/reverse.wasm
Binary file not shown.
10 changes: 8 additions & 2 deletions examples/water_bins/reverse_v0/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use water_wasm_v0::*;
use water_watm_v0::*;

use lazy_static::lazy_static;
use std::ops::DerefMut;
Expand All @@ -11,6 +11,8 @@ use tokio::{
};
use v0plus::ConnPair;

use tracing::Level;

const READ_BUFFER_SIZE: usize = 1024; // 1KB is shorter than common MTU but longer than common TCP MSS

lazy_static! {
Expand All @@ -28,7 +30,11 @@ pub static VERSION: i32 = v0plus::VERSION;

// version-independent API
#[export_name = "_water_init"]
pub fn _init() -> i32 {
pub fn _init(debug: bool) -> i32 {
if debug {
tracing_subscriber::fmt().with_max_level(Level::INFO).init();
}

// do all the initializing work here AND pull config from host
sleep(Duration::from_millis(10)); // sleep for 10ms
error::Error::None.i32()
Expand Down
2 changes: 1 addition & 1 deletion examples/water_bins/ss_client_wasm_v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ futures = "0.3.28"
pin-project = "1.1.2"

# water wasm lib import
water-wasm = { path = "../../../crates/wasm/", version = "0.1.0" }
water-watm = { path = "../../../crates/watm/", version = "0.1.0" }
2 changes: 1 addition & 1 deletion examples/water_bins/ss_client_wasm_v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use config::*;
use crypto_io::*;
use socks5::*;
use utils::*;
use water_wasm::*;
use water_watm::*;

// =================== SHADOWSOCKS_CRYPTO ===================
use shadowsocks_crypto::{v1::random_iv_or_salt, v1::Cipher, CipherKind};
Expand Down
Binary file modified examples/water_bins/ss_client_wasm_v1/ss_client_wasm.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion scripts/build_wasm_targets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
set -e # Exit on any error

members=(
"crates/wasm"
"crates/watm"
"crates/watm_v0"
"examples/water_bins/ss_client_wasm_v1"
"examples/water_bins/echo_client"
"examples/water_bins/plain_v0"
Expand Down
2 changes: 1 addition & 1 deletion tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ publish = false


[dev-dependencies]
water-wasm = { path = "../crates/wasm" }
water-watm = { path = "../crates/watm" }
water = { path = "../crates/water" }

tracing = "0.1"
Expand Down
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.
Binary file modified tests/test_wasm/plain.wasm
Binary file not shown.
Binary file added tests/test_wasm/reverse.wasm
Binary file not shown.
Binary file modified tests/test_wasm/ss_client_wasm.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(())
}
Loading
Loading