1use crate::{TproxyArgs, TproxyState};
2
3#[cfg(target_os = "linux")]
4use crate::linux::{_tproxy_remove, _tproxy_setup};
5
6#[cfg(target_os = "windows")]
7use crate::windows::{_tproxy_remove, _tproxy_setup};
8
9#[cfg(target_os = "macos")]
10use crate::macos::{_tproxy_remove, _tproxy_setup};
11
12impl Drop for TproxyState {
13 fn drop(&mut self) {
14 let inner = self.inner.clone();
15 tokio::spawn(async move {
16 log::debug!("restoring network settings");
17 let mut state = inner.lock().await;
18
19 _ = _tproxy_remove(&mut state).await;
20 });
21 }
22}
23
24pub async fn tproxy_setup(tproxy_args: &TproxyArgs) -> std::io::Result<TproxyState> {
25 log::debug!("Setting up TProxy with args: {tproxy_args:?}");
26 match _tproxy_setup(tproxy_args).await {
27 Ok(state) => {
28 log::debug!("TProxy setup completed successfully");
29 Ok(TproxyState::new(state))
30 }
31 Err(e) => {
32 log::error!("Failed to set up TProxy: {e}");
33 Err(std::io::Error::other(format!("{e}")))
34 }
35 }
36}
37
38pub async fn tproxy_remove(state: Option<TproxyState>) -> std::io::Result<()> {
39 match state {
40 Some(state) => {
41 let inner = state.inner.clone();
42 let mut state = inner.lock().await;
43 return _tproxy_remove(&mut state).await.map_err(|e| std::io::Error::other(format!("{e}")));
44 }
45 #[cfg(all(feature = "unsafe-state-file", any(target_os = "macos", target_os = "windows")))]
46 None => {
47 if let Ok(mut state) = crate::retrieve_intermediate_state() {
48 return _tproxy_remove(&mut state).await.map_err(|e| std::io::Error::other(format!("{e}")));
49 }
50 Ok(())
51 }
52 #[cfg(not(all(feature = "unsafe-state-file", any(target_os = "macos", target_os = "windows"))))]
53 None => Ok(()),
54 }
55}