From 3fb02f0fc7d2e058d7b0660f068ed58df81cbc1b Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:03:35 +0800 Subject: [PATCH] switch to tun crate instead of tun2 --- Cargo.toml | 2 +- src/args.rs | 5 ++--- src/desktop_api.rs | 6 +++--- src/mobile_api.rs | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3ad63ce..650e60f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ thiserror = "1" tokio = { version = "1", features = ["full"] } tokio-util = "0.7" tproxy-config = { version = "6", default-features = false } -tun2 = { version = "3", features = ["async"] } +tun = { version = "0.7", features = ["async"] } udp-stream = { version = "0.0.12", default-features = false } unicase = "2" url = "2" diff --git a/src/args.rs b/src/args.rs index 65d0998..4e71166 100644 --- a/src/args.rs +++ b/src/args.rs @@ -30,10 +30,9 @@ pub struct Args { pub tun_fd: Option, /// Set whether to close the received raw file descriptor on drop or not. - /// This setting is passed to the tun2 crate. - /// See [tun2::Configuration::close_fd_on_drop]. + /// This setting is dependent on [tun_fd]. #[cfg(unix)] - #[arg(long, value_name = "true or false", conflicts_with = "tun")] + #[arg(long, value_name = "true or false", conflicts_with = "tun", requires = "tun_fd")] pub close_fd_on_drop: Option, /// Create a tun interface in a newly created unprivileged namespace diff --git a/src/desktop_api.rs b/src/desktop_api.rs index 0617ad3..17428fb 100644 --- a/src/desktop_api.rs +++ b/src/desktop_api.rs @@ -6,7 +6,7 @@ use crate::{ }; use std::os::raw::{c_char, c_int}; use tproxy_config::{TproxyArgs, TUN_GATEWAY, TUN_IPV4, TUN_NETMASK}; -use tun2::{AbstractDevice, DEFAULT_MTU as MTU}; +use tun::{AbstractDevice, DEFAULT_MTU as MTU}; static TUN_QUIT: std::sync::Mutex> = std::sync::Mutex::new(None); @@ -83,7 +83,7 @@ pub unsafe extern "C" fn tun2proxy_with_name_run( pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::CancellationToken) -> std::io::Result<()> { let bypass_ips = args.bypass.clone(); - let mut tun_config = tun2::Configuration::default(); + let mut tun_config = tun::Configuration::default(); tun_config.address(TUN_IPV4).netmask(TUN_NETMASK).mtu(MTU).up(); tun_config.destination(TUN_GATEWAY); #[cfg(unix)] @@ -122,7 +122,7 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can #[allow(unused_mut, unused_assignments, unused_variables)] let mut setup = true; - let device = tun2::create_as_async(&tun_config)?; + let device = tun::create_as_async(&tun_config)?; if let Ok(tun_name) = device.tun_name() { tproxy_args = tproxy_args.tun_name(&tun_name); diff --git a/src/mobile_api.rs b/src/mobile_api.rs index 69e684c..a32f68d 100644 --- a/src/mobile_api.rs +++ b/src/mobile_api.rs @@ -28,7 +28,7 @@ pub fn mobile_run(args: Args, tun_mtu: u16, _packet_information: bool) -> c_int } let block = async move { - let mut config = tun2::Configuration::default(); + let mut config = tun::Configuration::default(); #[cfg(unix)] if let Some(fd) = args.tun_fd { @@ -49,7 +49,7 @@ pub fn mobile_run(args: Args, tun_mtu: u16, _packet_information: bool) -> c_int config.packet_information(_packet_information); }); - let device = tun2::create_as_async(&config).map_err(std::io::Error::from)?; + let device = tun::create_as_async(&config).map_err(std::io::Error::from)?; let join_handle = tokio::spawn(crate::run(device, tun_mtu, args, shutdown_token)); join_handle.await.map_err(std::io::Error::from)?