From ebbe939f857531655d36d70bb77059f7616182f3 Mon Sep 17 00:00:00 2001 From: "B. Blechschmidt" Date: Sun, 7 Apr 2024 23:08:32 +0200 Subject: [PATCH] Use destructor to restore network config --- Cargo.toml | 2 +- src/desktop_api.rs | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 65e1f74..7e2588f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ socks5-impl = { version = "0.5" } thiserror = "1.0" tokio = { version = "1.36", features = ["full"] } tokio-util = "0.7" -tproxy-config = { version = "3.0", features = ["log"] } +tproxy-config = { version = ">=3.0.2", features = ["log"] } trust-dns-proto = "0.23" tun2 = { version = "1.2", features = ["async"] } udp-stream = { version = "0.0", default-features = false } diff --git a/src/desktop_api.rs b/src/desktop_api.rs index 3364bb8..44f16c4 100644 --- a/src/desktop_api.rs +++ b/src/desktop_api.rs @@ -119,7 +119,9 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can tproxy_args = tproxy_args.tun_name(&tun_name); } - let mut restore: Option = None; + // TproxyState implements the Drop trait to restore network configuration, + // so we we need to assign it to a variable, even if it is not used. + let mut _restore: Option = None; #[cfg(target_os = "linux")] { @@ -128,7 +130,7 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can #[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))] if setup { - restore = Some(tproxy_config::tproxy_setup(&tproxy_args)?); + _restore = Some(tproxy_config::tproxy_setup(&tproxy_args)?); } #[cfg(target_os = "linux")] @@ -191,13 +193,6 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can let join_handle = tokio::spawn(crate::run(device, MTU, args, shutdown_token)); join_handle.await.map_err(std::io::Error::from)??; - #[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))] - if setup { - // TODO: This probably should be handled by a destructor - // since otherwise removal is not guaranteed if anything above returns early. - tproxy_config::tproxy_remove(restore)?; - } - Ok::<(), std::io::Error>(()) }