From 5fb3fdcac6f2450cef9276b15aa47d481b2cb0fd Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:00:14 +0800 Subject: [PATCH] update tun2 version --- Cargo.toml | 2 +- README.md | 14 ++++++++++++-- examples/tun.rs | 17 +++++++++++------ src/stream/mod.rs | 1 + 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6e9f7fe..4e77be9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ tracing = { version = "0.1", default-features = false, features = [ [dev-dependencies] clap = { version = "4.4", features = ["derive"] } udp-stream = { version = "0.0", default-features = false } -tun2 = { version = "0.6", features = ["async"] } +tun2 = { version = "0.7", features = ["async"] } tokio = { version = "1.35", features = [ "rt-multi-thread", ], default-features = false } diff --git a/README.md b/README.md index 4925f09..63c2616 100644 --- a/README.md +++ b/README.md @@ -19,14 +19,20 @@ async fn main(){ const MTU: u16 = 1500; let ipv4 = Ipv4Addr::new(10, 0, 0, 1); let netmask = Ipv4Addr::new(255, 255, 255, 0); - let mut config = tun::Configuration::default(); + let mut config = tun2::Configuration::default(); config.address(ipv4).netmask(netmask).mtu(MTU as i32).up(); + #[cfg(target_os = "linux")] + config.platform_config(|config| { + config.packet_information(true); + config.apply_settings(true); + }); + let mut ipstack_config = ipstack::IpStackConfig::default(); ipstack_config.mtu(MTU); let packet_information = cfg!(all(target_family = "unix", not(target_os = "android"))); ipstack_config.packet_information(packet_information); - let mut ip_stack = ipstack::IpStack::new(ipstack_config, tun::create_as_async(&config).unwrap()); + let mut ip_stack = ipstack::IpStack::new(ipstack_config, tun2::create_as_async(&config).unwrap()); while let Ok(stream) = ip_stack.accept().await { match stream { @@ -43,6 +49,10 @@ async fn main(){ copy_from_lhs_to_rhs(udp, rhs).await; }); } + _ => { + println!("unknown transport"); + continue; + } } } } diff --git a/examples/tun.rs b/examples/tun.rs index 8a5dfa5..c0b35fb 100644 --- a/examples/tun.rs +++ b/examples/tun.rs @@ -5,7 +5,7 @@ //! //! This example must be run as root or administrator privileges. //! ``` -//! sudo target/debug/examples/tun --server-addr 127.0.0.1:8080 # Linux or macOS +//! sudo target/debug/examples/tun --server-addr 127.0.0.1:8080 //! ``` //! Then please run the `echo` example server, which listens on TCP & UDP ports 127.0.0.1:8080. //! ``` @@ -13,9 +13,9 @@ //! ``` //! To route traffic to the tun interface, run the following command with root or administrator privileges: //! ``` -//! sudo ip route add 1.2.3.4/32 dev utun3 # Linux +//! sudo ip route add 1.2.3.4/32 dev tun0 # Linux //! route add 1.2.3.4 mask 255.255.255.255 10.0.0.1 metric 100 # Windows -//! sudo route add 1.2.3.4/32 10.0.0.1 # Apple macOS +//! sudo route add 1.2.3.4/32 10.0.0.1 # macOS //! ``` //! Now you can test it with `nc 1.2.3.4 any_port` or `nc -u 1.2.3.4 any_port`. //! You can watch the echo information in the `nc` console. @@ -56,13 +56,14 @@ async fn main() -> Result<(), Box> { config.destination(gateway); #[cfg(target_os = "linux")] - config.platform(|config| { + config.platform_config(|config| { config.packet_information(true); + config.apply_settings(true); }); #[cfg(target_os = "windows")] - config.platform(|config| { - config.initialize(Some(12324323423423434234_u128)); + config.platform_config(|config| { + config.device_guid(Some(12324323423423434234_u128)); }); let mut ipstack_config = ipstack::IpStackConfig::default(); @@ -136,6 +137,10 @@ async fn main() -> Result<(), Box> { println!("unknown transport - {} bytes", payload.len()); continue; } + _ => { + println!("unknown transport"); + continue; + } }; } } diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 9878f99..3117fea 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -9,6 +9,7 @@ mod tcp; mod udp; mod unknown; +#[non_exhaustive] pub enum IpStackStream { Tcp(IpStackTcpStream), Udp(IpStackUdpStream),