Use destructor to restore network config

This commit is contained in:
B. Blechschmidt 2024-04-07 23:08:32 +02:00
parent 0239a225a1
commit ebbe939f85
2 changed files with 5 additions and 10 deletions

View file

@ -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 }

View file

@ -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<tproxy_config::TproxyState> = 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<tproxy_config::TproxyState> = 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>(())
}