fix: enhanced profile consistency
This commit is contained in:
parent
ef47a74920
commit
7108d5f3ab
@ -1,10 +1,9 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
core::{ClashInfo, PrfItem, Profiles, VergeConfig},
|
core::{ClashInfo, PrfItem, Profiles, VergeConfig},
|
||||||
ret_err,
|
|
||||||
states::{ClashState, ProfilesState, VergeState},
|
states::{ClashState, ProfilesState, VergeState},
|
||||||
utils::{dirs, sysopt::SysProxyConfig},
|
utils::{dirs, sysopt::SysProxyConfig},
|
||||||
wrap_err,
|
|
||||||
};
|
};
|
||||||
|
use crate::{ret_err, wrap_err};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use serde_yaml::Mapping;
|
use serde_yaml::Mapping;
|
||||||
use std::{path::PathBuf, process::Command};
|
use std::{path::PathBuf, process::Command};
|
||||||
@ -80,7 +79,7 @@ pub async fn update_profile(
|
|||||||
// reactivate the profile
|
// reactivate the profile
|
||||||
if Some(index) == profiles.get_current() {
|
if Some(index) == profiles.get_current() {
|
||||||
let clash = clash_state.0.lock().unwrap();
|
let clash = clash_state.0.lock().unwrap();
|
||||||
wrap_err!(clash.activate(&profiles))?;
|
wrap_err!(clash.activate(&profiles, false))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -97,7 +96,7 @@ pub fn select_profile(
|
|||||||
wrap_err!(profiles.put_current(index))?;
|
wrap_err!(profiles.put_current(index))?;
|
||||||
|
|
||||||
let clash = clash_state.0.lock().unwrap();
|
let clash = clash_state.0.lock().unwrap();
|
||||||
wrap_err!(clash.activate(&profiles))
|
wrap_err!(clash.activate(&profiles, false))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// change the profile chain
|
/// change the profile chain
|
||||||
@ -108,16 +107,13 @@ pub fn change_profile_chain(
|
|||||||
clash_state: State<'_, ClashState>,
|
clash_state: State<'_, ClashState>,
|
||||||
profiles_state: State<'_, ProfilesState>,
|
profiles_state: State<'_, ProfilesState>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let clash = clash_state.0.lock().unwrap();
|
let mut clash = clash_state.0.lock().unwrap();
|
||||||
let mut profiles = profiles_state.0.lock().unwrap();
|
let mut profiles = profiles_state.0.lock().unwrap();
|
||||||
|
|
||||||
profiles.put_chain(chain);
|
profiles.put_chain(chain);
|
||||||
|
clash.set_window(app_handle.get_window("main"));
|
||||||
|
|
||||||
app_handle
|
wrap_err!(clash.activate_enhanced(&profiles, false))
|
||||||
.get_window("main")
|
|
||||||
.map(|win| wrap_err!(clash.activate_enhanced(&profiles, win, false)));
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// manually exec enhanced profile
|
/// manually exec enhanced profile
|
||||||
@ -127,14 +123,12 @@ pub fn enhance_profiles(
|
|||||||
clash_state: State<'_, ClashState>,
|
clash_state: State<'_, ClashState>,
|
||||||
profiles_state: State<'_, ProfilesState>,
|
profiles_state: State<'_, ProfilesState>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let clash = clash_state.0.lock().unwrap();
|
let mut clash = clash_state.0.lock().unwrap();
|
||||||
let profiles = profiles_state.0.lock().unwrap();
|
let profiles = profiles_state.0.lock().unwrap();
|
||||||
|
|
||||||
app_handle
|
clash.set_window(app_handle.get_window("main"));
|
||||||
.get_window("main")
|
|
||||||
.map(|win| wrap_err!(clash.activate_enhanced(&profiles, win, false)));
|
|
||||||
|
|
||||||
Ok(())
|
wrap_err!(clash.activate_enhanced(&profiles, false))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// delete profile item
|
/// delete profile item
|
||||||
@ -148,7 +142,7 @@ pub fn delete_profile(
|
|||||||
|
|
||||||
if wrap_err!(profiles.delete_item(index))? {
|
if wrap_err!(profiles.delete_item(index))? {
|
||||||
let clash = clash_state.0.lock().unwrap();
|
let clash = clash_state.0.lock().unwrap();
|
||||||
wrap_err!(clash.activate(&profiles))?;
|
wrap_err!(clash.activate(&profiles, false))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -291,7 +285,7 @@ pub fn patch_verge_config(
|
|||||||
|
|
||||||
wrap_err!(clash.tun_mode(tun_mode.unwrap()))?;
|
wrap_err!(clash.tun_mode(tun_mode.unwrap()))?;
|
||||||
clash.update_config();
|
clash.update_config();
|
||||||
wrap_err!(clash.activate(&profiles))?;
|
wrap_err!(clash.activate(&profiles, false))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use serde_yaml::{Mapping, Value};
|
use serde_yaml::{Mapping, Value};
|
||||||
use std::{collections::HashMap, time::Duration};
|
use std::{collections::HashMap, time::Duration};
|
||||||
use tauri::api::process::{Command, CommandChild, CommandEvent};
|
use tauri::api::process::{Command, CommandChild, CommandEvent};
|
||||||
|
use tauri::Window;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||||
@ -23,7 +24,6 @@ pub struct ClashInfo {
|
|||||||
pub secret: Option<String>,
|
pub secret: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Clash {
|
pub struct Clash {
|
||||||
/// maintain the clash config
|
/// maintain the clash config
|
||||||
pub config: Mapping,
|
pub config: Mapping,
|
||||||
@ -33,6 +33,9 @@ pub struct Clash {
|
|||||||
|
|
||||||
/// clash sidecar
|
/// clash sidecar
|
||||||
pub sidecar: Option<CommandChild>,
|
pub sidecar: Option<CommandChild>,
|
||||||
|
|
||||||
|
/// save the main window
|
||||||
|
pub window: Option<Window>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clash {
|
impl Clash {
|
||||||
@ -44,6 +47,7 @@ impl Clash {
|
|||||||
config,
|
config,
|
||||||
info,
|
info,
|
||||||
sidecar: None,
|
sidecar: None,
|
||||||
|
window: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,6 +118,11 @@ impl Clash {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// save the main window
|
||||||
|
pub fn set_window(&mut self, win: Option<Window>) {
|
||||||
|
self.window = win;
|
||||||
|
}
|
||||||
|
|
||||||
/// run clash sidecar
|
/// run clash sidecar
|
||||||
pub fn run_sidecar(&mut self) -> Result<()> {
|
pub fn run_sidecar(&mut self) -> Result<()> {
|
||||||
let app_dir = dirs::app_home_dir();
|
let app_dir = dirs::app_home_dir();
|
||||||
@ -156,7 +165,7 @@ impl Clash {
|
|||||||
self.update_config();
|
self.update_config();
|
||||||
self.drop_sidecar()?;
|
self.drop_sidecar()?;
|
||||||
self.run_sidecar()?;
|
self.run_sidecar()?;
|
||||||
self.activate(profiles)
|
self.activate(profiles, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// update the clash info
|
/// update the clash info
|
||||||
@ -241,12 +250,15 @@ impl Clash {
|
|||||||
self.save_config()
|
self.save_config()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// activate the profile
|
||||||
|
/// generate a new profile to the temp_dir
|
||||||
|
/// then put the path to the clash core
|
||||||
fn _activate(info: ClashInfo, config: Mapping) -> Result<()> {
|
fn _activate(info: ClashInfo, config: Mapping) -> Result<()> {
|
||||||
let temp_path = dirs::profiles_temp_path();
|
let temp_path = dirs::profiles_temp_path();
|
||||||
config::save_yaml(temp_path.clone(), &config, Some("# Clash Verge Temp File"))?;
|
config::save_yaml(temp_path.clone(), &config, Some("# Clash Verge Temp File"))?;
|
||||||
|
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
let server = info.server.clone().unwrap();
|
let server = info.server.unwrap();
|
||||||
let server = format!("http://{server}/configs");
|
let server = format!("http://{server}/configs");
|
||||||
|
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
@ -263,22 +275,20 @@ impl Clash {
|
|||||||
// retry 5 times
|
// retry 5 times
|
||||||
for _ in 0..5 {
|
for _ in 0..5 {
|
||||||
match reqwest::ClientBuilder::new().no_proxy().build() {
|
match reqwest::ClientBuilder::new().no_proxy().build() {
|
||||||
Ok(client) => match client
|
Ok(client) => {
|
||||||
.put(&server)
|
let builder = client.put(&server).headers(headers.clone()).json(&data);
|
||||||
.headers(headers.clone())
|
|
||||||
.json(&data)
|
match builder.send().await {
|
||||||
.send()
|
Ok(resp) => {
|
||||||
.await
|
if resp.status() != 204 {
|
||||||
{
|
log::error!("failed to activate clash for status \"{}\"", resp.status());
|
||||||
Ok(resp) => {
|
}
|
||||||
if resp.status() != 204 {
|
// do not retry
|
||||||
log::error!("failed to activate clash for status \"{}\"", resp.status());
|
break;
|
||||||
}
|
}
|
||||||
// do not retry
|
Err(err) => log::error!("failed to activate for `{err}`"),
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
Err(err) => log::error!("failed to activate for `{err}`"),
|
}
|
||||||
},
|
|
||||||
Err(err) => log::error!("failed to activate for `{err}`"),
|
Err(err) => log::error!("failed to activate for `{err}`"),
|
||||||
}
|
}
|
||||||
sleep(Duration::from_millis(500)).await;
|
sleep(Duration::from_millis(500)).await;
|
||||||
@ -288,26 +298,14 @@ impl Clash {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// activate the profile
|
/// enhanced profiles mode
|
||||||
pub fn activate(&self, profiles: &Profiles) -> Result<()> {
|
/// only change the enhanced profiles
|
||||||
let info = self.info.clone();
|
pub fn activate_enhanced(&self, profiles: &Profiles, delay: bool) -> Result<()> {
|
||||||
let mut config = self.config.clone();
|
if self.window.is_none() {
|
||||||
let gen_map = profiles.gen_activate()?;
|
bail!("failed to get the main window");
|
||||||
|
|
||||||
for (key, value) in gen_map.into_iter() {
|
|
||||||
config.insert(key, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Self::_activate(info, config)
|
let win = self.window.clone().unwrap();
|
||||||
}
|
|
||||||
|
|
||||||
/// enhanced profiles mode
|
|
||||||
pub fn activate_enhanced(
|
|
||||||
&self,
|
|
||||||
profiles: &Profiles,
|
|
||||||
win: tauri::Window,
|
|
||||||
delay: bool,
|
|
||||||
) -> Result<()> {
|
|
||||||
let event_name = help::get_uid("e");
|
let event_name = help::get_uid("e");
|
||||||
let event_name = format!("enhanced-cb-{event_name}");
|
let event_name = format!("enhanced-cb-{event_name}");
|
||||||
|
|
||||||
@ -344,6 +342,21 @@ impl Clash {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// activate the profile
|
||||||
|
/// auto activate enhanced profile
|
||||||
|
pub fn activate(&self, profiles: &Profiles, delay: bool) -> Result<()> {
|
||||||
|
let gen_map = profiles.gen_activate()?;
|
||||||
|
let info = self.info.clone();
|
||||||
|
let mut config = self.config.clone();
|
||||||
|
|
||||||
|
for (key, value) in gen_map.into_iter() {
|
||||||
|
config.insert(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::_activate(info, config)?;
|
||||||
|
self.activate_enhanced(profiles, delay)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Clash {
|
impl Default for Clash {
|
||||||
|
@ -24,12 +24,9 @@ pub fn resolve_setup(app: &App) {
|
|||||||
log_if_err!(clash.run_sidecar());
|
log_if_err!(clash.run_sidecar());
|
||||||
|
|
||||||
*profiles = Profiles::read_file();
|
*profiles = Profiles::read_file();
|
||||||
log_if_err!(clash.activate(&profiles));
|
|
||||||
|
|
||||||
match app.get_window("main") {
|
clash.set_window(app.get_window("main"));
|
||||||
Some(win) => log_if_err!(clash.activate_enhanced(&profiles, win, true)),
|
log_if_err!(clash.activate(&profiles, true));
|
||||||
None => log::error!("failed to get window for enhanced profiles"),
|
|
||||||
};
|
|
||||||
|
|
||||||
verge.init_sysproxy(clash.info.port.clone());
|
verge.init_sysproxy(clash.info.port.clone());
|
||||||
// enable tun mode
|
// enable tun mode
|
||||||
|
Loading…
x
Reference in New Issue
Block a user