Skip to content

Commit

Permalink
update tun2 version
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Jan 22, 2024
1 parent 9be0637 commit 5fb3fdc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -43,6 +49,10 @@ async fn main(){
copy_from_lhs_to_rhs(udp, rhs).await;
});
}
_ => {
println!("unknown transport");
continue;
}
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions examples/tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
//!
//! 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.
//! ```
//! target/debug/examples/echo 127.0.0.1:8080
//! ```
//! 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.
Expand Down Expand Up @@ -56,13 +56,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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();
Expand Down Expand Up @@ -136,6 +137,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("unknown transport - {} bytes", payload.len());
continue;
}
_ => {
println!("unknown transport");
continue;
}
};
}
}
1 change: 1 addition & 0 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod tcp;
mod udp;
mod unknown;

#[non_exhaustive]
pub enum IpStackStream {
Tcp(IpStackTcpStream),
Udp(IpStackUdpStream),
Expand Down

0 comments on commit 5fb3fdc

Please sign in to comment.