feat: change proxy mode to close connections if enable auto close connections

This commit is contained in:
Tunglies 2025-03-31 21:43:29 +08:00
parent cde8c4004f
commit cd4bfdd743
2 changed files with 40 additions and 0 deletions

View File

@ -37,6 +37,19 @@ pub fn restart_app() {
}); });
} }
fn after_change_clash_mode() {
tauri::async_runtime::block_on(tauri::async_runtime::spawn_blocking(|| {
tauri::async_runtime::block_on(async {
let connections = MihomoManager::global().get_connections().await.unwrap();
let connections = connections["connections"].as_array().unwrap();
for connection in connections {
let id = connection["id"].as_str().unwrap();
let _ = MihomoManager::global().delete_connection(id).await;
}
})
}));
}
/// Change Clash mode (rule/global/direct/script) /// Change Clash mode (rule/global/direct/script)
pub fn change_clash_mode(mode: String) { pub fn change_clash_mode(mode: String) {
let mut mapping = Mapping::new(); let mut mapping = Mapping::new();
@ -57,6 +70,14 @@ pub fn change_clash_mode(mode: String) {
logging_error!(Type::Tray, true, tray::Tray::global().update_menu()); logging_error!(Type::Tray, true, tray::Tray::global().update_menu());
logging_error!(Type::Tray, true, tray::Tray::global().update_icon(None)); logging_error!(Type::Tray, true, tray::Tray::global().update_icon(None));
} }
let is_auto_close_connection = Config::verge()
.data()
.auto_close_connection
.unwrap_or(false);
if is_auto_close_connection {
after_change_clash_mode();
}
} }
Err(err) => println!("{err}"), Err(err) => println!("{err}"),
} }

View File

@ -140,4 +140,23 @@ impl MihomoManager {
let response = self.send_request(Method::GET, url, None).await?; let response = self.send_request(Method::GET, url, None).await?;
Ok(response) Ok(response)
} }
pub async fn get_connections(&self) -> Result<serde_json::Value, String> {
let url = format!("{}/connections", self.mihomo_server);
let response = self.send_request(Method::GET, url, None).await?;
Ok(response)
}
pub async fn delete_connection(&self, id: &str) -> Result<(), String> {
let url = format!("{}/connections/{}", self.mihomo_server, id);
let response = self.send_request(Method::DELETE, url, None).await?;
if response["code"] == 204 {
Ok(())
} else {
Err(response["message"]
.as_str()
.unwrap_or("unknown error")
.to_string())
}
}
} }