Max/rust sdk stream abstraction (#4743)

* add TcpProxyClient and TcpProxyServer abstractions to SDK 
* add single connection example
* add multi-connection example 
* add simple echo server to `tools/`: used for multi-connection example 
* update FFI toml files: switched to local imports 
* add proxy bindings to `ffi/shared`
* add proxy bindings and example to `ffi/go` 
* add note to `ffi/cpp` about lack of Proxy bindings for the moment
This commit is contained in:
mx
2024-09-24 09:29:46 +00:00
committed by GitHub
parent 3b7088aeea
commit 60fa5cfeb8
32 changed files with 2668 additions and 144 deletions
Generated
+363 -13
View File
@@ -265,6 +265,47 @@ version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711"
[[package]]
name = "askama"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b79091df18a97caea757e28cd2d5fda49c6cd4bd01ddffd7ff01ace0c0ad2c28"
dependencies = [
"askama_derive",
"askama_escape",
]
[[package]]
name = "askama_derive"
version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19fe8d6cb13c4714962c072ea496f3392015f0989b1a2847bb4b2d9effd71d83"
dependencies = [
"askama_parser",
"basic-toml",
"mime",
"mime_guess",
"proc-macro2",
"quote",
"serde",
"syn 2.0.66",
]
[[package]]
name = "askama_escape"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341"
[[package]]
name = "askama_parser"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acb1161c6b64d1c3d83108213c2a2533a342ac225aabd0bda218278c2ddb00c0"
dependencies = [
"nom",
]
[[package]]
name = "async-channel"
version = "1.9.0"
@@ -535,6 +576,15 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]]
name = "basic-toml"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "823388e228f614e9558c6804262db37960ec8821856535f5c3f59913140558f8"
dependencies = [
"serde",
]
[[package]]
name = "binascii"
version = "0.1.4"
@@ -686,7 +736,7 @@ checksum = "bc0bdbcf2078e0ba8a74e1fe0cf36f54054a04485759b61dfd60b174658e9607"
dependencies = [
"bit-vec",
"getrandom",
"siphasher",
"siphasher 1.0.1",
]
[[package]]
@@ -782,6 +832,20 @@ dependencies = [
"serde",
]
[[package]]
name = "cargo_metadata"
version = "0.15.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eee4243f1f26fc7a42710e7439c149e2b10b05472f88090acce52632f231a73a"
dependencies = [
"camino",
"cargo-platform",
"semver 1.0.23",
"serde",
"serde_json",
"thiserror",
]
[[package]]
name = "cargo_metadata"
version = "0.18.1"
@@ -2060,6 +2124,26 @@ dependencies = [
"spki",
]
[[package]]
name = "echo-server"
version = "0.1.0"
dependencies = [
"anyhow",
"bincode",
"bytecodec",
"bytes",
"dashmap",
"dirs",
"nym-sdk",
"serde",
"tokio",
"tokio-stream",
"tokio-util",
"tracing",
"tracing-subscriber",
"uuid",
]
[[package]]
name = "ed25519"
version = "2.2.3"
@@ -2357,7 +2441,7 @@ dependencies = [
"atomic 0.6.0",
"pear",
"serde",
"toml",
"toml 0.8.14",
"uncased",
"version_check",
]
@@ -2433,6 +2517,15 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8cbd1169bd7b4a0a20d92b9af7a7e0422888bd38a6f5ec29c1fd8c1558a272e"
[[package]]
name = "fs-err"
version = "2.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88a41f105fe1d5b6b34b2055e3dc59bb79b46b48b2040b9e6c7b4b5de097aa41"
dependencies = [
"autocfg",
]
[[package]]
name = "fsevent-sys"
version = "4.1.0"
@@ -2709,6 +2802,17 @@ dependencies = [
"web-sys",
]
[[package]]
name = "goblin"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d6b4de4a8eb6c46a8c77e1d3be942cb9a8bf073c22374578e5ba4b08ed0ff68"
dependencies = [
"log",
"plain",
"scroll",
]
[[package]]
name = "group"
version = "0.13.0"
@@ -4406,7 +4510,7 @@ dependencies = [
"thiserror",
"time",
"tokio",
"toml",
"toml 0.8.14",
"url",
"zeroize",
]
@@ -4669,7 +4773,7 @@ dependencies = [
"log",
"nym-network-defaults",
"serde",
"toml",
"toml 0.8.14",
"url",
]
@@ -4696,6 +4800,20 @@ dependencies = [
"tracing",
]
[[package]]
name = "nym-cpp-ffi"
version = "0.1.2"
dependencies = [
"anyhow",
"bs58",
"lazy_static",
"nym-bin-common",
"nym-ffi-shared",
"nym-sdk",
"nym-sphinx-anonymous-replies",
"tokio",
]
[[package]]
name = "nym-credential-storage"
version = "0.1.0"
@@ -4921,6 +5039,21 @@ dependencies = [
"url",
]
[[package]]
name = "nym-ffi-shared"
version = "0.2.0"
dependencies = [
"anyhow",
"bs58",
"lazy_static",
"nym-bin-common",
"nym-sdk",
"nym-sphinx-anonymous-replies",
"tokio",
"uniffi",
"uniffi_build",
]
[[package]]
name = "nym-gateway"
version = "1.1.36"
@@ -5057,6 +5190,22 @@ dependencies = [
"tracing",
]
[[package]]
name = "nym-go-ffi"
version = "0.2.0"
dependencies = [
"anyhow",
"lazy_static",
"nym-bin-common",
"nym-ffi-shared",
"nym-sdk",
"nym-sphinx-anonymous-replies",
"thiserror",
"tokio",
"uniffi",
"uniffi_build",
]
[[package]]
name = "nym-group-contract-common"
version = "0.1.0"
@@ -5290,7 +5439,7 @@ dependencies = [
"time",
"tokio",
"tokio-util",
"toml",
"toml 0.8.14",
"url",
]
@@ -5439,7 +5588,7 @@ dependencies = [
"anyhow",
"bip39",
"bs58",
"cargo_metadata",
"cargo_metadata 0.18.1",
"celes",
"clap 4.5.17",
"colored",
@@ -5470,7 +5619,7 @@ dependencies = [
"sysinfo",
"thiserror",
"tokio",
"toml",
"toml 0.8.14",
"tracing",
"url",
"zeroize",
@@ -5639,9 +5788,12 @@ version = "0.1.0"
dependencies = [
"anyhow",
"async-trait",
"bincode",
"bip39",
"bytecodec",
"bytes",
"dashmap",
"dirs",
"dotenvy",
"futures",
"hex",
@@ -5670,13 +5822,17 @@ dependencies = [
"pretty_env_logger",
"rand",
"reqwest 0.12.4",
"serde",
"tap",
"thiserror",
"tokio",
"tokio-stream",
"tokio-util",
"toml",
"toml 0.8.14",
"tracing",
"tracing-subscriber",
"url",
"uuid",
"zeroize",
]
@@ -6323,6 +6479,12 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "oneshot-uniffi"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c548d5c78976f6955d72d0ced18c48ca07030f7a1d4024529fedd7c1c01b29c"
[[package]]
name = "oorandom"
version = "11.1.3"
@@ -6726,6 +6888,12 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]]
name = "plain"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
[[package]]
name = "plotters"
version = "0.3.6"
@@ -7772,6 +7940,26 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "scroll"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04c565b551bafbef4157586fa379538366e4385d42082f255bfd96e4fe8519da"
dependencies = [
"scroll_derive",
]
[[package]]
name = "scroll_derive"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1db149f81d46d2deba7cd3c50772474707729550221e69588478ebf9ada425ae"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.66",
]
[[package]]
name = "sct"
version = "0.7.1"
@@ -8145,6 +8333,12 @@ dependencies = [
"rand_core 0.6.4",
]
[[package]]
name = "siphasher"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d"
[[package]]
name = "siphasher"
version = "1.0.1"
@@ -8419,6 +8613,12 @@ dependencies = [
"loom",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "stringprep"
version = "0.1.5"
@@ -8684,7 +8884,7 @@ dependencies = [
"serde",
"serde_json",
"tendermint 0.37.0",
"toml",
"toml 0.8.14",
"url",
]
@@ -8798,7 +8998,7 @@ dependencies = [
"thiserror",
"time",
"tokio",
"toml",
"toml 0.8.14",
"tracing",
"url",
"zeroize",
@@ -9073,6 +9273,15 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
dependencies = [
"serde",
]
[[package]]
name = "toml"
version = "0.8.14"
@@ -9554,6 +9763,138 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e"
[[package]]
name = "uniffi"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21345172d31092fd48c47fd56c53d4ae9e41c4b1f559fb8c38c1ab1685fd919f"
dependencies = [
"anyhow",
"camino",
"clap 4.5.17",
"uniffi_bindgen",
"uniffi_build",
"uniffi_core",
"uniffi_macros",
]
[[package]]
name = "uniffi_bindgen"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd992f2929a053829d5875af1eff2ee3d7a7001cb3b9a46cc7895f2caede6940"
dependencies = [
"anyhow",
"askama",
"camino",
"cargo_metadata 0.15.4",
"clap 4.5.17",
"fs-err",
"glob",
"goblin",
"heck 0.4.1",
"once_cell",
"paste",
"serde",
"toml 0.5.11",
"uniffi_meta",
"uniffi_testing",
"uniffi_udl",
]
[[package]]
name = "uniffi_build"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "001964dd3682d600084b3aaf75acf9c3426699bc27b65e96bb32d175a31c74e9"
dependencies = [
"anyhow",
"camino",
"uniffi_bindgen",
]
[[package]]
name = "uniffi_checksum_derive"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55137c122f712d9330fd985d66fa61bdc381752e89c35708c13ce63049a3002c"
dependencies = [
"quote",
"syn 2.0.66",
]
[[package]]
name = "uniffi_core"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6121a127a3af1665cd90d12dd2b3683c2643c5103281d0fed5838324ca1fad5b"
dependencies = [
"anyhow",
"bytes",
"camino",
"log",
"once_cell",
"oneshot-uniffi",
"paste",
"static_assertions",
]
[[package]]
name = "uniffi_macros"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11cf7a58f101fcedafa5b77ea037999b88748607f0ef3a33eaa0efc5392e92e4"
dependencies = [
"bincode",
"camino",
"fs-err",
"once_cell",
"proc-macro2",
"quote",
"serde",
"syn 2.0.66",
"toml 0.5.11",
"uniffi_build",
"uniffi_meta",
]
[[package]]
name = "uniffi_meta"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71dc8573a7b1ac4b71643d6da34888273ebfc03440c525121f1b3634ad3417a2"
dependencies = [
"anyhow",
"bytes",
"siphasher 0.3.11",
"uniffi_checksum_derive",
]
[[package]]
name = "uniffi_testing"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "118448debffcb676ddbe8c5305fb933ab7e0123753e659a71dc4a693f8d9f23c"
dependencies = [
"anyhow",
"camino",
"cargo_metadata 0.15.4",
"fs-err",
"once_cell",
]
[[package]]
name = "uniffi_udl"
version = "0.25.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "889edb7109c6078abe0e53e9b4070cf74a6b3468d141bdf5ef1bd4d1dc24a1c3"
dependencies = [
"anyhow",
"uniffi_meta",
"uniffi_testing",
"weedle2",
]
[[package]]
name = "uninit"
version = "0.5.1"
@@ -9704,9 +10045,9 @@ dependencies = [
[[package]]
name = "uuid"
version = "1.8.0"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314"
dependencies = [
"getrandom",
"serde",
@@ -9732,7 +10073,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e27d6bdd219887a9eadd19e1c34f32e47fa332301184935c6d9bca26f3cca525"
dependencies = [
"anyhow",
"cargo_metadata",
"cargo_metadata 0.18.1",
"cfg-if",
"regex",
"rustc_version 0.4.0",
@@ -10014,6 +10355,15 @@ dependencies = [
"rustls-pki-types",
]
[[package]]
name = "weedle2"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e79c5206e1f43a2306fd64bdb95025ee4228960f2e6c5a8b173f3caaf807741"
dependencies = [
"nom",
]
[[package]]
name = "winapi"
version = "0.3.9"
+7 -1
View File
@@ -103,6 +103,9 @@ members = [
"mixnode",
"sdk/lib/socks5-listener",
"sdk/rust/nym-sdk",
"sdk/ffi/shared",
"sdk/ffi/go",
"sdk/ffi/cpp",
"service-providers/authenticator",
"service-providers/common",
"service-providers/ip-packet-router",
@@ -116,6 +119,7 @@ members = [
"nym-node/nym-node-requests",
"nym-outfox",
"nym-validator-rewarder",
"tools/echo-server",
"tools/internal/ssl-inject",
# "tools/internal/sdk-version-bump",
"tools/internal/testnet-manager",
@@ -130,6 +134,9 @@ members = [
"wasm/mix-fetch",
"wasm/node-tester",
"wasm/zknym-lib",
"tools/internal/testnet-manager",
"tools/internal/testnet-manager/dkg-bypass-contract",
"tools/echo-server",
]
default-members = [
@@ -153,7 +160,6 @@ exclude = [
"nym-wallet",
"nym-vpn/ui/src-tauri",
"cpu-cycles",
"sdk/ffi/cpp",
]
[workspace.package]
+5 -5
View File
@@ -1,7 +1,8 @@
[package]
name = "nym-cpp-ffi"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license.workspace = true
[lib]
name = "nym_cpp_ffi"
@@ -11,13 +12,12 @@ crate-type = ["cdylib"]
# Async runtime
tokio = { version = "1", features = ["full"] }
# Nym clients, addressing, packet format, common tools (logging), ffi shared
nym-sdk = { git = "https://github.com/nymtech/nym", branch = "master" }
nym-bin-common = { git = "https://github.com/nymtech/nym", branch = "master" }
nym-sphinx-anonymous-replies = { git = "https://github.com/nymtech/nym", branch = "master" }
nym-sdk = { path = "../../rust/nym-sdk/" }
nym-bin-common = { path = "../../../common/bin-common" }
nym-sphinx-anonymous-replies = { path = "../../../common/nymsphinx/anonymous-replies" }
nym-ffi-shared = { path = "../shared" }
lazy_static = "1.4.0"
# error handling
anyhow = "1.0.75"
# base58 en/decoding
bs58 = "0.5.0"
+23 -23
View File
@@ -1,46 +1,46 @@
# C++ FFI
> ⚠️ This is an initial version of this library in order to give developers something to experiment with. If you use this code to begin testing out Mixnet integration and run into issues, errors, or have feedback, please feel free to open an issue; feedback from developers trying to use it will help us improve it. If you have questions feel free to reach out via our [Matrix channel](https://matrix.to/#/#dev:nymtech.chat).
# C++ FFI
> ⚠️ This is an initial version of this library in order to give developers something to experiment with. If you use this code to begin testing out Mixnet integration and run into issues, errors, or have feedback, please feel free to open an issue; feedback from developers trying to use it will help us improve it. If you have questions feel free to reach out via our [Matrix channel](https://matrix.to/#/#dev:nymtech.chat).
This repo contains:
* `lib.rs`: an initial version of bindings for interacting with the Mixnet via the Rust SDK from C++. These are essentially match statements wrapping imported functions from the `nym-ffi-shared` lib allowing for nicer [error handling](#error-handling-).
* `main.cpp`: an example of using this library, relying on `Boost` for threads.
* `lib.rs`: an initial version of bindings for interacting with the Mixnet via the Rust SDK from C++. These are essentially match statements wrapping imported functions from the `nym-ffi-shared` lib allowing for nicer [error handling](#error-handling-).
* `main.cpp`: an example of using this library, relying on `Boost` for threads.
The example `.cpp` file is a simple example flow of:
* setting up Nym client logging
The example `.cpp` file is a simple example flow of:
* setting up Nym client logging
* creating an ephemeral Nym client (no key storage / persistent address - this will come in a future iteration)
* getting its [Nym address](https://nymtech.net/docs/clients/addressing-system.html)
* using that address to send a message to yourself via the Mixnet
* using that address to send a message to yourself via the Mixnet
* listen for and parse the incoming message for the `sender_tag` used for [anonymous replies with SURBs](https://nymtech.net/docs/architecture/traffic-flow.html#private-replies-using-surbs)
* send a reply to yourself using SURBs
## Installation
Prerequisites:
> Unlike the Go FFI code, this code does not yet have bindings for the TcpProxyClient/Server. This will happen in the future.
## Installation
Prerequisites:
* Rust
* C++
* C++
* [Boost](https://www.boost.org/) which can be installed with:
```
# Arch / Manjaro
yay -S boost boost-libs
# Arch / Manjaro
yay -S boost boost-libs
# Debian / Ubuntu
# Debian / Ubuntu
sudo apt install libboost-all-dev
```
## Usage
The `build.sh` script in the root of the repository speeds up the task of building and linking the Rust and C++ code.
* if want to quickly recompile your code run it as-is with `./build.sh`
* if you want to clean build both the Rust and C++ code after removing existing compiled binaries run it with the optional `clean` argument: `./build.sh clean`.
> Make sure to run the script from the root of the project directory.
The `build.sh` script in the root of the repository speeds up the task of building and linking the Rust and C++ code.
* if want to quickly recompile your code run it as-is with `./build.sh`
* if you want to clean build both the Rust and C++ code after removing existing compiled binaries run it with the optional `clean` argument: `./build.sh clean`.
This script will:
> Make sure to run the script from the root of the project directory.
This script will:
* (optionally if called with `clean` argument) remove existing Rust and C++ artifacts
* build `lib.rs` with the `--release` flag
* compile `main.cpp`, linking `lib.rs`
* compile `main.cpp`, linking `lib.rs`
* set value of `LD_LIBRARY_PATH` to the Rust code in `target/release/`
* run the compiled `main`
## Error Handling
## Error Handling
When calling a function across the FFI boundary (e.g.) `reply`, the Rust code is matching the output of an `_internal` function - `Res` or `Err` - to a member of the `StatusCode` enum. This allows for both Rust-style error handling and the ease of returning a `c_int` across the FFI boundary, which can be used by C++ for its own error handling / conditional logic.
+2 -3
View File
@@ -20,8 +20,8 @@ build_artifacts_and_link() {
cargo build --release &&
cd src/ &&
printf "compiling cpp \n"
g++ -std=c++11 -o main main.cpp -ldl -lpthread -L../target/release -lnym_cpp_ffi -lboost_thread &&
export LD_LIBRARY_PATH=../target/release:$LD_LIBRARY_PATH
g++ -std=c++11 -o main main.cpp -ldl -lpthread -L../../../../target/release -lnym_cpp_ffi -lboost_thread &&
export LD_LIBRARY_PATH=../../../../target/release:$LD_LIBRARY_PATH
# check output for name of rust lib - can be helpful if you've changed e.g. the name of a file and the compilation is failing
# printf "ldd main: \n"
# ldd main
@@ -48,4 +48,3 @@ else
exit 1
fi
fi
+3 -2
View File
@@ -1,7 +1,8 @@
// Copyright 2023-2024 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use nym_ffi_shared;
// TODO REMOVE when you're working on new CPP branch
#![allow(clippy::all)]
// use nym_ffi_shared;
use std::ffi::{c_char, c_int, CStr, CString};
use nym_sphinx_anonymous_replies::requests::AnonymousSenderTag;
+6 -1
View File
@@ -1,12 +1,17 @@
// Copyright 2023-2024 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
// TODO REMOVE when you're working on new CPP branch
#![allow(clippy::all)]
pub mod types {
use std::ffi::c_char;
// TODO change all the numbers / replace -2 with prxy?
#[derive(Debug)]
pub enum StatusCode {
NoError = 0,
ClientInitError = -1,
ClientUninitialisedError = -2,
// ClientUninitialisedError = -2,
SelfAddrError = -3,
SendMsgError = -4,
ReplyError = -5,
+9 -8
View File
@@ -1,19 +1,20 @@
[package]
name = "nym-go-ffi" #"goffitest"
version = "0.1.0"
name = "nym-go-ffi" #"goffitest"
version = "0.2.0"
edition = "2021"
license.workspace = true
[lib]
crate-type = ["cdylib"]
name = "nym_go_ffi" #"go_ffi"
name = "nym_go_ffi" #"go_ffi"
[dependencies]
# Bindgen
uniffi = { version = "0.25.2", features = ["cli"] }
# Nym clients, addressing, packet format, common tools (logging), ffi shared
nym-bin-common = { git = "https://github.com/nymtech/nym", branch = "master" }
nym-sdk = { git = "https://github.com/nymtech/nym", branch = "master" }
nym-sphinx-anonymous-replies = { git = "https://github.com/nymtech/nym", branch = "master" }
nym-sdk = { path = "../../rust/nym-sdk/" }
nym-bin-common = { path = "../../../common/bin-common" }
nym-sphinx-anonymous-replies = { path = "../../../common/nymsphinx/anonymous-replies" }
nym-ffi-shared = { path = "../shared" }
# Async runtime
tokio = { version = "1", features = ["full"] }
@@ -23,8 +24,8 @@ anyhow = "1.0.79"
thiserror = "1.0.56"
[build-dependencies]
uniffi = { version = "0.25.2", features = ["build" ] }
uniffi_build = { version = "0.25.2", features=["builtin-bindgen"] }
uniffi = { version = "0.25.2", features = ["build"] }
uniffi_build = { version = "0.25.2", features = ["builtin-bindgen"] }
[[bin]]
name = "uniffi-bindgen"
+16 -24
View File
@@ -1,27 +1,20 @@
# Go FFI
> ⚠️ This is an initial version of this library in order to give developers something to experiment with. If you use this code to begin testing out Mixnet integration and run into issues, errors, or have feedback, please feel free to open an issue; feedback from developers trying to use it will help us improve it. If you have questions feel free to reach out via our [Matrix channel](https://matrix.to/#/#dev:nymtech.chat).
This repo contains:
* `lib.rs`: an initial version of bindings for interacting with the Mixnet via the Rust SDK from Go. These are essentially match statemtns wrapping imported functions from the `nym-ffi-shared` lib.
* `ffi/`: a directory containing:
This repo contains:
* `lib.rs`: an initial version of bindings for interacting with the Mixnet via the Rust SDK from Go. These are essentially match statemtns wrapping imported functions from the `nym-ffi-shared` lib.
* `ffi/`: a directory containing:
* the `bindings/` files generated using [`uniffi-bindgen-go`](https://github.com/NordSecurity/uniffi-bindgen-go)
* [`example.go`](./example.go): an example of using this library.
* [`example.go`](./example.go): an example of using the mixnet client functionality.
* [`proxy_example.go`](./proxy_example.go): an example of using the TcpProxy functionality.
The `example.go` file is an example flow of:
* setting up Nym client logging
* creating an ephemeral Nym client (no key storage / persistent address - this will come in a future iteration)
* getting its [Nym address](https://nymtech.net/docs/clients/addressing-system.html)
* using that address to send a message to yourself via the Mixnet
* listen for and parse the incoming message for the `sender_tag` used for [anonymous replies with SURBs](https://nymtech.net/docs/architecture/traffic-flow.html#private-replies-using-surbs)
* send a reply to yourself using SURBs
## Useage - Consuming the Library
You can import the bindings as normal and interact with them as shown in the example files. These files import the bindings from this repository (hence the `go.mod` and `go.sum` in the crate root) but you can import them remotely as usual.
## Useage - Consuming the Library
You can import the bindings as normal and interact with them as shown in the [example file](./example.go). This example imports the bindings from the this repository (hence the `go.mod` and `go.sum` in the crate root) but you can import them remotely as usual.
## Useage - Developing on the Library
If you want to fork and add new features/functions to this library use the following instructions to rebuild the Go bindings.
## Useage - Developing on the Library
If you want to fork and add new features/functions to this library use the following instructions to rebuild the Go bindings.
Rust functions exposed to the Go binding library are in `./src/lib.rs`.
Rust functions exposed to the Go binding library are in `./src/lib.rs`.
The `build.sh` script in the root of the repository speeds up the task of building and linking the Rust and Go code.
* if want to quickly recompile your code run it as-is with `./build.sh`
@@ -29,19 +22,18 @@ The `build.sh` script in the root of the repository speeds up the task of buildi
> Make sure to run the script from the root of the project directory, and that your LD PATH is set first!
> ```
> RUST_BINARIES=target/release
> echo 'export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:'${RUST_BINARIES} >> ~/.zshrc
> source ~/.zshrc
> RUST_BINARIES=../../../target/release
> echo 'export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:'${RUST_BINARIES} >> ~/.zshrc
> source ~/.zshrc
> ```
This script will:
* (optionally if called with `clean` argument) remove existing Rust and Go artifacts
* build `lib.rs` with the `--release` flag
* compile the Go bindings
* compile the Go bindings
**WIP** you need to manually add the following `cgo` flags to the generated bindings immediately underneath LN3 (`// #include <bindings.h`). In the future this will be automated in `build.sh`:
**WIP** you need to manually add the following `cgo` flags to the generated bindings immediately underneath LN3 (`// #include <bindings.h`). In the future this will be automated in `build.sh`:
```
// #cgo LDFLAGS: -L../../target/release -lnym_go_ffi
// #cgo LDFLAGS: -L../../../../../target/release -lnym_go_ffi
```
-1
View File
@@ -1,4 +1,3 @@
fn main() {
uniffi::generate_scaffolding("src/bindings.udl").unwrap();
}
+1 -8
View File
@@ -15,14 +15,7 @@ build_artifacts() {
printf "building go bindings \n"
uniffi-bindgen-go $UDL_PATH --out-dir $GO_DIR
printf "bindings built \n\n"
# something not right with these - having to add it manually to bindings.go for the moment
# pushd $GO_DIR/bindings
# echo $(pwd)
# LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:../../target/release" \
# CGO_LDFLAGS="-L../target/release -lnym_go_ffi -lm -ldl" \
# CGO_ENABLED=1 \
# go run ../main.go
# TODO pull in auto binding from https://github.com/NordSecurity/uniffi-bindgen-go/blob/main/test_bindings.sh (removes need for manual addition of cgo flags)
}
clean_artifacts() {
+9
View File
@@ -6,6 +6,15 @@ import (
"time"
)
/*
Flow showing:
- setting up Nym client logging
- creating an ephemeral Nym client (no key storage / persistent address - this will come in a future iteration)
- getting its [Nym address](https://nymtech.net/docs/clients/addressing-system.html)
- using that address to send a message to yourself via the Mixnet
- listen for and parse the incoming message for the `sender_tag` used for [anonymous replies with SURBs] (https://nymtech.net/docs/architecture/traffic-flow.html#private-replies-using-surbs)
- send a reply to yourself using SURBs
*/
func main() {
// initialise Nym client logging - this is quite verbose but very informative
+289 -30
View File
@@ -1,7 +1,7 @@
package bindings
// #include <bindings.h>
// #cgo LDFLAGS: -L../../target/release -lnym_go_ffi
// #cgo LDFLAGS: -L../../../../../target/release -lnym_go_ffi
import "C"
import (
@@ -379,6 +379,42 @@ func uniffiCheckChecksums() {
panic("bindings: uniffi_nym_go_ffi_checksum_func_listen_for_incoming: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_nym_go_ffi_checksum_func_new_proxy_client(uniffiStatus)
})
if checksum != 14386 {
// If this happens try cleaning and rebuilding your project
panic("bindings: uniffi_nym_go_ffi_checksum_func_new_proxy_client: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_nym_go_ffi_checksum_func_new_proxy_client_default(uniffiStatus)
})
if checksum != 23215 {
// If this happens try cleaning and rebuilding your project
panic("bindings: uniffi_nym_go_ffi_checksum_func_new_proxy_client_default: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_nym_go_ffi_checksum_func_new_proxy_server(uniffiStatus)
})
if checksum != 40789 {
// If this happens try cleaning and rebuilding your project
panic("bindings: uniffi_nym_go_ffi_checksum_func_new_proxy_server: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_nym_go_ffi_checksum_func_proxy_server_address(uniffiStatus)
})
if checksum != 1079 {
// If this happens try cleaning and rebuilding your project
panic("bindings: uniffi_nym_go_ffi_checksum_func_proxy_server_address: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_nym_go_ffi_checksum_func_reply(uniffiStatus)
@@ -388,6 +424,24 @@ func uniffiCheckChecksums() {
panic("bindings: uniffi_nym_go_ffi_checksum_func_reply: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_nym_go_ffi_checksum_func_run_proxy_client(uniffiStatus)
})
if checksum != 45441 {
// If this happens try cleaning and rebuilding your project
panic("bindings: uniffi_nym_go_ffi_checksum_func_run_proxy_client: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_nym_go_ffi_checksum_func_run_proxy_server(uniffiStatus)
})
if checksum != 57536 {
// If this happens try cleaning and rebuilding your project
panic("bindings: uniffi_nym_go_ffi_checksum_func_run_proxy_server: UniFFI API checksum mismatch")
}
}
{
checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t {
return C.uniffi_nym_go_ffi_checksum_func_send_message(uniffiStatus)
@@ -399,6 +453,30 @@ func uniffiCheckChecksums() {
}
}
type FfiConverterUint64 struct{}
var FfiConverterUint64INSTANCE = FfiConverterUint64{}
func (FfiConverterUint64) Lower(value uint64) C.uint64_t {
return C.uint64_t(value)
}
func (FfiConverterUint64) Write(writer io.Writer, value uint64) {
writeUint64(writer, value)
}
func (FfiConverterUint64) Lift(value C.uint64_t) uint64 {
return uint64(value)
}
func (FfiConverterUint64) Read(reader io.Reader) uint64 {
return readUint64(reader)
}
type FfiDestroyerUint64 struct{}
func (FfiDestroyerUint64) Destroy(_ uint64) {}
type FfiConverterString struct{}
var FfiConverterStringINSTANCE = FfiConverterString{}
@@ -547,11 +625,15 @@ func (err GoWrapError) Unwrap() error {
// Err* are used for checking error type with `errors.Is`
var ErrGoWrapErrorClientInitError = fmt.Errorf("GoWrapErrorClientInitError")
var ErrGoWrapErrorClientUninitialisedError = fmt.Errorf("GoWrapErrorClientUninitialisedError")
var ErrGoWrapErrorSelfAddrError = fmt.Errorf("GoWrapErrorSelfAddrError")
var ErrGoWrapErrorSendMsgError = fmt.Errorf("GoWrapErrorSendMsgError")
var ErrGoWrapErrorReplyError = fmt.Errorf("GoWrapErrorReplyError")
var ErrGoWrapErrorListenError = fmt.Errorf("GoWrapErrorListenError")
var ErrGoWrapErrorProxyInitError = fmt.Errorf("GoWrapErrorProxyInitError")
var ErrGoWrapErrorProxyRunError = fmt.Errorf("GoWrapErrorProxyRunError")
var ErrGoWrapErrorServerInitError = fmt.Errorf("GoWrapErrorServerInitError")
var ErrGoWrapErrorAddressGetterError = fmt.Errorf("GoWrapErrorAddressGetterError")
var ErrGoWrapErrorServerRunError = fmt.Errorf("GoWrapErrorServerRunError")
// Variant structs
type GoWrapErrorClientInitError struct {
@@ -572,24 +654,6 @@ func (self GoWrapErrorClientInitError) Is(target error) bool {
return target == ErrGoWrapErrorClientInitError
}
type GoWrapErrorClientUninitialisedError struct {
message string
}
func NewGoWrapErrorClientUninitialisedError() *GoWrapError {
return &GoWrapError{
err: &GoWrapErrorClientUninitialisedError{},
}
}
func (err GoWrapErrorClientUninitialisedError) Error() string {
return fmt.Sprintf("ClientUninitialisedError: %s", err.message)
}
func (self GoWrapErrorClientUninitialisedError) Is(target error) bool {
return target == ErrGoWrapErrorClientUninitialisedError
}
type GoWrapErrorSelfAddrError struct {
message string
}
@@ -662,6 +726,96 @@ func (self GoWrapErrorListenError) Is(target error) bool {
return target == ErrGoWrapErrorListenError
}
type GoWrapErrorProxyInitError struct {
message string
}
func NewGoWrapErrorProxyInitError() *GoWrapError {
return &GoWrapError{
err: &GoWrapErrorProxyInitError{},
}
}
func (err GoWrapErrorProxyInitError) Error() string {
return fmt.Sprintf("ProxyInitError: %s", err.message)
}
func (self GoWrapErrorProxyInitError) Is(target error) bool {
return target == ErrGoWrapErrorProxyInitError
}
type GoWrapErrorProxyRunError struct {
message string
}
func NewGoWrapErrorProxyRunError() *GoWrapError {
return &GoWrapError{
err: &GoWrapErrorProxyRunError{},
}
}
func (err GoWrapErrorProxyRunError) Error() string {
return fmt.Sprintf("ProxyRunError: %s", err.message)
}
func (self GoWrapErrorProxyRunError) Is(target error) bool {
return target == ErrGoWrapErrorProxyRunError
}
type GoWrapErrorServerInitError struct {
message string
}
func NewGoWrapErrorServerInitError() *GoWrapError {
return &GoWrapError{
err: &GoWrapErrorServerInitError{},
}
}
func (err GoWrapErrorServerInitError) Error() string {
return fmt.Sprintf("ServerInitError: %s", err.message)
}
func (self GoWrapErrorServerInitError) Is(target error) bool {
return target == ErrGoWrapErrorServerInitError
}
type GoWrapErrorAddressGetterError struct {
message string
}
func NewGoWrapErrorAddressGetterError() *GoWrapError {
return &GoWrapError{
err: &GoWrapErrorAddressGetterError{},
}
}
func (err GoWrapErrorAddressGetterError) Error() string {
return fmt.Sprintf("AddressGetterError: %s", err.message)
}
func (self GoWrapErrorAddressGetterError) Is(target error) bool {
return target == ErrGoWrapErrorAddressGetterError
}
type GoWrapErrorServerRunError struct {
message string
}
func NewGoWrapErrorServerRunError() *GoWrapError {
return &GoWrapError{
err: &GoWrapErrorServerRunError{},
}
}
func (err GoWrapErrorServerRunError) Error() string {
return fmt.Sprintf("ServerRunError: %s", err.message)
}
func (self GoWrapErrorServerRunError) Is(target error) bool {
return target == ErrGoWrapErrorServerRunError
}
type FfiConverterTypeGoWrapError struct{}
var FfiConverterTypeGoWrapErrorINSTANCE = FfiConverterTypeGoWrapError{}
@@ -682,15 +836,23 @@ func (c FfiConverterTypeGoWrapError) Read(reader io.Reader) error {
case 1:
return &GoWrapError{&GoWrapErrorClientInitError{message}}
case 2:
return &GoWrapError{&GoWrapErrorClientUninitialisedError{message}}
case 3:
return &GoWrapError{&GoWrapErrorSelfAddrError{message}}
case 4:
case 3:
return &GoWrapError{&GoWrapErrorSendMsgError{message}}
case 5:
case 4:
return &GoWrapError{&GoWrapErrorReplyError{message}}
case 6:
case 5:
return &GoWrapError{&GoWrapErrorListenError{message}}
case 6:
return &GoWrapError{&GoWrapErrorProxyInitError{message}}
case 7:
return &GoWrapError{&GoWrapErrorProxyRunError{message}}
case 8:
return &GoWrapError{&GoWrapErrorServerInitError{message}}
case 9:
return &GoWrapError{&GoWrapErrorAddressGetterError{message}}
case 10:
return &GoWrapError{&GoWrapErrorServerRunError{message}}
default:
panic(fmt.Sprintf("Unknown error code %d in FfiConverterTypeGoWrapError.Read()", errorID))
}
@@ -701,22 +863,67 @@ func (c FfiConverterTypeGoWrapError) Write(writer io.Writer, value *GoWrapError)
switch variantValue := value.err.(type) {
case *GoWrapErrorClientInitError:
writeInt32(writer, 1)
case *GoWrapErrorClientUninitialisedError:
writeInt32(writer, 2)
case *GoWrapErrorSelfAddrError:
writeInt32(writer, 3)
writeInt32(writer, 2)
case *GoWrapErrorSendMsgError:
writeInt32(writer, 4)
writeInt32(writer, 3)
case *GoWrapErrorReplyError:
writeInt32(writer, 5)
writeInt32(writer, 4)
case *GoWrapErrorListenError:
writeInt32(writer, 5)
case *GoWrapErrorProxyInitError:
writeInt32(writer, 6)
case *GoWrapErrorProxyRunError:
writeInt32(writer, 7)
case *GoWrapErrorServerInitError:
writeInt32(writer, 8)
case *GoWrapErrorAddressGetterError:
writeInt32(writer, 9)
case *GoWrapErrorServerRunError:
writeInt32(writer, 10)
default:
_ = variantValue
panic(fmt.Sprintf("invalid error value `%v` in FfiConverterTypeGoWrapError.Write", value))
}
}
type FfiConverterOptionalString struct{}
var FfiConverterOptionalStringINSTANCE = FfiConverterOptionalString{}
func (c FfiConverterOptionalString) Lift(rb RustBufferI) *string {
return LiftFromRustBuffer[*string](c, rb)
}
func (_ FfiConverterOptionalString) Read(reader io.Reader) *string {
if readInt8(reader) == 0 {
return nil
}
temp := FfiConverterStringINSTANCE.Read(reader)
return &temp
}
func (c FfiConverterOptionalString) Lower(value *string) RustBuffer {
return LowerIntoRustBuffer[*string](c, value)
}
func (_ FfiConverterOptionalString) Write(writer io.Writer, value *string) {
if value == nil {
writeInt8(writer, 0)
} else {
writeInt8(writer, 1)
FfiConverterStringINSTANCE.Write(writer, *value)
}
}
type FfiDestroyerOptionalString struct{}
func (_ FfiDestroyerOptionalString) Destroy(value *string) {
if value != nil {
FfiDestroyerString{}.Destroy(*value)
}
}
func GetSelfAddress() (string, error) {
_uniffiRV, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) RustBufferI {
return C.uniffi_nym_go_ffi_fn_func_get_self_address(_uniffiStatus)
@@ -756,6 +963,42 @@ func ListenForIncoming() (IncomingMessage, error) {
}
}
func NewProxyClient(serverAddress string, listenAddress string, listenPort string, closeTimeout uint64, env *string) error {
_, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) bool {
C.uniffi_nym_go_ffi_fn_func_new_proxy_client(FfiConverterStringINSTANCE.Lower(serverAddress), FfiConverterStringINSTANCE.Lower(listenAddress), FfiConverterStringINSTANCE.Lower(listenPort), FfiConverterUint64INSTANCE.Lower(closeTimeout), FfiConverterOptionalStringINSTANCE.Lower(env), _uniffiStatus)
return false
})
return _uniffiErr
}
func NewProxyClientDefault(serverAddress string, env *string) error {
_, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) bool {
C.uniffi_nym_go_ffi_fn_func_new_proxy_client_default(FfiConverterStringINSTANCE.Lower(serverAddress), FfiConverterOptionalStringINSTANCE.Lower(env), _uniffiStatus)
return false
})
return _uniffiErr
}
func NewProxyServer(upstreamAddress string, configDir string, env *string) error {
_, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) bool {
C.uniffi_nym_go_ffi_fn_func_new_proxy_server(FfiConverterStringINSTANCE.Lower(upstreamAddress), FfiConverterStringINSTANCE.Lower(configDir), FfiConverterOptionalStringINSTANCE.Lower(env), _uniffiStatus)
return false
})
return _uniffiErr
}
func ProxyServerAddress() (string, error) {
_uniffiRV, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) RustBufferI {
return C.uniffi_nym_go_ffi_fn_func_proxy_server_address(_uniffiStatus)
})
if _uniffiErr != nil {
var _uniffiDefaultValue string
return _uniffiDefaultValue, _uniffiErr
} else {
return FfiConverterStringINSTANCE.Lift(_uniffiRV), _uniffiErr
}
}
func Reply(recipient []byte, message string) error {
_, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) bool {
C.uniffi_nym_go_ffi_fn_func_reply(FfiConverterBytesINSTANCE.Lower(recipient), FfiConverterStringINSTANCE.Lower(message), _uniffiStatus)
@@ -764,6 +1007,22 @@ func Reply(recipient []byte, message string) error {
return _uniffiErr
}
func RunProxyClient() error {
_, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) bool {
C.uniffi_nym_go_ffi_fn_func_run_proxy_client(_uniffiStatus)
return false
})
return _uniffiErr
}
func RunProxyServer() error {
_, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) bool {
C.uniffi_nym_go_ffi_fn_func_run_proxy_server(_uniffiStatus)
return false
})
return _uniffiErr
}
func SendMessage(recipient string, message string) error {
_, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) bool {
C.uniffi_nym_go_ffi_fn_func_send_message(FfiConverterStringINSTANCE.Lower(recipient), FfiConverterStringINSTANCE.Lower(message), _uniffiStatus)
+58
View File
@@ -84,12 +84,46 @@ RustBuffer uniffi_nym_go_ffi_fn_func_listen_for_incoming(
RustCallStatus* out_status
);
void uniffi_nym_go_ffi_fn_func_new_proxy_client(
RustBuffer server_address,
RustBuffer listen_address,
RustBuffer listen_port,
uint64_t close_timeout,
RustBuffer env,
RustCallStatus* out_status
);
void uniffi_nym_go_ffi_fn_func_new_proxy_client_default(
RustBuffer server_address,
RustBuffer env,
RustCallStatus* out_status
);
void uniffi_nym_go_ffi_fn_func_new_proxy_server(
RustBuffer upstream_address,
RustBuffer config_dir,
RustBuffer env,
RustCallStatus* out_status
);
RustBuffer uniffi_nym_go_ffi_fn_func_proxy_server_address(
RustCallStatus* out_status
);
void uniffi_nym_go_ffi_fn_func_reply(
RustBuffer recipient,
RustBuffer message,
RustCallStatus* out_status
);
void uniffi_nym_go_ffi_fn_func_run_proxy_client(
RustCallStatus* out_status
);
void uniffi_nym_go_ffi_fn_func_run_proxy_server(
RustCallStatus* out_status
);
void uniffi_nym_go_ffi_fn_func_send_message(
RustBuffer recipient,
RustBuffer message,
@@ -411,10 +445,34 @@ uint16_t uniffi_nym_go_ffi_checksum_func_listen_for_incoming(
RustCallStatus* out_status
);
uint16_t uniffi_nym_go_ffi_checksum_func_new_proxy_client(
RustCallStatus* out_status
);
uint16_t uniffi_nym_go_ffi_checksum_func_new_proxy_client_default(
RustCallStatus* out_status
);
uint16_t uniffi_nym_go_ffi_checksum_func_new_proxy_server(
RustCallStatus* out_status
);
uint16_t uniffi_nym_go_ffi_checksum_func_proxy_server_address(
RustCallStatus* out_status
);
uint16_t uniffi_nym_go_ffi_checksum_func_reply(
RustCallStatus* out_status
);
uint16_t uniffi_nym_go_ffi_checksum_func_run_proxy_client(
RustCallStatus* out_status
);
uint16_t uniffi_nym_go_ffi_checksum_func_run_proxy_server(
RustCallStatus* out_status
);
uint16_t uniffi_nym_go_ffi_checksum_func_send_message(
RustCallStatus* out_status
);
+158
View File
@@ -0,0 +1,158 @@
package main
import (
"bufio"
"fmt"
"net"
"nymffi/go-nym/bindings"
"os"
"time"
)
func runProxyClient() {
run_err := bindings.RunProxyClient()
if run_err != nil {
fmt.Println(run_err)
return
}
}
func runProxyServer() {
run_err := bindings.RunProxyServer()
if run_err != nil {
fmt.Println(run_err)
return
}
}
// connects to the proxy server and listens out for incoming as you would with a normal tcp connection
func startTcpListener() {
ln, err := net.Listen("tcp", ":9000")
if err != nil {
fmt.Println(err)
return
}
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Server-side tcp received: %s", buf)
_, err = conn.Write(buf)
if err != nil {
fmt.Println(err)
return
}
}
/*
Flow showing:
- setting up Nym client logging
- creating instances of both the NymProxyClient and NymProxyServer
- running both in goroutines to kick off the process
- starting a vanilla tcp listener/echo in a goroutine connected to the ProxyServer instance
- pinging that via a tcp client and waiting for the reply: this is (under the hood) sent anonymously via SURBs - the ProxyServer and 'server-side' tcp listener never know the Nym address or IP of the ProxyClient/'client-side' tcp client.
*/
func main() {
// our mixnet client config file defining which network to use
var env_path = "../../../envs/canary.env"
// the tcp socket our server communicates with - the remote host your client is trying to hit
var upstreamAddress = "127.0.0.1:9000"
// where the keys and persistent storage for SURBs is located (this path will be prepended with the value of $HOME in the rust lib)
var configDir = "/tmp/go-proxy-server-example"
// tcp socket port our proxy client communicates with
var clientPort = "8080"
// timeout for ephemeral client to shutdown connection after sending Close message enum once it has sent all of the other messages (in seconds): this is used by the ProxyServer for session management
var clientTimeout uint64 = 60
bindings.InitLogging()
// checking loading proper env
file, err := os.Open(env_path)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
// init a proxy server
build_serv_err := bindings.NewProxyServer(upstreamAddress, configDir, &env_path)
if build_serv_err != nil {
fmt.Println(build_serv_err)
return
}
// get proxy addr
proxyAddr, get_addr_err := bindings.ProxyServerAddress()
if get_addr_err != nil {
fmt.Println("(Go) Error:", get_addr_err)
return
}
fmt.Println("(Go) server address:")
fmt.Println(proxyAddr)
// run it in a goroutine
go runProxyServer()
// initialise a proxy client
build_err := bindings.NewProxyClient(proxyAddr, "127.0.0.1", clientPort, clientTimeout, &env_path)
if build_err != nil {
fmt.Println(build_err)
return
}
// run it in a goroutine
go runProxyClient()
// connect 'server-side' tcp socket to ProxyServer
go startTcpListener()
// send a oneshot message, wait for the echo, and close. you will see the session uuid info and the fact that the proxy_client logs it will be closing the session in <clientTimeout>.
conn, err := net.Dial("tcp", "localhost:8080")
if err != nil {
fmt.Println(err)
return
}
_, err = conn.Write([]byte("Hello, server: oneshot ping\n"))
if err != nil {
fmt.Println(err)
return
}
buf := make([]byte, 1024)
_, read_err := conn.Read(buf)
if read_err != nil {
fmt.Println(read_err)
return
}
fmt.Printf("Client-side tcp received: %s", buf)
conn.Close()
// sleep so that the nym client processes can catch up - in reality you'd have another process
// running to keep logging going, so this is only necessary for this reference
time.Sleep(60 * time.Second)
fmt.Println("(Go) end go example")
}
+18 -2
View File
@@ -1,11 +1,15 @@
[Error]
enum GoWrapError {
"ClientInitError",
"ClientUninitialisedError",
"SelfAddrError",
"SendMsgError",
"ReplyError",
"ListenError"
"ListenError",
"ProxyInitError",
"ProxyRunError",
"ServerInitError",
"AddressGetterError",
"ServerRunError"
};
dictionary IncomingMessage {
@@ -25,4 +29,16 @@ namespace bindings {
void reply(bytes recipient, string message);
[Throws=GoWrapError]
IncomingMessage listen_for_incoming();
[Throws=GoWrapError]
void new_proxy_client(string server_address, string listen_address, string listen_port, u64 close_timeout, string? env);
[Throws=GoWrapError]
void new_proxy_client_default(string server_address, string? env);
[Throws=GoWrapError]
void run_proxy_client();
[Throws=GoWrapError]
void new_proxy_server(string upstream_address, string config_dir, string? env);
[Throws=GoWrapError]
string proxy_server_address();
[Throws=GoWrapError]
void run_proxy_server();
};
+81 -5
View File
@@ -5,12 +5,11 @@ use nym_sdk::mixnet::Recipient;
use nym_sphinx_anonymous_replies::requests::AnonymousSenderTag;
uniffi::include_scaffolding!("bindings");
#[allow(clippy::enum_variant_names)]
#[derive(Debug, thiserror::Error)]
enum GoWrapError {
#[error("Couldn't init client")]
ClientInitError {},
#[error("Client is uninitialised: init client first")]
ClientUninitialisedError {},
#[error("Error getting self address")]
SelfAddrError {},
#[error("Error sending message")]
@@ -19,6 +18,16 @@ enum GoWrapError {
ReplyError {},
#[error("Could not start listening")]
ListenError {},
#[error("Couldn't init proxy client")]
ProxyInitError {},
#[error("Couldn't run proxy client")]
ProxyRunError {},
#[error("Couldn't init proxy server")]
ServerInitError {},
#[error("Couldn't get proxy server address")]
AddressGetterError {},
#[error("Couldn't run proxy server")]
ServerRunError {},
}
#[no_mangle]
@@ -44,7 +53,8 @@ fn get_self_address() -> Result<String, GoWrapError> {
#[no_mangle]
fn send_message(recipient: String, message: String) -> Result<(), GoWrapError> {
let nym_recipient_type = Recipient::try_from_base58_string(recipient).unwrap();
let nym_recipient_type =
Recipient::try_from_base58_string(recipient).expect("couldn't create Recipient");
match nym_ffi_shared::send_message_internal(nym_recipient_type, &message) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::SendMsgError {}),
@@ -72,11 +82,77 @@ fn listen_for_incoming() -> Result<IncomingMessage, GoWrapError> {
match nym_ffi_shared::listen_for_incoming_internal() {
Ok(received) => {
let message = String::from_utf8_lossy(&received.message).to_string();
// maybe change this to raw bytes to send over TODO
let sender = received.sender_tag.unwrap().to_bytes().to_vec(); //.to_base58_string();
let sender = received.sender_tag.unwrap().to_bytes().to_vec();
let incoming = IncomingMessage { message, sender };
Ok(incoming)
}
Err(_) => Err(GoWrapError::ListenError {}),
}
}
#[no_mangle]
fn new_proxy_client(
server_address: String,
listen_address: String,
listen_port: String,
close_timeout: u64,
env: Option<String>,
) -> Result<(), GoWrapError> {
let server_nym_addr =
Recipient::try_from_base58_string(server_address).expect("couldn't create Recipient");
match nym_ffi_shared::proxy_client_new_internal(
server_nym_addr,
&listen_address,
&listen_port,
close_timeout,
env,
) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ProxyInitError {}),
}
}
#[no_mangle]
fn new_proxy_client_default(
server_address: String,
env: Option<String>,
) -> Result<(), GoWrapError> {
let server_nym_addr =
Recipient::try_from_base58_string(server_address).expect("couldn't create Recipient");
match nym_ffi_shared::proxy_client_new_defaults_internal(server_nym_addr, env) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ProxyInitError {}),
}
}
fn run_proxy_client() -> Result<(), GoWrapError> {
match nym_ffi_shared::proxy_client_run_internal() {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ProxyRunError {}),
}
}
fn new_proxy_server(
upstream_address: String,
config_dir: String,
env: Option<String>,
) -> Result<(), GoWrapError> {
match nym_ffi_shared::proxy_server_new_internal(&upstream_address, &config_dir, env) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ServerInitError {}),
}
}
fn proxy_server_address() -> Result<String, GoWrapError> {
match nym_ffi_shared::proxy_server_address_internal() {
Ok(address) => Ok(address.to_string()),
Err(_) => Err(GoWrapError::AddressGetterError {}),
}
}
fn run_proxy_server() -> Result<(), GoWrapError> {
match nym_ffi_shared::proxy_server_run_internal() {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ServerRunError {}),
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
fn main() {
uniffi::uniffi_bindgen_main()
}
}
+7 -9
View File
@@ -1,16 +1,16 @@
[package]
name = "nym-ffi-shared"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
license.workspace = true
# TODO change to load relative + remove this from the workspace exclude list
[dependencies]
# Async runtime
tokio = { version = "1", features = ["full"] }
# Nym clients, addressing, packet format, common tools (logging)
nym-sdk = { git = "https://github.com/nymtech/nym", branch = "master" }
nym-bin-common = { git = "https://github.com/nymtech/nym", branch = "master" }
nym-sphinx-anonymous-replies = { git = "https://github.com/nymtech/nym", branch = "master" }
nym-sdk = { path = "../../rust/nym-sdk/" }
nym-bin-common = { path = "../../../common/bin-common" }
nym-sphinx-anonymous-replies = { path = "../../../common/nymsphinx/anonymous-replies" }
# static var macro
lazy_static = "1.4.0"
# error handling
@@ -21,7 +21,5 @@ bs58 = "0.5.0"
uniffi = { version = "0.25.2", features = ["cli"] }
[build-dependencies]
uniffi = { version = "0.25.2", features = ["build" ] }
uniffi_build = { version = "0.25.2", features=["builtin-bindgen"] }
uniffi = { version = "0.25.2", features = ["build"] }
uniffi_build = { version = "0.25.2", features = ["builtin-bindgen"] }
+162 -7
View File
@@ -3,19 +3,24 @@
use anyhow::{anyhow, bail};
use lazy_static::lazy_static;
use nym_sdk::mixnet::{MixnetClient, MixnetMessageSender, ReconstructedMessage, Recipient};
use nym_sdk::mixnet::{
MixnetClient, MixnetClientBuilder, MixnetMessageSender, Recipient, ReconstructedMessage,
StoragePaths,
};
use nym_sdk::tcp_proxy::{NymProxyClient, NymProxyServer};
use nym_sphinx_anonymous_replies::requests::AnonymousSenderTag;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use tokio::runtime::Runtime;
// NYM_CLIENT: Static reference (only init-ed once) to:
// NYM_CLIENT/PROXIES: Static reference (only init-ed once) to:
// - Arc: share ownership
// - Mutex: thread-safe way to share data between threads
// - Option: init-ed or not
// RUNTIME: Tokio runtime: no need to pass back to C and deal with raw pointers as it was previously
lazy_static! {
static ref NYM_PROXY_CLIENT: Arc<Mutex<Option<NymProxyClient>>> = Arc::new(Mutex::new(None));
static ref NYM_PROXY_SERVER: Arc<Mutex<Option<NymProxyServer>>> = Arc::new(Mutex::new(None));
static ref NYM_CLIENT: Arc<Mutex<Option<MixnetClient>>> = Arc::new(Mutex::new(None));
static ref RUNTIME: Runtime = Runtime::new().unwrap();
}
@@ -30,7 +35,30 @@ pub fn init_ephemeral_internal() -> anyhow::Result<(), anyhow::Error> {
if let Ok(ref mut client) = client {
**client = Some(init_client);
} else {
anyhow!("couldnt lock NYM_CLIENT");
return Err(anyhow!("couldnt lock ephemeral NYM_CLIENT"));
}
Ok::<(), anyhow::Error>(())
})?;
}
Ok(())
}
pub fn init_default_storage_internal(config_dir: PathBuf) -> anyhow::Result<(), anyhow::Error> {
if NYM_CLIENT.lock().unwrap().as_ref().is_some() {
bail!("client already exists");
} else {
RUNTIME.block_on(async move {
let storage_paths = StoragePaths::new_from_dir(&config_dir).unwrap();
let init_client = MixnetClientBuilder::new_with_default_storage(storage_paths)
.await?
.build()?
.connect_to_mixnet()
.await?;
let mut client = NYM_CLIENT.try_lock();
if let Ok(ref mut client) = client {
**client = Some(init_client);
} else {
return Err(anyhow!("couldnt lock NYM_CLIENT"));
}
Ok::<(), anyhow::Error>(())
})?;
@@ -50,6 +78,8 @@ pub fn get_self_address_internal() -> anyhow::Result<String, anyhow::Error> {
Ok(nym_client.nym_address().to_string())
}
// TODO split sender
pub fn send_message_internal(
recipient: Recipient,
message: &str,
@@ -62,7 +92,6 @@ pub fn send_message_internal(
.as_ref()
.ok_or_else(|| anyhow!("could not get client as_ref()"))?;
// send message
RUNTIME.block_on(async move {
nym_client.send_plain_message(recipient, message).await?;
Ok::<(), anyhow::Error>(())
@@ -70,6 +99,8 @@ pub fn send_message_internal(
Ok(())
}
// TODO send_raw_message_internal
pub fn reply_internal(
recipient: AnonymousSenderTag,
message: &str,
@@ -100,7 +131,10 @@ pub fn listen_for_incoming_internal() -> anyhow::Result<ReconstructedMessage, an
let message = RUNTIME.block_on(async move {
let received = wait_for_non_empty_message(client).await?;
Ok::<ReconstructedMessage, anyhow::Error>(ReconstructedMessage {message: received.message, sender_tag: received.sender_tag})
Ok::<ReconstructedMessage, anyhow::Error>(ReconstructedMessage {
message: received.message,
sender_tag: received.sender_tag,
})
})?;
Ok(message)
@@ -118,3 +152,124 @@ pub async fn wait_for_non_empty_message(
}
bail!("(Rust) did not receive any non-empty message")
}
pub fn proxy_client_new_internal(
server_address: Recipient,
listen_address: &str,
listen_port: &str,
close_timeout: u64,
env: Option<String>,
) -> anyhow::Result<(), anyhow::Error> {
if NYM_PROXY_CLIENT.lock().unwrap().as_ref().is_some() {
bail!("proxy client already exists");
} else {
RUNTIME.block_on(async move {
let init_proxy_client = NymProxyClient::new(
server_address,
listen_address,
listen_port,
close_timeout,
env,
)
.await?;
let mut client = NYM_PROXY_CLIENT.try_lock();
if let Ok(ref mut client) = client {
**client = Some(init_proxy_client);
} else {
return Err(anyhow!("couldnt lock NYM_PROXY_CLIENT"));
}
Ok::<(), anyhow::Error>(())
})?;
}
Ok(())
}
pub fn proxy_client_new_defaults_internal(
server_address: Recipient,
env: Option<String>,
) -> anyhow::Result<(), anyhow::Error> {
if NYM_PROXY_CLIENT.lock().unwrap().as_ref().is_some() {
bail!("proxy client already exists");
} else {
RUNTIME.block_on(async move {
let init_proxy_client = NymProxyClient::new_with_defaults(server_address, env).await?;
let mut client = NYM_PROXY_CLIENT.try_lock();
if let Ok(ref mut client) = client {
**client = Some(init_proxy_client);
} else {
return Err(anyhow!("couldn't lock PROXY_CLIENT"));
}
Ok::<(), anyhow::Error>(())
})?;
}
Ok(())
}
pub fn proxy_client_run_internal() -> anyhow::Result<(), anyhow::Error> {
let proxy_client = NYM_PROXY_CLIENT
.lock()
.expect("could not lock NYM_PROXY_CLIENT");
if proxy_client.is_none() {
bail!("Client is not yet initialised");
}
let proxy = proxy_client
.as_ref()
.ok_or_else(|| anyhow!("could not get proxy_client as_ref()"))?;
RUNTIME.block_on(async move {
proxy.run().await?;
Ok::<(), anyhow::Error>(())
})?;
Ok(())
}
pub fn proxy_server_new_internal(
upstream_address: &str,
config_dir: &str,
env: Option<String>,
) -> anyhow::Result<(), anyhow::Error> {
if NYM_PROXY_SERVER.lock().unwrap().as_ref().is_some() {
bail!("proxy client already exists");
} else {
RUNTIME.block_on(async move {
let init_proxy_server = NymProxyServer::new(upstream_address, config_dir, env).await?;
let mut client = NYM_PROXY_SERVER.try_lock();
if let Ok(ref mut client) = client {
**client = Some(init_proxy_server);
} else {
return Err(anyhow!("couldn't lock PROXY_SERVER"));
}
Ok::<(), anyhow::Error>(())
})?;
}
Ok(())
}
pub fn proxy_server_run_internal() -> anyhow::Result<(), anyhow::Error> {
let mut proxy_server = NYM_PROXY_SERVER
.lock()
.expect("could not lock NYM_PROXY_CLIENT");
if proxy_server.is_none() {
bail!("Server is not yet initialised");
}
let proxy = proxy_server
.as_mut()
.ok_or_else(|| anyhow!("could not get proxy_client as_ref()"))?;
RUNTIME.block_on(async move {
proxy.run_with_shutdown().await?;
Ok::<(), anyhow::Error>(())
})?;
Ok(())
}
pub fn proxy_server_address_internal() -> anyhow::Result<Recipient, anyhow::Error> {
let mut proxy_server = NYM_PROXY_SERVER
.lock()
.expect("could not lock NYM_PROXY_CLIENT");
if proxy_server.is_none() {
bail!("Server is not yet initialised");
}
let proxy = proxy_server
.as_mut()
.ok_or_else(|| anyhow!("could not get proxy_client as_ref()"))?;
Ok(proxy.nym_address().to_owned())
}
+14 -1
View File
@@ -40,12 +40,25 @@ zeroize = { workspace = true }
futures = { workspace = true }
log = { workspace = true }
rand = { workspace = true }
rand = { workspace = true, features = ["small_rng"] }
tap = { workspace = true }
thiserror = { workspace = true }
url = { workspace = true }
toml = { workspace = true }
# tcpproxy dependencies
anyhow.workspace = true
dashmap.workspace = true
tokio.workspace = true
tokio-stream.workspace = true
tokio-util.workspace = true
uuid = { version = "1", features = ["v4", "serde"] }
bincode = "1.0"
serde = { version = "1", features = ["derive"] }
tracing.workspace = true
tracing-subscriber = "0.3"
dirs.workspace = true
[dev-dependencies]
anyhow = { workspace = true }
dotenvy = { workspace = true }
+38
View File
@@ -0,0 +1,38 @@
use futures::StreamExt;
use nym_network_defaults::setup_env;
use nym_sdk::mixnet;
use nym_sdk::mixnet::MixnetMessageSender;
// An example of creating a client relying on a testnet, in this case Sandbox.
#[tokio::main]
async fn main() -> anyhow::Result<()> {
nym_bin_common::logging::setup_logging();
// relative root is `sdk/rust/nym-sdk/` for fallback file path
let env_path =
std::env::var("NYM_ENV_PATH").unwrap_or_else(|_| "../../../envs/sandbox.env".to_string());
setup_env(Some(&env_path));
let sandbox_network = mixnet::NymNetworkDetails::new_from_env();
let mixnet_client = mixnet::MixnetClientBuilder::new_ephemeral()
.network_details(sandbox_network)
.build()?;
let mut client = mixnet_client.connect_to_mixnet().await?;
let our_address = client.nym_address();
// Send a message throughout the mixnet to ourselves
client
.send_plain_message(*our_address, "hello there")
.await?;
println!("Waiting for message");
if let Some(received) = client.next().await {
println!("Received: {}", String::from_utf8_lossy(&received.message));
} else {
eprintln!("Failed to receive message.");
}
client.disconnect().await;
Ok(())
}
@@ -0,0 +1,147 @@
use nym_sdk::mixnet::Recipient;
use nym_sdk::tcp_proxy;
use rand::rngs::SmallRng;
use rand::Rng;
use rand::SeedableRng;
use serde::{Deserialize, Serialize};
use std::env;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
use tokio::signal;
use tokio_stream::StreamExt;
use tokio_util::codec;
#[derive(Serialize, Deserialize, Debug)]
struct ExampleMessage {
message_id: i8,
message_bytes: Vec<u8>,
tcp_conn: i8,
}
// This example just starts off a bunch of Tcp connections on a loop to a remote endpoint: in this case the TcpListener behind the NymProxyServer instance on the echo server found in `nym/tools/echo-server/`. It pipes a few messages to it, logs the replies, and keeps track of the number of replies received per connection.
//
// To run:
// - run the echo server with `cargo run`
// - run this example with `cargo run --example tcp_proxy_multistream -- <ECHO_SERVER_NYM_ADDRESS> <ENV_FILE_PATH> <CLIENT_PORT>` e.g.
// cargo run --example tcp_proxy_multistream -- DMHyxo8n6sKWHHTVvjRVDxDSMX8gYXRU1AQ6UpwsrWiB.6STYCWGWyRxqn2juWdgjMkAMsT9EaAzPpLWq5zkS68MB@CJG5zTcmoLijmDrtAiLV9PZHxNz8LQu6hmgA89V2RxxL ../../../envs/canary.env 8080
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let server_address = env::args().nth(1).expect("Server address not provided");
let server: Recipient =
Recipient::try_from_base58_string(&server_address).expect("Invalid server address");
// Comment this out to just see println! statements from this example.
// Nym client logging is very informative but quite verbose.
// The Message Decay related logging gives you an ideas of the internals of the proxy message ordering: you need to switch
// to DEBUG to see the contents of the msg buffer, sphinx packet chunking, etc.
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
let env_path = env::args().nth(2).expect("Env file not specified");
let env = env_path.to_string();
let listen_port = env::args().nth(3).expect("Port not specified");
// Within the TcpProxyClient, individual client shutdown is triggered by the timeout.
let proxy_client =
tcp_proxy::NymProxyClient::new(server, "127.0.0.1", &listen_port, 45, Some(env)).await?;
tokio::spawn(async move {
proxy_client.run().await?;
Ok::<(), anyhow::Error>(())
});
println!("waiting for everything to be set up..");
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
println!("done. sending bytes");
// In the info traces you will see the different session IDs being set up, one for each TcpStream.
for i in 0..4 {
let conn_id = i;
println!("Starting TCP connection {}", conn_id);
let local_tcp_addr = format!("127.0.0.1:{}", listen_port.clone());
tokio::spawn(async move {
// Now the client and server proxies are running we can create and pipe traffic to/from
// a socket on the same port as our ProxyClient instance as if we were just communicating
// between a client and host via a normal TcpStream - albeit with a decent amount of additional latency.
//
// The assumption regarding integration is that you know what you're sending, and will do proper
// framing before and after, know what data types you're expecting; the proxies are just piping bytes
// back and forth using tokio's `Bytecodec` under the hood.
let stream = TcpStream::connect(local_tcp_addr).await?;
let (read, mut write) = stream.into_split();
// Lets just send a bunch of messages to the server with variable delays between them, with a message and tcp connection ids to keep track of ordering on the server side (for illustrative purposes **only**; keeping track of anonymous replies is handled by the proxy under the hood with Single Use Reply Blocks (SURBs); for this illustration we want some kind of app-level message id, but irl most of the time you'll probably be parsing on e.g. the incoming response type instead)
tokio::spawn(async move {
for i in 0..4 {
let mut rng = SmallRng::from_entropy();
let delay: f64 = rng.gen_range(2.5..5.0);
tokio::time::sleep(tokio::time::Duration::from_secs_f64(delay)).await;
let random_bytes = gen_bytes_fixed(i as usize);
let msg = ExampleMessage {
message_id: i,
message_bytes: random_bytes,
tcp_conn: conn_id,
};
let serialised = bincode::serialize(&msg)?;
write
.write_all(&serialised)
.await
.expect("couldn't write to stream");
println!(
">> client sent {}: {} bytes on conn {}",
&i,
msg.message_bytes.len(),
&conn_id
);
}
Ok::<(), anyhow::Error>(())
});
tokio::spawn(async move {
let mut reply_counter = 0;
let codec = codec::BytesCodec::new();
let mut framed_read = codec::FramedRead::new(read, codec);
while let Some(Ok(bytes)) = framed_read.next().await {
match bincode::deserialize::<ExampleMessage>(&bytes) {
Ok(msg) => {
println!(
"<< client received {}: {} bytes on conn {}",
msg.message_id,
msg.message_bytes.len(),
msg.tcp_conn
);
reply_counter += 1;
println!(
"tcp connection {} replies received {}/4",
msg.tcp_conn, reply_counter
);
}
Err(e) => {
println!("<< client received something that wasn't an example message of {} bytes. error: {}", bytes.len(), e);
}
}
}
});
Ok::<(), anyhow::Error>(())
});
let mut rng = SmallRng::from_entropy();
let delay: f64 = rng.gen_range(4.5..7.0);
tokio::time::sleep(tokio::time::Duration::from_secs_f64(delay)).await;
}
// Once timeout is passed, you can either wait for graceful shutdown or just hard stop it.
signal::ctrl_c().await?;
println!("CTRL+C received, shutting down");
Ok(())
}
// emulate a series of small messages followed by a closing larger one
fn gen_bytes_fixed(i: usize) -> Vec<u8> {
let amounts = [10, 15, 50, 1000];
let len = amounts[i];
let mut rng = rand::thread_rng();
(0..len).map(|_| rng.gen::<u8>()).collect()
}
@@ -0,0 +1,192 @@
use nym_sdk::tcp_proxy;
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use serde::{Deserialize, Serialize};
use std::env;
use std::fs;
use std::sync::atomic::{AtomicU8, Ordering};
use tokio::io::AsyncWriteExt;
use tokio::net::{TcpListener, TcpStream};
use tokio::signal;
use tokio_stream::StreamExt;
use tokio_util::codec;
#[derive(Serialize, Deserialize, Debug)]
struct ExampleMessage {
message_id: i8,
message_bytes: Vec<u8>,
}
// This is a basic example which opens a single TCP connection and writes a bunch of messages between a client and an echo
// server, so only uses a single session under the hood and doesn't really show off the message ordering capabilities; this is mainly
// just a quick introductory illustration on how:
// - the mixnet does message ordering
// - the NymProxyClient and NymProxyServer can be hooked into and used to communicate between two otherwise pretty vanilla TcpStreams
//
// For a more irl example checkout tcp_proxy_multistream.rs
//
// Run this with:
// `cargo run --example tcp_proxy_single_connection <SERVER_LISTEN_PORT> <ENV_FILE_PATH> <CLIENT_LISTEN_PATH>` e.g.
// `cargo run --example tcp_proxy_single_connection 8081 ../../../envs/canary.env 8080 `
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Keep track of sent/received messages
// let counter = Arc::new(Mutex::new(0));
let counter = AtomicU8::new(0);
// Comment this out to just see println! statements from this example, as Nym client logging is very informative but quite verbose.
// The Message Decay related logging gives you an ideas of the internals of the proxy message ordering. To see the contents of the msg buffer, sphinx packet chunking, etc change the tracing::Level to DEBUG.
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
let server_port = env::args()
.nth(1)
.expect("Server listen port not specified");
let upstream_tcp_addr = format!("127.0.0.1:{}", server_port);
// This dir gets cleaned up at the end: NOTE if you switch env between tests without letting the file do the automatic cleanup, make sure to manually remove this directory up before running again, otherwise your client will attempt to use these keys for the new env
let home_dir = dirs::home_dir().expect("Unable to get home directory");
let conf_path = format!("{}/tmp/nym-proxy-server-config", home_dir.display());
let env_path = env::args().nth(2).expect("Env file not specified");
let env = env_path.to_string();
let client_port = env::args().nth(3).expect("Port not specified");
let mut proxy_server =
tcp_proxy::NymProxyServer::new(&upstream_tcp_addr, &conf_path, Some(env_path.clone()))
.await?;
let proxy_nym_addr = proxy_server.nym_address();
// We'll run the instance with a long timeout since we're sending everything down the same Tcp connection, so should be using a single session.
// Within the TcpProxyClient, individual client shutdown is triggered by the timeout.
let proxy_client =
tcp_proxy::NymProxyClient::new(*proxy_nym_addr, "127.0.0.1", &client_port, 60, Some(env))
.await?;
tokio::spawn(async move {
proxy_server.run_with_shutdown().await?;
Ok::<(), anyhow::Error>(())
});
tokio::spawn(async move {
proxy_client.run().await?;
Ok::<(), anyhow::Error>(())
});
// 'Server side' thread: echo back incoming as response to the messages sent in the 'client side' thread below
tokio::spawn(async move {
let listener = TcpListener::bind(upstream_tcp_addr).await?;
loop {
let (socket, _) = listener.accept().await.unwrap();
let (read, mut write) = socket.into_split();
let codec = codec::BytesCodec::new();
let mut framed_read = codec::FramedRead::new(read, codec);
while let Some(Ok(bytes)) = framed_read.next().await {
match bincode::deserialize::<ExampleMessage>(&bytes) {
Ok(msg) => {
println!(
"<< server received {}: {} bytes",
msg.message_id,
msg.message_bytes.len()
);
let msg = ExampleMessage {
message_id: msg.message_id,
message_bytes: msg.message_bytes,
};
let serialised = bincode::serialize(&msg)?;
write
.write_all(&serialised)
.await
.expect("couldnt send reply");
println!(
">> server sent {}: {} bytes",
msg.message_id,
msg.message_bytes.len()
);
}
Err(e) => {
println!("<< server received something that wasn't an example message of {} bytes. error: {}", bytes.len(), e);
}
}
}
}
#[allow(unreachable_code)]
Ok::<(), anyhow::Error>(())
});
// Just wait for Nym clients to connect, TCP clients to bind, etc.
println!("waiting for everything to be set up..");
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
println!("done. sending bytes");
// Now the client and server proxies are running we can create and pipe traffic to/from
// a socket on the same port as our ProxyClient instance as if we were just communicating
// between a client and host via a normal TcpStream - albeit with a decent amount of additional latency.
//
// The assumption regarding integration is that you know what you're sending, and will do proper
// framing before and after, know what data types you're expecting, etc; the proxies are just piping bytes
// back and forth using tokio's `Bytecodec` under the hood.
let local_tcp_addr = format!("127.0.0.1:{}", client_port);
let stream = TcpStream::connect(local_tcp_addr).await?;
let (read, mut write) = stream.into_split();
// 'Client side' thread; lets just send a bunch of messages to the server with variable delays between them, with an id to keep track of ordering in the printlns; the mixnet only guarantees message delivery, not ordering. You might not be necessarily streaming traffic in this manner IRL, but this example is a good illustration of how messages travel through the mixnet.
// - On the level of individual messages broken into multiple packets, the Proxy abstraction deals with making sure that everything is sent between the sockets in the corrent order.
// - On the level of different messages, this is not enforced: you might see in the logs that message 1 arrives at the server and is reconstructed after message 2.
tokio::spawn(async move {
let mut rng = SmallRng::from_entropy();
for i in 0..10 {
let random_bytes = gen_bytes_fixed(i as usize);
let msg = ExampleMessage {
message_id: i,
message_bytes: random_bytes,
};
let serialised = bincode::serialize(&msg)?;
write
.write_all(&serialised)
.await
.expect("couldn't write to stream");
println!(">> client sent {}: {} bytes", &i, msg.message_bytes.len());
let delay = rng.gen_range(3.0..7.0);
tokio::time::sleep(tokio::time::Duration::from_secs_f64(delay)).await;
}
Ok::<(), anyhow::Error>(())
});
let codec = codec::BytesCodec::new();
let mut framed_read = codec::FramedRead::new(read, codec);
while let Some(Ok(bytes)) = framed_read.next().await {
match bincode::deserialize::<ExampleMessage>(&bytes) {
Ok(msg) => {
println!(
"<< client received {}: {} bytes",
msg.message_id,
msg.message_bytes.len()
);
counter.fetch_add(1, Ordering::SeqCst);
println!(
":: messages received back: {:?}/10",
counter.load(Ordering::SeqCst)
);
}
Err(e) => {
println!("<< client received something that wasn't an example message of {} bytes. error: {}", bytes.len(), e);
}
}
}
// Once timeout is passed, you can either wait for graceful shutdown or just hard stop it.
signal::ctrl_c().await?;
println!(":: CTRL+C received, shutting down + cleanup up proxy server config files");
fs::remove_dir_all(conf_path)?;
Ok(())
}
fn gen_bytes_fixed(i: usize) -> Vec<u8> {
// let amounts = vec![1, 10, 50, 100, 150, 200, 350, 500, 750, 1000];
let amounts = [158, 1088, 505, 1001, 150, 200, 3500, 500, 750, 100];
let len = amounts[i];
let mut rng = rand::thread_rng();
(0..len).map(|_| rng.gen::<u8>()).collect()
}
+2
View File
@@ -1,11 +1,13 @@
//! Rust SDK for the Nym platform
//!
//! The main component currently is [`mixnet`].
//! [`tcp_proxy`] is probably a good place to start for anyone wanting to integrate with existing app code and read/write from a socket.
mod error;
pub mod bandwidth;
pub mod mixnet;
pub mod tcp_proxy;
pub use error::{Error, Result};
pub use nym_client_core::client::mix_traffic::transceiver::*;
+208
View File
@@ -0,0 +1,208 @@
//! Proxy abstractions for interacting with the mixnet like a tcp socket
//!
//!
//! # Basic example
//!
//! ```no_run
//! use bincode;
//! use dirs;
//! use nym_sdk::tcp_proxy;
//! use rand::rngs::SmallRng;
//! use rand::{Rng, SeedableRng};
//! use serde::{Deserialize, Serialize};
//! use std::env;
//! use std::fs;
//! use std::sync::atomic::{AtomicU8, Ordering};
//! use tokio::io::AsyncWriteExt;
//! use tokio::net::{TcpListener, TcpStream};
//! use tokio::signal;
//! use tokio_stream::StreamExt;
//! use tokio_util::codec;
//! use tracing_subscriber;
//!
//! #[derive(Serialize, Deserialize, Debug)]
//! struct ExampleMessage {
//! message_id: i8,
//! message_bytes: Vec<u8>,
//! }
//!
//! // This is a basic example which opens a single TCP connection //! and writes a bunch of messages between a client and an echo
//! // server, so only uses a single session under the hood and //! doesn't really show off the message ordering capabilities; this is mainly
//! // just a quick introductory illustration on how:
//! // - the mixnet does message ordering
//! // - the NymProxyClient and NymProxyServer can be hooked into //! and used to communicate between two otherwise pretty vanilla TcpStreams
//! //
//! // For a more irl example checkout tcp_proxy_multistream.rs
//! //
//! // Run this with:
//! // `cargo run --example tcp_proxy_single_connection <SERVER_LISTEN_PORT> <ENV_FILE_PATH> <CLIENT_LISTEN_PATH>` e.g.
//! // `cargo run --example tcp_proxy_single_connection 8081 ../../../envs/canary.env 8080 `
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! // Keep track of sent/received messages
//! // let counter = Arc::new(Mutex::new(0));
//! let counter = AtomicU8::new(0);
//!
//! // Comment this out to just see println! statements from this example, as Nym client logging is very informative but quite verbose.
//! // The Message Decay related logging gives you an ideas of the internals of the proxy message ordering. To see the contents of the msg buffer, sphinx packet chunking, etc change the tracing::Level to DEBUG.
//! tracing_subscriber::fmt()
//! .with_max_level(tracing::Level::INFO)
//! .init();
//!
//! let server_port = env::args()
//! .nth(1)
//! .expect("Server listen port not specified");
//! let upstream_tcp_addr = format!("127.0.0.1:{}", server_port);
//!
//! // This dir gets cleaned up at the end: NOTE if you switch env between tests without letting the file do the automatic cleanup, make sure to manually remove this directory up before running again, otherwise your client will attempt to use these keys for the new env
//! let home_dir = dirs::home_dir().expect("Unable to get home directory");
//! let conf_path = format!("{}/tmp/nym-proxy-server-config", home_dir.display());
//!
//! let env_path = env::args().nth(2).expect("Env file not specified");
//! let env = env_path.to_string();
//! let client_port = env::args().nth(3).expect("Port not specified");
//!
//! let mut proxy_server =
//! tcp_proxy::NymProxyServer::new(&upstream_tcp_addr, &conf_path, Some(env_path.clone()))
//! .await?;
//! let proxy_nym_addr = proxy_server.nym_address();
//!
//! // We'll run the instance with a long timeout since we're sending everything down the same Tcp connection, so should be using a single session.
//! // Within the TcpProxyClient, individual client shutdown is triggered by the timeout.
//! let proxy_client =
//! tcp_proxy::NymProxyClient::new(*proxy_nym_addr, "127.0.0.1", &client_port, 60, Some(env))
//! .await?;
//!
//! tokio::spawn(async move {
//! let _ = proxy_server.run_with_shutdown().await?;
//! Ok::<(), anyhow::Error>(())
//! });
//!
//! tokio::spawn(async move {
//! let _ = proxy_client.run().await?;
//! Ok::<(), anyhow::Error>(())
//! });
//!
//! // 'Server side' thread: echo back incoming as response to the messages sent in the 'client side' thread below
//! tokio::spawn(async move {
//! let listener = TcpListener::bind(upstream_tcp_addr).await?;
//! loop {
//! let (socket, _) = listener.accept().await.unwrap();
//! let (read, mut write) = socket.into_split();
//! let codec = codec::BytesCodec::new();
//! let mut framed_read = codec::FramedRead::new(read, codec);
//! while let Some(Ok(bytes)) = framed_read.next().await {
//! match bincode::deserialize::<ExampleMessage> (&bytes) {
//! Ok(msg) => {
//! println!(
//! "<< server received {}: {} bytes",
//! msg.message_id,
//! msg.message_bytes.len()
//! );
//! let msg = ExampleMessage {
//! message_id: msg.message_id,
//! message_bytes: msg.message_bytes,
//! };
//! let serialised = bincode::serialize(&msg)?;
//! write
//! .write_all(&serialised)
//! .await
//! .expect("couldnt send reply");
//! println!(
//! ">> server sent {}: {} bytes",
//! msg.message_id,
//! msg.message_bytes.len()
//! );
//! }
//! Err(e) => {
//! println!("<< server received something that wasn't an example message of {} bytes. error: {}", bytes.len(), e);
//! }
//! }
//! }
//! }
//! #[allow(unreachable_code)]
//! Ok::<(), anyhow::Error>(())
//! });
//!
//! // Just wait for Nym clients to connect, TCP clients to bind, etc.
//! println!("waiting for everything to be set up..");
//! tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
//! println!("done. sending bytes");
//!
//! // Now the client and server proxies are running we can create and pipe traffic to/from
//! // a socket on the same port as our ProxyClient instance as if we were just communicating
//! // between a client and host via a normal TcpStream - albeit with a decent amount of additional latency.
//! //
//! // The assumption regarding integration is that you know what you're sending, and will do proper
//! // framing before and after, know what data types you're expecting, etc; the proxies are just piping bytes
//! // back and forth using tokio's `Bytecodec` under the hood.
//! let local_tcp_addr = format!("127.0.0.1:{}", client_port);
//! let stream = TcpStream::connect(local_tcp_addr).await?;
//! let (read, mut write) = stream.into_split();
//!
//! // 'Client side' thread; lets just send a bunch of messages to the server with variable delays between them, with an id to keep track of ordering in the printlns; the mixnet only guarantees message delivery, not ordering. You might not be necessarily streaming traffic in this manner IRL, but this example is a good illustration of how messages travel through the mixnet.
//! // - On the level of individual messages broken into multiple packets, the Proxy abstraction deals with making sure that everything is sent between the sockets in the correct order.
//! // - On the level of different messages, this is not enforced: you might see in the logs that message 1 arrives at the server and is reconstructed after message 2.
//! tokio::spawn(async move {
//! let mut rng = SmallRng::from_entropy();
//! for i in 0..10 {
//! let random_bytes = gen_bytes_fixed(i as usize);
//! let msg = ExampleMessage {
//! message_id: i,
//! message_bytes: random_bytes,
//! };
//! let serialised = bincode::serialize(&msg)?;
//! write
//! .write_all(&serialised)
//! .await
//! .expect("couldn't write to stream");
//! println!(">> client sent {}: {} bytes", &i, msg.message_bytes.len());
//! let delay = rng.gen_range(3.0..7.0);
//! tokio::time::sleep(tokio::time::Duration::from_secs_f64(delay.clone())).await;
//! }
//! Ok::<(), anyhow::Error>(())
//! });
//!
//! let codec = codec::BytesCodec::new();
//! let mut framed_read = codec::FramedRead::new(read, codec);
//! while let Some(Ok(bytes)) = framed_read.next().await {
//! match bincode::deserialize::<ExampleMessage>(&bytes) {
//! Ok(msg) => {
//! println!(
//! "<< client received {}: {} bytes",
//! msg.message_id,
//! msg.message_bytes.len()
//! );
//! counter.fetch_add(1, Ordering::SeqCst);
//! println!(
//! ":: messages received back: {:?}/10",
//! counter.load(Ordering::SeqCst)
//! );
//! }
//! Err(e) => {
//! println!("<< client received something that wasn't an example message of {} bytes. error: {}", bytes.len(), e);
//! }
//! }
//! }
//!
//! // Once timeout is passed, you can either wait for graceful shutdown or just hard stop it.
//! signal::ctrl_c().await?;
//! println!(":: CTRL+C received, shutting down + cleanup up proxy server config files");
//! fs::remove_dir_all(conf_path)?;
//! Ok(())
//! }
//!
//! fn gen_bytes_fixed(i: usize) -> Vec<u8> {
//! // let amounts = vec![1, 10, 50, 100, 150, 200, 350, 500, 750, 1000];
//! let amounts = vec![158, 1088, 505, 1001, 150, 200, 3500, 500, 750, 100];
//! let len = amounts[i];
//! let mut rng = rand::thread_rng();
//! (0..len).map(|_| rng.gen::<u8>()).collect()
//! }
//! ```
mod tcp_proxy_client;
mod tcp_proxy_server;
pub use tcp_proxy_client::NymProxyClient;
pub use tcp_proxy_server::NymProxyServer;
@@ -0,0 +1,228 @@
use crate::mixnet::{IncludedSurbs, MixnetClientBuilder, MixnetMessageSender, NymNetworkDetails};
use std::sync::Arc;
#[path = "utils.rs"]
mod utils;
use anyhow::Result;
use dashmap::DashSet;
use nym_network_defaults::setup_env;
use nym_sphinx::addressing::Recipient;
use tokio::{
net::{TcpListener, TcpStream},
sync::oneshot,
};
use tokio_stream::StreamExt;
use tokio_util::codec::{BytesCodec, FramedRead};
use tracing::{debug, info, instrument, warn};
use utils::{MessageBuffer, Payload, ProxiedMessage};
const DEFAULT_CLOSE_TIMEOUT: u64 = 60;
const DEFAULT_LISTEN_HOST: &str = "127.0.0.1";
const DEFAULT_LISTEN_PORT: &str = "8080";
pub struct NymProxyClient {
server_address: Recipient,
listen_address: String,
listen_port: String,
close_timeout: u64,
}
impl NymProxyClient {
pub async fn new(
server_address: Recipient,
listen_address: &str,
listen_port: &str,
close_timeout: u64,
env: Option<String>,
) -> Result<Self> {
debug!("loading env file: {:?}", env);
setup_env(env);
Ok(NymProxyClient {
server_address,
listen_address: listen_address.to_string(),
listen_port: listen_port.to_string(),
close_timeout,
})
}
// server_address is the Nym address of the NymProxyServer to communicate with.
pub async fn new_with_defaults(server_address: Recipient, env: Option<String>) -> Result<Self> {
debug!("loading env file: {:?}", env);
setup_env(env); // Defaults to mainnet if empty
Ok(NymProxyClient {
server_address,
listen_address: DEFAULT_LISTEN_HOST.to_string(),
listen_port: DEFAULT_LISTEN_PORT.to_string(),
close_timeout: DEFAULT_CLOSE_TIMEOUT,
})
}
pub async fn run(&self) -> Result<()> {
info!("Connecting to mixnet server at {}", self.server_address);
let listener =
TcpListener::bind(format!("{}:{}", self.listen_address, self.listen_port)).await?;
loop {
let (stream, _) = listener.accept().await?;
tokio::spawn(NymProxyClient::handle_incoming(
stream,
self.server_address,
self.close_timeout,
));
}
}
// The main body of our logic, triggered on each accepted incoming tcp connection. To deal with assumptions about
// streaming we have to implement an abstract session for each set of outgoing messages atop each connection, with message
// IDs to deal with the fact that the mixnet does not enforce message ordering.
//
// There is an initial thread which does a bunch of setup logic
// - Create a random session ID
// - Create a Nym Client (and split into read/write clients for concurrent read/write)
// - Split incoming TcpStream into OwnedReadHalf and OwnedWriteHalf for concurrent read/write
//
// Then we spawn 2 tasks:
// - 'Outgoing' thread => frames incoming bytes from OwnedReadHalf and pipe through the mixnet & trigger session close.
// - 'Incoming' thread => orders incoming messages from the Mixnet via placing them in a MessageBuffer and using tick(), as well as manage session closing.
#[instrument]
async fn handle_incoming(
stream: TcpStream,
server_address: Recipient,
close_timeout: u64,
) -> Result<()> {
// ID for creation of session abstraction; new session ID per new connection accepted by our tcp listener above.
let session_id = uuid::Uuid::new_v4();
// Used to communicate end of session between 'Outgoing' and 'Incoming' tasks
let (tx, mut rx) = oneshot::channel();
// Client creation can fail for multiple reasons like bad network connection: this loop just allows us to
// retry in a loop until we can successfully connect without having to restart the entire function
info!(":: Starting session: {}", session_id);
info!(":: creating client...");
let mut client = loop {
let net = NymNetworkDetails::new_from_env();
// TODO change to builder but ephemeral
// match MixnetClient::connect_new().await {
match MixnetClientBuilder::new_ephemeral()
.network_details(net)
.build()?
.connect_to_mixnet()
.await
{
Ok(client) => break client,
Err(err) => {
warn!(":: Error creating client: {:?}, will retry in 100ms", err);
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
}
};
let client_addr = &client.nym_address();
info!(":: client created: {}", &client_addr);
// Split our tcpstream into OwnedRead and OwnedWrite halves for concurrent read/writing
let (read, mut write) = stream.into_split();
// Since we're just trying to pipe whatever bytes our client/server are normally sending to each other,
// the bytescodec is fine to use here; we're trying to avoid modifying this stream e.g. in the process of Sphinx packet
// creation and adding padding to the payload whilst also sidestepping the need to manually manage an intermediate buffer of the
// incoming bytes from the tcp stream and writing them to our server with our Nym client.
let codec = BytesCodec::new();
let mut framed_read = FramedRead::new(read, codec);
// Much like the tcpstream, split our Nym client into a sender and receiver for concurrent read/write
let sender = client.split_sender();
// The server / service provider address our client is sending messages to will remain static
let server_addr = server_address;
// Store outgoing messages in instance of Dashset abstraction
let messages_account = Arc::new(DashSet::new());
// Wrap in an Arc for memsafe concurrent access
let sent_messages_account = Arc::clone(&messages_account);
// 'Outgoing' thread
tokio::spawn(async move {
let mut message_id = 0;
// While able to read from OwnedReadHalf of TcpStream:
// - increment our messageID - we need to ensure message ordering on both client and server.
// - create instance of ProxiedMessage abstraction with framed bytes: this is really just the message data payload in the form of those bytes
// & session and messageIDs.
// - Serialise + send message through the mixnet to the Service Provider.
// - Repeat these steps, but sending a message with a payload containing a Close signal for this session; since we have message ordering implemented
// we can fire off the session close signal without having to wait on making sure the server has received the rest of the messages.
// - Trigger our session timeout alert in the 'Incoming' thread select! loop via tx end of our oneshot channel.
while let Some(Ok(bytes)) = framed_read.next().await {
message_id += 1;
sent_messages_account.insert(message_id);
let message =
ProxiedMessage::new(Payload::Data(bytes.to_vec()), session_id, message_id);
let coded_message = bincode::serialize(&message)?;
sender
.send_message(server_addr, &coded_message, IncludedSurbs::Amount(100))
.await?;
info!(
"Sent message with id {} for session {} of {} bytes",
message_id,
session_id,
bytes.len()
);
}
message_id += 1;
let message = ProxiedMessage::new(Payload::Close, session_id, message_id);
let coded_message = bincode::serialize(&message)?;
sender
.send_message(server_addr, &coded_message, IncludedSurbs::Amount(100))
.await?;
info!(":: Closing read end of session: {}", session_id);
tx.send(true)
.map_err(|_| anyhow::anyhow!("Could not send close signal"))?;
Ok::<(), anyhow::Error>(())
});
// 'Incoming' thread
tokio::spawn(async move {
// Abstraction containing logic ordering: all our incoming messages need to be parsed based on their messageIDs per session.
// All the message-ordering and time-tracking methods are defined in utils.rs, mostly used in .tick().
let mut msg_buffer = MessageBuffer::new();
// Select!-ing one of following options:
// - rx is triggered by tx to log the session will end in ARGS.close_timeout time, break from this loop to pass to loop below
// - Deserialise incoming mixnet message, push to msg buffer and tick() to order and write to OwnedWriteHalf.
// - call tick() once per 100ms if neither of the above have occurred.
loop {
tokio::select! {
_ = &mut rx => {
info!(":: Closing write end of session: {} in {} seconds", session_id, close_timeout);
break
}
Some(message) = client.next() => {
let message = bincode::deserialize::<ProxiedMessage>(&message.message)?;
msg_buffer.push(message);
msg_buffer.tick(&mut write).await?;
},
_ = tokio::time::sleep(tokio::time::Duration::from_millis(100)) => {
msg_buffer.tick(&mut write).await?;
}
}
}
// Select!-ing one of following options:
// - Deserialise incoming mixnet message, push to msg buffer and tick() to order and write next messageID in line to OwnedWriteHalf.
// - Sleep for session timeout and return, kills thread with Ok(()).
loop {
tokio::select! {
Some(message) = client.next() => {
let message = bincode::deserialize::<ProxiedMessage>(&message.message)?;
msg_buffer.push(message);
msg_buffer.tick(&mut write).await?;
},
_ = tokio::time::sleep(tokio::time::Duration::from_secs(close_timeout)) => {
info!(":: Closing write end of session: {}", session_id);
info!(":: Triggering client shutdown");
client.disconnect().await;
return Ok::<(), anyhow::Error>(())
}
}
}
});
tokio::signal::ctrl_c().await?;
Ok(())
}
}
@@ -0,0 +1,229 @@
use crate::mixnet::{
AnonymousSenderTag, MixnetClient, MixnetClientBuilder, MixnetClientSender, MixnetMessageSender,
NymNetworkDetails, StoragePaths,
};
use anyhow::Result;
use dashmap::DashSet;
use nym_network_defaults::setup_env;
use nym_sphinx::addressing::Recipient;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::net::TcpStream;
use tokio::sync::watch::Receiver;
use tokio::sync::RwLock;
use tokio_stream::StreamExt;
use tracing::{debug, error, info};
#[allow(clippy::duplicate_mod)]
#[path = "utils.rs"]
mod utils;
use utils::{MessageBuffer, Payload, ProxiedMessage};
use uuid::Uuid;
pub struct NymProxyServer {
upstream_address: String,
session_map: DashSet<Uuid>,
mixnet_client: MixnetClient,
mixnet_client_sender: Arc<RwLock<MixnetClientSender>>,
tx: tokio::sync::watch::Sender<Option<(ProxiedMessage, AnonymousSenderTag)>>,
rx: tokio::sync::watch::Receiver<Option<(ProxiedMessage, AnonymousSenderTag)>>,
}
impl NymProxyServer {
pub async fn new(
upstream_address: &str,
config_dir: &str,
env: Option<String>,
) -> Result<Self> {
info!(":: creating client...");
// We're wanting to build a client with a constant address, vs the ephemeral in-memory data storage of the NymProxyClient clients.
// Following a builder pattern, having to manually connect to the mixnet below.
let config_dir = PathBuf::from(config_dir);
debug!("loading env file: {:?}", env);
setup_env(env); // Defaults to mainnet if empty
let net = NymNetworkDetails::new_from_env();
let storage_paths = StoragePaths::new_from_dir(&config_dir)?;
let client = MixnetClientBuilder::new_with_default_storage(storage_paths)
.await?
.network_details(net)
.build()?;
let client = client.connect_to_mixnet().await?;
// Since we're splitting the client in the main thread, we have to wrap the sender side of the client in an Arc<RwLock>>.
let sender = Arc::new(RwLock::new(client.split_sender()));
// Used for passing the incoming Mixnet message => session_handler().
let (tx, rx) =
tokio::sync::watch::channel::<Option<(ProxiedMessage, AnonymousSenderTag)>>(None);
info!(":: client created: {}", client.nym_address());
Ok(NymProxyServer {
upstream_address: upstream_address.to_string(),
session_map: DashSet::new(),
mixnet_client: client,
mixnet_client_sender: sender,
tx,
rx,
})
}
pub fn nym_address(&self) -> &Recipient {
self.mixnet_client.nym_address()
}
pub fn mixnet_client_mut(&mut self) -> &mut MixnetClient {
&mut self.mixnet_client
}
pub fn session_map(&self) -> &DashSet<Uuid> {
&self.session_map
}
pub fn mixnet_client_sender(&self) -> Arc<RwLock<MixnetClientSender>> {
Arc::clone(&self.mixnet_client_sender)
}
pub fn tx(&self) -> tokio::sync::watch::Sender<Option<(ProxiedMessage, AnonymousSenderTag)>> {
self.tx.clone()
}
pub fn rx(&self) -> tokio::sync::watch::Receiver<Option<(ProxiedMessage, AnonymousSenderTag)>> {
self.rx.clone()
}
// The main body of our logic, triggered on each received new sessionID. To deal with assumptions about
// streaming we have to implement an abstract session for each set of outgoing messages atop each connection, with message
// IDs to deal with the fact that the mixnet does not enforce message ordering.
//
// There is an initial thread which does a bunch of setup logic:
// - Create a TcpStream connecting to our upstream server process.
// - Split incoming TcpStream into OwnedReadHalf and OwnedWriteHalf for concurrent read/write.
// - Create an Arc to store our session SURB - used for anonymous replies.
//
// Then we spawn 2 tasks:
// - 'Incoming' thread => deals with parsing and storing the SURB (used in Mixnet replies), deserialising and passing the incoming data from the Mixnet to the upstream server.
// - 'Outgoing' thread => frames bytes coming from TcpStream (the server) and deals with ordering + sending reply anonymously => Mixnet.
async fn session_handler(
upstream_address: String,
session_id: Uuid,
mut rx: Receiver<Option<(ProxiedMessage, AnonymousSenderTag)>>,
sender: Arc<RwLock<MixnetClientSender>>,
) -> Result<()> {
let global_surb = Arc::new(RwLock::new(None));
let stream = TcpStream::connect(upstream_address).await?;
// Split our tcpstream into OwnedRead and OwnedWrite halves for concurrent read/writing.
let (read, mut write) = stream.into_split();
// Used for anonymous replies per session. Initially parsed from the incoming message.
let send_side_surb = Arc::clone(&global_surb);
tokio::spawn(async move {
let mut message_id = 0;
// Since we're just trying to pipe whatever bytes our client/server are normally sending to each other,
// the bytescodec is fine to use here; we're trying to avoid modifying this stream e.g. in the process of Sphinx packet
// creation and adding padding to the payload whilst also sidestepping the need to manually manage an intermediate buffer of the
// incoming bytes from the tcp stream and writing them to our server with our Nym client.
let codec = tokio_util::codec::BytesCodec::new();
let mut framed_read = tokio_util::codec::FramedRead::new(read, codec);
// While able to read from OwnedReadHalf of TcpStream:
// - Keep track of outgoing messageIDs.
// - Read and store incoming SURB.
// - Send serialised reply => Mixnet via SURB.
// - If tick() returns true, close session.
while let Some(Ok(bytes)) = framed_read.next().await {
info!("server received {} bytes", bytes.len());
let reply =
ProxiedMessage::new(Payload::Data(bytes.to_vec()), session_id, message_id);
message_id += 1;
let surb = send_side_surb.read().await;
if let Some(surb) = *surb {
sender
.write()
.await
.send_reply(surb, bincode::serialize(&reply)?)
.await?
}
info!(
"Sent reply with id {} for session {}",
message_id, session_id
);
}
Ok::<(), anyhow::Error>(())
});
let messages_accounter = Arc::new(DashSet::new());
messages_accounter.insert(1);
let mut msg_buffer = MessageBuffer::new();
loop {
tokio::select! {
_ = rx.changed() => {
let value = rx.borrow_and_update().clone();
if let Some((message, surb)) = value {
if message.session_id() != session_id {
continue;
}
msg_buffer.push(message);
let local_surb = Arc::clone(&global_surb);
{
*local_surb.write().await = Some(surb);
}
let should_close = msg_buffer.tick(&mut write).await?;
if should_close {
info!(":: Closing write end of session: {}", session_id);
break;
}
}
}
_ = tokio::time::sleep(tokio::time::Duration::from_millis(100)) => {
msg_buffer.tick(&mut write).await?;
}
}
}
// This times out after 60 seconds by default.
#[allow(unreachable_code)]
Ok(())
}
pub async fn run_with_shutdown(&mut self) -> Result<()> {
// On our Mixnet client getting a new message:
// - Check if the attached sessionID exists.
// - If !sessionID, spawn a new session_handler() task.
// - Send the message down tx => rx in our handler.
while let Some(new_message) = &self.mixnet_client_mut().next().await {
let message: ProxiedMessage = bincode::deserialize(&new_message.message)?;
let session_id = message.session_id();
// If we've already got message from an existing session, continue, else add it to the session mapping and spawn a new handler().
if self.session_map().contains(&message.session_id()) {
debug!("Got message for an existing session");
} else {
self.session_map().insert(message.session_id());
debug!("Got message for a new session");
tokio::spawn(Self::session_handler(
self.upstream_address.clone(),
session_id,
self.rx(),
self.mixnet_client_sender(),
));
info!("Spawned a new session handler: {}", message.session_id());
}
debug!("Sending message for session {}", message.session_id());
if let Some(sender_tag) = new_message.sender_tag {
self.tx.send(Some((message, sender_tag)))?
} else {
error!("No sender tag found, we can't send a reply without it!")
}
}
tokio::signal::ctrl_c().await?;
Ok(())
}
}
+196
View File
@@ -0,0 +1,196 @@
use std::{collections::HashSet, fmt, ops::Deref, time::Instant};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use tokio::{io::AsyncWriteExt as _, net::tcp::OwnedWriteHalf};
use tracing::{debug, info};
use uuid::Uuid;
// Keeps track of
// - incoming and unsorted messages wrapped in DecayWrapper for keeping track of when they were received
// - the expected next message ID (reset on .tick())
#[derive(Debug)]
pub struct MessageBuffer {
buffer: Vec<DecayWrapper<ProxiedMessage>>,
next_msg_id: u16,
}
impl MessageBuffer {
pub fn new() -> Self {
MessageBuffer {
buffer: Vec::new(),
next_msg_id: 0,
}
}
pub fn push(&mut self, msg: ProxiedMessage) {
self.buffer.push(DecayWrapper::new(msg));
}
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&DecayWrapper<ProxiedMessage>) -> bool,
{
self.buffer.retain(f);
}
#[allow(dead_code)]
pub fn len(&self) -> usize {
self.buffer.len()
}
pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}
pub fn iter(&self) -> std::slice::Iter<DecayWrapper<ProxiedMessage>> {
self.buffer.iter()
}
// Used by the client to create and manipulate a buffer of messages to write => OwnedWriteHalf.
// Used by the server for this + to conditionally decide whether to kill a session on returning true.
// #[instrument]
pub async fn tick(&mut self, write: &mut OwnedWriteHalf) -> Result<bool> {
if self.is_empty() {
return Ok(false);
}
debug!("Messages in buffer:");
for msg in self.iter() {
debug!("{}", msg.inner());
}
// Iterate over self, filtering messages where msg.decayed() = true (aka message is older than 2 seconds), or where msg.message_id is less than next_msg_id. Then collect and order according to message_id.
let mut send_buffer = self
.iter()
.filter(|msg| msg.decayed() || msg.message_id() <= self.next_msg_id)
.map(|msg| msg.inner())
.collect::<Vec<&ProxiedMessage>>();
send_buffer.sort_by(|a, b| a.message_id.cmp(&b.message_id()));
if send_buffer.is_empty() {
debug!("send buf is empty");
return Ok(false);
}
let mut sent_messages = HashSet::new();
// Send collected & ordered messages down OwnedReadHalf until matching on Close enum, in which case exit & cause server to start session shutdown.
for msg in send_buffer {
match &msg.message() {
Payload::Data(data) => {
write.write_all(data).await?;
info!("Wrote message {} to stream", msg.message_id())
}
Payload::Close => {
return Ok(true);
}
}
// Store sent messages in hashmap.
sent_messages.insert(msg.message_id());
}
// Iterate through sent, find the largest message ID and add 1, set this as expected next message ID.
self.next_msg_id = sent_messages
.iter()
.max()
.expect("This is safe since we know we've set something")
+ 1;
// Retain as next_msg_id in MessageBuffer instance for parsing potential further incoming msgs.
self.retain(|msg| !sent_messages.contains(&msg.inner().message_id()));
info!("next_msg_id is: {}", self.next_msg_id.clone());
Ok(false)
}
}
// Wrapper used for tracking the 'age' of a message from when it was received.
// Used in the ordering logic in MessageBuffer.tick().
#[derive(Debug)]
pub struct DecayWrapper<T> {
value: T,
start: Instant,
decay: u64,
}
impl<T> DecayWrapper<T> {
pub fn decayed(&self) -> bool {
debug!("Decayed: {:?}", self.start.elapsed().as_secs() > self.decay);
self.start.elapsed().as_secs() > self.decay
}
pub fn new(value: T) -> Self {
DecayWrapper {
value,
start: Instant::now(),
decay: 6,
}
}
#[allow(dead_code)]
pub fn into_inner(self) -> T {
self.value
}
pub fn inner(&self) -> &T {
&self.value
}
}
impl<T> Deref for DecayWrapper<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ProxiedMessage {
message: Payload,
session_id: Uuid,
message_id: u16,
}
impl ProxiedMessage {
pub fn new(message: Payload, session_id: Uuid, message_id: u16) -> Self {
ProxiedMessage {
message,
session_id,
message_id,
}
}
pub fn message(&self) -> &Payload {
&self.message
}
pub fn session_id(&self) -> Uuid {
self.session_id
}
pub fn message_id(&self) -> u16 {
self.message_id
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum Payload {
Data(Vec<u8>),
Close,
}
impl fmt::Display for ProxiedMessage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let message = match self.message() {
Payload::Data(ref data) => format!("Data({})", data.len()),
Payload::Close => "Close".to_string(),
};
write!(
f,
"ProxiedMessage {{ message: {}, session_id: {}, message_id: {} }}",
message,
self.session_id(),
self.message_id()
)
}
}
+26
View File
@@ -0,0 +1,26 @@
[package]
name = "echo-server"
version = "0.1.0"
authors.workspace = true
repository.workspace = true
homepage.workspace = true
documentation.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[dependencies]
anyhow.workspace = true
dashmap.workspace = true
tokio = { workspace = true, features = ["full"] }
tokio-stream.workspace = true
tokio-util.workspace = true
uuid = { version = "1", features = ["v4", "serde"] }
bincode = "1.0"
serde = { version = "1", features = ["derive"] }
tracing.workspace = true
tracing-subscriber = "0.3"
bytecodec = { workspace = true }
nym-sdk = { path = "../../sdk/rust/nym-sdk/" }
bytes.workspace = true
dirs.workspace = true
+9
View File
@@ -0,0 +1,9 @@
# Nym Echo Server
This is an initial minimal implementation of an echo server built using the `NymProxyServer` Rust SDK abstraction.
## Usage
```
cargo build --release
../../target/release/echo-server <PORT> <PATH_TO_ENV_FILE> e.g. ../../target/release/echo-server 9000 ../../envs/canary.env
```
+161
View File
@@ -0,0 +1,161 @@
use anyhow::Result;
use bytes::Bytes;
use nym_sdk::tcp_proxy;
use std::env;
use std::fs;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::io::AsyncWriteExt;
use tokio::net::{TcpListener, TcpStream};
use tokio::signal;
use tokio::sync::broadcast;
use tokio::task;
use tokio_stream::StreamExt;
use tracing::{error, info, warn};
struct Metrics {
total_conn: AtomicU64,
active_conn: AtomicU64,
bytes_recv: AtomicU64,
bytes_sent: AtomicU64,
}
impl Metrics {
fn new() -> Self {
Self {
total_conn: AtomicU64::new(0),
active_conn: AtomicU64::new(0),
bytes_recv: AtomicU64::new(0),
bytes_sent: AtomicU64::new(0),
}
}
}
#[tokio::main]
async fn main() -> Result<()> {
// if you run this with DEBUG you see the msg buffer on the ProxyServer, but its quite chatty
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
let server_port = env::args()
.nth(1)
.expect("Server listen port not specified");
let tcp_addr = format!("127.0.0.1:{}", server_port);
// This dir gets cleaned up at the end: NOTE if you switch env between tests without letting the file do the automatic cleanup, make sure to manually remove this directory up before running again, otherwise your client will attempt to use these keys for the new env
let home_dir = dirs::home_dir().expect("Unable to get home directory");
let conf_path = format!("{}/tmp/nym-proxy-server-config", home_dir.display());
let env_path = env::args().nth(2).expect("Env file not specified");
let env = env_path.to_string();
let mut proxy_server = tcp_proxy::NymProxyServer::new(&tcp_addr, &conf_path, Some(env.clone()))
.await
.unwrap();
let proxy_nym_addr = *proxy_server.nym_address();
info!("ProxyServer listening out on {}", proxy_nym_addr);
task::spawn(async move {
proxy_server.run_with_shutdown().await?;
Ok::<(), anyhow::Error>(())
});
let (shutdown_sender, _) = broadcast::channel(1);
let metrics = Arc::new(Metrics::new());
let all_metrics = Arc::clone(&metrics);
tokio::spawn(async move {
loop {
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
info!(
"Metrics: total_connections={}, active_connections={}, bytes_received={}, bytes_sent={}",
all_metrics.total_conn.load(Ordering::Relaxed),
all_metrics.active_conn.load(Ordering::Relaxed),
all_metrics.bytes_recv.load(Ordering::Relaxed),
all_metrics.bytes_sent.load(Ordering::Relaxed),
);
}
});
let listener = TcpListener::bind(tcp_addr).await?;
loop {
tokio::select! {
_ = signal::ctrl_c() => {
info!("Shutdown signal received, closing server...");
let _ = shutdown_sender.send(());
// TODO we need something like this for the ProxyServer client
break;
}
Ok((socket, _)) = listener.accept() => {
let connection_metrics = Arc::clone(&metrics);
let shutdown_rx = shutdown_sender.subscribe();
connection_metrics.total_conn.fetch_add(1, Ordering::Relaxed);
connection_metrics.active_conn.fetch_add(1, Ordering::Relaxed);
tokio::spawn(async move {
handle_incoming(socket, connection_metrics, shutdown_rx).await;
});
}
}
}
signal::ctrl_c().await?;
info!("Received CTRL+C");
fs::remove_dir_all(conf_path)?;
while metrics.active_conn.load(Ordering::Relaxed) > 0 {
info!("Waiting on active connections to close: sleeping 100ms");
// TODO some kind of hard kill here for the ProxyServer
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
Ok(())
}
async fn handle_incoming(
socket: TcpStream,
metrics: Arc<Metrics>,
mut shutdown_rx: broadcast::Receiver<()>,
) {
let (read, mut write) = socket.into_split();
let codec = tokio_util::codec::BytesCodec::new();
let mut framed_read = tokio_util::codec::FramedRead::new(read, codec);
loop {
tokio::select! {
Some(result) = framed_read.next() => {
match result {
Ok(bytes) => {
let len = bytes.len();
metrics.bytes_recv.fetch_add(len as u64, Ordering::Relaxed);
if let Err(e) = write.write_all(&bytes).await {
error!("Failed to write to stream with err: {}", e);
break;
}
metrics.bytes_sent.fetch_add(len as u64, Ordering::Relaxed);
}
Err(e) => {
error!("Failed to read from stream with err: {}", e);
break;
}
}
}
_ = shutdown_rx.recv() => {
warn!("Shutdown signal received, closing connection");
break;
}
// TODO need to work out a way that if this timesout and breaks but you dont hang up the conn on the client end you can reconnect..maybe. If we just use this as a ping echo server I dont think this is a problem
// EDIT I'm not actually sure we want this functionality? Measuring active connections might be useful though
_ = tokio::time::sleep(tokio::time::Duration::from_secs(120)) => {
info!("Timeout reached, assuming we wont get more messages on this conn, closing");
let close_message = "Closing conn, reconnect if you want to ping again";
let bytes: Bytes = close_message.into();
write.write_all(&bytes).await.expect("Couldn't write to socket");
break;
}
}
}
metrics
.active_conn
.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
info!("Connection closed");
}