fix: optimize clash launch

This commit is contained in:
GyDi 2022-04-15 01:29:25 +08:00
parent f5d0513d1f
commit 844ffab4ed
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
2 changed files with 32 additions and 34 deletions

View File

@ -69,10 +69,10 @@ impl Clash {
/// parse the clash's config.yaml /// parse the clash's config.yaml
/// get some information /// get some information
fn get_info(clash_config: &Mapping) -> ClashInfo { fn get_info(clash_config: &Mapping) -> ClashInfo {
let key_port_1 = Value::String("port".to_string()); let key_port_1 = Value::from("port");
let key_port_2 = Value::String("mixed-port".to_string()); let key_port_2 = Value::from("mixed-port");
let key_server = Value::String("external-controller".to_string()); let key_server = Value::from("external-controller");
let key_secret = Value::String("secret".to_string()); let key_secret = Value::from("secret");
let port = match clash_config.get(&key_port_1) { let port = match clash_config.get(&key_port_1) {
Some(value) => match value { Some(value) => match value {
@ -136,31 +136,31 @@ impl Clash {
} }
/// run clash sidecar /// run clash sidecar
pub fn run_sidecar(&mut self) -> Result<()> { pub fn run_sidecar(&mut self, profiles: &Profiles, delay: bool) -> Result<()> {
let app_dir = dirs::app_home_dir(); let app_dir = dirs::app_home_dir();
let app_dir = app_dir.as_os_str().to_str().unwrap(); let app_dir = app_dir.as_os_str().to_str().unwrap();
match Command::new_sidecar("clash") { let cmd = Command::new_sidecar("clash")?;
Ok(cmd) => match cmd.args(["-d", app_dir]).spawn() { let (mut rx, cmd_child) = cmd.args(["-d", app_dir]).spawn()?;
Ok((mut rx, cmd_child)) => {
self.sidecar = Some(cmd_child);
// clash log self.sidecar = Some(cmd_child);
tauri::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await { // clash log
match event { tauri::async_runtime::spawn(async move {
CommandEvent::Stdout(line) => log::info!("[clash]: {}", line), while let Some(event) = rx.recv().await {
CommandEvent::Stderr(err) => log::error!("[clash]: {}", err), match event {
_ => {} CommandEvent::Stdout(line) => log::info!("[clash]: {}", line),
} CommandEvent::Stderr(err) => log::error!("[clash]: {}", err),
} _ => {}
});
Ok(())
} }
Err(err) => bail!(err.to_string()), }
}, });
Err(err) => bail!(err.to_string()),
} // activate profile
log_if_err!(self.activate(&profiles));
log_if_err!(self.activate_enhanced(&profiles, delay, true));
Ok(())
} }
/// drop clash sidecar /// drop clash sidecar
@ -176,9 +176,7 @@ impl Clash {
pub fn restart_sidecar(&mut self, profiles: &mut Profiles) -> Result<()> { pub fn restart_sidecar(&mut self, profiles: &mut Profiles) -> Result<()> {
self.update_config(); self.update_config();
self.drop_sidecar()?; self.drop_sidecar()?;
self.run_sidecar()?; self.run_sidecar(profiles, false)
self.activate(profiles)?;
self.activate_enhanced(profiles, false, true)
} }
/// update the clash info /// update the clash info
@ -309,6 +307,10 @@ impl Clash {
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 {
if info.server.is_none() {
return;
}
let server = info.server.unwrap(); let server = info.server.unwrap();
let server = format!("http://{server}/configs"); let server = format!("http://{server}/configs");

View File

@ -1,5 +1,5 @@
use super::{init, server}; use crate::log_if_err;
use crate::{core::Profiles, log_if_err, states}; use crate::{core::Profiles, states, utils::init, utils::server};
use tauri::{App, AppHandle, Manager}; use tauri::{App, AppHandle, Manager};
/// handle something when start app /// handle something when start app
@ -19,16 +19,12 @@ pub fn resolve_setup(app: &App) {
let mut verge = verge_state.0.lock().unwrap(); let mut verge = verge_state.0.lock().unwrap();
let mut profiles = profiles_state.0.lock().unwrap(); let mut profiles = profiles_state.0.lock().unwrap();
log_if_err!(clash.run_sidecar());
*profiles = Profiles::read_file(); *profiles = Profiles::read_file();
clash.set_window(app.get_window("main")); clash.set_window(app.get_window("main"));
log_if_err!(clash.activate(&profiles)); log_if_err!(clash.run_sidecar(&profiles, true));
log_if_err!(clash.activate_enhanced(&profiles, true, true));
verge.init_sysproxy(clash.info.port.clone()); verge.init_sysproxy(clash.info.port.clone());
log_if_err!(verge.init_launch()); log_if_err!(verge.init_launch());
verge.config.enable_system_proxy.map(|enable| { verge.config.enable_system_proxy.map(|enable| {