diff --git a/src/args.rs b/src/args.rs index 5a676df..10d9cb7 100644 --- a/src/args.rs +++ b/src/args.rs @@ -23,9 +23,17 @@ pub struct Args { pub tun: Option, /// File descriptor of the tun interface + #[cfg(unix)] #[arg(long, value_name = "fd", conflicts_with = "tun")] 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]. + #[cfg(unix)] + #[arg(long, conflicts_with = "tun")] + pub close_fd_on_drop: Option, + /// Create a tun interface in a newly created unprivileged namespace /// while maintaining proxy connectivity via the global network namespace. #[cfg(target_os = "linux")] @@ -104,7 +112,10 @@ impl Default for Args { Args { proxy: ArgProxy::default(), tun: None, + #[cfg(unix)] tun_fd: None, + #[cfg(unix)] + close_fd_on_drop: None, #[cfg(target_os = "linux")] unshare: false, #[cfg(target_os = "linux")] @@ -148,11 +159,18 @@ impl Args { self } + #[cfg(unix)] pub fn tun_fd(&mut self, tun_fd: Option) -> &mut Self { self.tun_fd = tun_fd; self } + #[cfg(unix)] + pub fn close_fd_on_drop(&mut self, close_fd_on_drop: bool) -> &mut Self { + self.close_fd_on_drop = Some(close_fd_on_drop); + self + } + pub fn verbosity(&mut self, verbosity: ArgVerbosity) -> &mut Self { self.verbosity = verbosity; self diff --git a/src/desktop_api.rs b/src/desktop_api.rs index f7c03bf..6e40ae3 100644 --- a/src/desktop_api.rs +++ b/src/desktop_api.rs @@ -86,11 +86,19 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can let mut tun_config = tun2::Configuration::default(); tun_config.address(TUN_IPV4).netmask(TUN_NETMASK).mtu(MTU).up(); tun_config.destination(TUN_GATEWAY); - if let Some(tun_fd) = args.tun_fd { - tun_config.raw_fd(tun_fd); + #[cfg(unix)] + if let Some(fd) = args.tun_fd { + tun_config.raw_fd(fd); + if let Some(v) = args.close_fd_on_drop { + tun_config.close_fd_on_drop(v); + }; } else if let Some(ref tun) = args.tun { tun_config.tun_name(tun); } + #[cfg(windows)] + if let Some(ref tun) = args.tun { + tun_config.tun_name(tun); + } #[cfg(target_os = "linux")] tun_config.platform_config(|cfg| { diff --git a/src/mobile_api.rs b/src/mobile_api.rs index 2750733..69e684c 100644 --- a/src/mobile_api.rs +++ b/src/mobile_api.rs @@ -33,6 +33,9 @@ pub fn mobile_run(args: Args, tun_mtu: u16, _packet_information: bool) -> c_int #[cfg(unix)] if let Some(fd) = args.tun_fd { config.raw_fd(fd); + if let Some(v) = args.close_fd_on_drop { + config.close_fd_on_drop(v); + }; } else if let Some(ref tun) = args.tun { config.tun_name(tun); }