Skip to content

Commit

Permalink
Merge pull request #1 from tun2proxy/async
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive authored Aug 31, 2024
2 parents 634a711 + f599a9b commit dcedd44
Show file tree
Hide file tree
Showing 13 changed files with 526 additions and 82 deletions.
40 changes: 16 additions & 24 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

name: CI
on: [push, pull_request]

Expand All @@ -11,7 +10,7 @@ jobs:
matrix:
rust: [stable, beta, nightly]
steps:
- uses: actions/checkout@master
- uses: actions/checkout@v4
- name: Build & Test
if: ${{ !cancelled() }}
run: |
Expand All @@ -25,7 +24,7 @@ jobs:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/checkout@v4
- name: Rustfmt Check
run: |
rustup update stable && rustup default stable && rustup component add rustfmt
Expand All @@ -35,30 +34,23 @@ jobs:
name: Clippy Check & Build
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-features -- -D warnings
- uses: actions-rs/cargo@v1
with:
command: build
args: --examples --all-features
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: run clippy and check
shell: bash
run: |
cargo clippy --all-features -- -D warnings
cargo clippy -- -D warnings
- name: build examples
shell: bash
run: |
cargo build --examples
cargo build --examples --all-features
semver:
name: Check semver
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: obi1kenobi/cargo-semver-checks-action@v2
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ targets = [

[features]
default = []
# default = ["verify_binary_signature", "panic_on_unsent_packets"]
# default = ["verify_binary_signature", "panic_on_unsent_packets", "async"]
async = ["blocking", "futures"]
panic_on_unsent_packets = []
verify_binary_signature = []

[dependencies]
blocking = { version = "1", optional = true }
c2rust-bitfields = "0.18"
futures = { version = "0.3", optional = true }
libloading = "0.8"
log = "0.4"
thiserror = "1"
Expand Down Expand Up @@ -59,3 +62,9 @@ packet = "0.1"
pcap-file = "2"
serde_json = "1"
subprocess = "0.2"
tokio = { version = "1", features = ["full"] }

[[example]]
name = "udp-echo-async"
path = "examples/udp-echo-async.rs"
required-features = ["async"]
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let adapter = match wintun_bindings::Adapter::open(&wintun, "Demo") {
}
};
//Specify the size of the ring buffer the wintun driver should use.
let session = Arc::new(adapter.start_session(wintun_bindings::MAX_RING_CAPACITY).unwrap());
let session = adapter.start_session(wintun_bindings::MAX_RING_CAPACITY).unwrap();

//Get a 20 byte packet from the ring buffer
let mut packet = session.allocate_send_packet(20).unwrap();
Expand Down Expand Up @@ -78,10 +78,19 @@ wintun's internal ring buffer.

- `verify_binary_signature`: Verifies the signature of the wintun dll file before loading it.

## TODO:
- Add async support
Requires hooking into a windows specific reactor and registering read interest on wintun's read
handle. Asyncify other slow operations via tokio::spawn_blocking. As always, PR's are welcome!

- `async`: Enables async support for the library.
Just add `async` feature to your `Cargo.toml`:
```toml
[dependencies]
wintun-bindings = { version = "0.6", features = ["async"] }
```
And simply transform your `Session` into an `AsyncSession`:
```rust
// ...
let session = adapter.start_session(MAX_RING_CAPACITY)?;
let mut reader_session = AsyncSession::from(session.clone());
let mut writer_session: AsyncSession = session.clone().into();
// ...
```

License: MIT
7 changes: 2 additions & 5 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::sync::atomic::{AtomicBool, Ordering};

static RUNNING: AtomicBool = AtomicBool::new(true);

Expand All @@ -22,7 +19,7 @@ fn main() -> Result<(), wintun_bindings::BoxError> {
let version = wintun_bindings::get_running_driver_version(&wintun)?;
log::info!("Using wintun version: {:?}", version);

let session = Arc::new(adapter.start_session(wintun_bindings::MAX_RING_CAPACITY)?);
let session = adapter.start_session(wintun_bindings::MAX_RING_CAPACITY)?;

let reader_session = session.clone();
let reader = std::thread::spawn(move || {
Expand Down
Loading

0 comments on commit dcedd44

Please sign in to comment.