| 
					
				 | 
			
			
				@@ -35,461 +35,467 @@ static mut WINDOW_CLOSABLE: bool = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #[derive(Clone)] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 pub struct Core { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub clash: Arc<Mutex<Clash>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub clash: Arc<Mutex<Clash>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub verge: Arc<Mutex<Verge>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub verge: Arc<Mutex<Verge>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub profiles: Arc<Mutex<Profiles>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub profiles: Arc<Mutex<Profiles>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub service: Arc<Mutex<Service>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub service: Arc<Mutex<Service>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub sysopt: Arc<Mutex<Sysopt>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub sysopt: Arc<Mutex<Sysopt>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub timer: Arc<Mutex<Timer>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub timer: Arc<Mutex<Timer>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub window: Arc<Mutex<Option<Window>>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub window: Arc<Mutex<Option<Window>>>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 impl Core { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn new() -> Core { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let clash = Clash::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let verge = Verge::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let profiles = Profiles::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let service = Service::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Core { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            clash: Arc::new(Mutex::new(clash)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            verge: Arc::new(Mutex::new(verge)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            profiles: Arc::new(Mutex::new(profiles)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            service: Arc::new(Mutex::new(service)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            sysopt: Arc::new(Mutex::new(Sysopt::new())), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            timer: Arc::new(Mutex::new(Timer::new())), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            window: Arc::new(Mutex::new(None)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn new() -> Core { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let clash = Clash::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let verge = Verge::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let profiles = Profiles::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let service = Service::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    Core { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      clash: Arc::new(Mutex::new(clash)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      verge: Arc::new(Mutex::new(verge)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      profiles: Arc::new(Mutex::new(profiles)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      service: Arc::new(Mutex::new(service)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      sysopt: Arc::new(Mutex::new(Sysopt::new())), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      timer: Arc::new(Mutex::new(Timer::new())), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      window: Arc::new(Mutex::new(None)), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// initialize the core state 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn init(&self, app_handle: tauri::AppHandle) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let clash_core = verge.clash_core.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// initialize the core state 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn init(&self, app_handle: tauri::AppHandle) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let clash_core = verge.clash_core.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mut service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        service.set_core(clash_core); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    service.set_core(clash_core); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        #[cfg(windows)] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let enable = verge.enable_service_mode.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            service.set_mode(enable.unwrap_or(false)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    #[cfg(windows)] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let enable = verge.enable_service_mode.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      service.set_mode(enable.unwrap_or(false)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        log_if_err!(service.start()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        drop(verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        drop(service); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    log_if_err!(service.start()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    drop(verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    drop(service); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        log_if_err!(self.activate()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    log_if_err!(self.activate()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let silent_start = verge.enable_silent_start.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let auto_launch = verge.enable_auto_launch.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let silent_start = verge.enable_silent_start.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let auto_launch = verge.enable_auto_launch.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // silent start 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if silent_start.unwrap_or(false) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            window.as_ref().map(|win| { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                win.hide().unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // silent start 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if silent_start.unwrap_or(false) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      window.as_ref().map(|win| { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        win.hide().unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mut sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        sysopt.init_sysproxy(clash.info.port.clone(), &verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    sysopt.init_sysproxy(clash.info.port.clone(), &verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        drop(clash); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        drop(verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    drop(clash); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    drop(verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        log_if_err!(sysopt.init_launch(auto_launch)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    log_if_err!(sysopt.init_launch(auto_launch)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        log_if_err!(self.update_systray(&app_handle)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    log_if_err!(self.update_systray(&app_handle)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // wait the window setup during resolve app 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let core = self.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        tauri::async_runtime::spawn(async move { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            sleep(Duration::from_secs(2)).await; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            log_if_err!(core.activate_enhanced(true)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // wait the window setup during resolve app 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let core = self.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    tauri::async_runtime::spawn(async move { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      sleep(Duration::from_secs(2)).await; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      log_if_err!(core.activate_enhanced(true)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // timer initialize 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mut timer = self.timer.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        timer.set_core(self.clone()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        log_if_err!(timer.refresh()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // timer initialize 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut timer = self.timer.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    timer.set_core(self.clone()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    log_if_err!(timer.refresh()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// save the window instance 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn set_win(&self, win: Option<Window>) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    *window = win; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// restart the clash sidecar 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn restart_clash(&self) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    service.restart()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    drop(service); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    self.activate()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    self.activate_enhanced(true) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// save the window instance 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn set_win(&self, win: Option<Window>) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mut window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        *window = win; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// change the clash core 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn change_core(&self, clash_core: Option<String>) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let clash_core = clash_core.unwrap_or("clash".into()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if &clash_core != "clash" && &clash_core != "clash-meta" { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      bail!("invalid clash core name \"{clash_core}\""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// restart the clash sidecar 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn restart_clash(&self) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mut service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        service.restart()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        drop(service); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    verge.patch_config(Verge { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      clash_core: Some(clash_core.clone()), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      ..Verge::default() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    })?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    drop(verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    service.stop()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    service.set_core(Some(clash_core)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    service.start()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    drop(service); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    self.activate()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    self.activate_enhanced(true) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// Patch Clash 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// handle the clash config changed 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn patch_clash(&self, patch: Mapping, app_handle: &AppHandle) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let ((changed_port, changed_mode), port) = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let mut clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      (clash.patch_config(patch)?, clash.info.port.clone()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // todo: port check 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if changed_port { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let mut service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      service.restart()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      drop(service); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      self.activate()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      self.activate_enhanced(true)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let mut sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      sysopt.init_sysproxy(port, &verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        self.activate()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        self.activate_enhanced(true) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if changed_mode { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      self.update_systray(app_handle)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// change the clash core 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn change_core(&self, clash_core: Option<String>) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let clash_core = clash_core.unwrap_or("clash".into()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if &clash_core != "clash" && &clash_core != "clash-meta" { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            bail!("invalid clash core name \"{clash_core}\""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// Patch Verge 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn patch_verge(&self, patch: Verge, app_handle: &AppHandle) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let tun_mode = patch.enable_tun_mode.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let auto_launch = patch.enable_auto_launch.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let system_proxy = patch.enable_system_proxy.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let proxy_bypass = patch.system_proxy_bypass.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let proxy_guard = patch.enable_proxy_guard.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mut verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        verge.patch_config(Verge { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            clash_core: Some(clash_core.clone()), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            ..Verge::default() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        })?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        drop(verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    #[cfg(windows)] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let service_mode = patch.enable_service_mode.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if service_mode.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        let service_mode = service_mode.unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         let mut service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         service.stop()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        service.set_core(Some(clash_core)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        service.set_mode(service_mode); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         service.start()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         drop(service); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        self.activate()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        self.activate_enhanced(true) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        self.activate_enhanced(false)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// Patch Clash 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// handle the clash config changed 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn patch_clash(&self, patch: Mapping, app_handle: &AppHandle) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let ((changed_port, changed_mode), port) = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let mut clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            (clash.patch_config(patch)?, clash.info.port.clone()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // todo: port check 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if changed_port { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let mut service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            service.restart()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            drop(service); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            self.activate()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            self.activate_enhanced(true)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let mut sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            sysopt.init_sysproxy(port, &verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if changed_mode { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            self.update_systray(app_handle)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if auto_launch.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let mut sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      sysopt.update_launch(auto_launch)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// Patch Verge 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn patch_verge(&self, patch: Verge, app_handle: &AppHandle) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let tun_mode = patch.enable_tun_mode.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let auto_launch = patch.enable_auto_launch.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let system_proxy = patch.enable_system_proxy.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let proxy_bypass = patch.system_proxy_bypass.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let proxy_guard = patch.enable_proxy_guard.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        #[cfg(windows)] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let service_mode = patch.enable_service_mode.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if service_mode.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                let service_mode = service_mode.unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                let mut service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                service.stop()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                service.set_mode(service_mode); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                service.start()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                drop(service); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                self.activate_enhanced(false)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if auto_launch.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let mut sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            sysopt.update_launch(auto_launch)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if system_proxy.is_some() || proxy_bypass.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let mut sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            sysopt.update_sysproxy(system_proxy.clone(), proxy_bypass)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            sysopt.guard_proxy(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if system_proxy.is_some() || proxy_bypass.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let mut sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      sysopt.update_sysproxy(system_proxy.clone(), proxy_bypass)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      sysopt.guard_proxy(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if proxy_guard.unwrap_or(false) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            sysopt.guard_proxy(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if proxy_guard.unwrap_or(false) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let sysopt = self.sysopt.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      sysopt.guard_proxy(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        #[cfg(target_os = "windows")] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if tun_mode.is_some() && *tun_mode.as_ref().unwrap_or(&false) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let wintun_dll = dirs::app_home_dir().join("wintun.dll"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if !wintun_dll.exists() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                bail!("failed to enable TUN for missing `wintun.dll`"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    #[cfg(target_os = "windows")] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if tun_mode.is_some() && *tun_mode.as_ref().unwrap_or(&false) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let wintun_dll = dirs::app_home_dir().join("wintun.dll"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if !wintun_dll.exists() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        bail!("failed to enable TUN for missing `wintun.dll`"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // save the patch 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mut verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        verge.patch_config(patch)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        drop(verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // save the patch 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    verge.patch_config(patch)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    drop(verge); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if system_proxy.is_some() || tun_mode.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            self.update_systray(app_handle)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if system_proxy.is_some() || tun_mode.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      self.update_systray(app_handle)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if tun_mode.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            self.activate_enhanced(false)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if tun_mode.is_some() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      self.activate_enhanced(false)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// update the system tray state 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn update_systray(&self, app_handle: &AppHandle) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let info = clash.info.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mode = info.mode.as_ref(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let tray = app_handle.tray_handle(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let system_proxy = verge.enable_system_proxy.as_ref(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let tun_mode = verge.enable_tun_mode.as_ref(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    tray 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .get_item("rule_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .set_selected((*mode.unwrap()).eq("rule"))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    tray 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .get_item("global_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .set_selected((*mode.unwrap()).eq("global"))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    tray 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .get_item("direct_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .set_selected((*mode.unwrap()).eq("direct"))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    tray 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .get_item("script_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .set_selected((*mode.unwrap()).eq("script"))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    tray 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .get_item("system_proxy") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .set_selected(*system_proxy.unwrap_or(&false))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    tray 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .get_item("tun_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      .set_selected(*tun_mode.unwrap_or(&false))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // update verge config 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let window = app_handle.get_window("main"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let notice = Notice::from(window); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    notice.refresh_verge(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// activate the profile 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// auto activate enhanced profile 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn activate(&self) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let data = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let profiles = self.profiles.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let data = profiles.gen_activate()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      Clash::strict_filter(data) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let (mut config, info) = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let config = clash.config.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let info = clash.info.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      (config, info) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    for (key, value) in data.into_iter() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      config.insert(key, value); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// update the system tray state 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn update_systray(&self, app_handle: &AppHandle) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let info = clash.info.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mode = info.mode.as_ref(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let tray = app_handle.tray_handle(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let system_proxy = verge.enable_system_proxy.as_ref(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let tun_mode = verge.enable_tun_mode.as_ref(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        tray.get_item("rule_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            .set_selected((*mode.unwrap()).eq("rule"))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        tray.get_item("global_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            .set_selected((*mode.unwrap()).eq("global"))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        tray.get_item("direct_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            .set_selected((*mode.unwrap()).eq("direct"))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        tray.get_item("script_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            .set_selected((*mode.unwrap()).eq("script"))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        tray.get_item("system_proxy") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            .set_selected(*system_proxy.unwrap_or(&false))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        tray.get_item("tun_mode") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            .set_selected(*tun_mode.unwrap_or(&false))?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // update verge config 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let window = app_handle.get_window("main"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let notice = Notice::from(window); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        notice.refresh_verge(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let config = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let tun_mode = verge.enable_tun_mode.unwrap_or(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      Clash::_tun_mode(config, tun_mode) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let notice = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      Notice::from(window.clone()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    service.set_config(info, config, notice) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// Enhanced 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// enhanced profiles mode 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn activate_enhanced(&self, skip: bool) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if window.is_none() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      bail!("failed to get the main window"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// activate the profile 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// auto activate enhanced profile 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn activate(&self) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let data = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let profiles = self.profiles.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let data = profiles.gen_activate()?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            Clash::strict_filter(data) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let (mut config, info) = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let config = clash.config.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let info = clash.info.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            (config, info) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        for (key, value) in data.into_iter() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            config.insert(key, value); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let event_name = help::get_uid("e"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let event_name = format!("enhanced-cb-{event_name}"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let config = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let tun_mode = verge.enable_tun_mode.unwrap_or(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            Clash::_tun_mode(config, tun_mode) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // generate the payload 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let payload = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let profiles = self.profiles.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      profiles.gen_enhanced(event_name.clone())? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let notice = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            Notice::from(window.clone()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // do not run enhanced 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if payload.chain.len() == 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if skip { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return Ok(()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        service.set_config(info, config, notice) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      drop(window); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      return self.activate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// Enhanced 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// enhanced profiles mode 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn activate_enhanced(&self, skip: bool) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if window.is_none() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            bail!("failed to get the main window"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let tun_mode = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      verge.enable_tun_mode.unwrap_or(false) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let event_name = help::get_uid("e"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let event_name = format!("enhanced-cb-{event_name}"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let info = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      clash.info.clone() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // generate the payload 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let payload = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let profiles = self.profiles.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            profiles.gen_enhanced(event_name.clone())? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let notice = Notice::from(window.clone()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let service = self.service.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // do not run enhanced 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if payload.chain.len() == 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if skip { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return Ok(()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let window = window.clone().unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    window.once(&event_name, move |event| { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let result = event.payload(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            drop(window); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return self.activate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if result.is_none() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log::warn!("event payload result is none"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let tun_mode = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            verge.enable_tun_mode.unwrap_or(false) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let result = result.unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let result: PrfEnhancedResult = serde_json::from_str(result).unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let info = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            clash.info.clone() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if let Some(data) = result.data { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        let mut config = Clash::read_config(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        let filter_data = Clash::loose_filter(data); // loose filter 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let notice = Notice::from(window.clone()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let service = self.service.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (key, value) in filter_data.into_iter() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+          config.insert(key, value); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let window = window.clone().unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        window.once(&event_name, move |event| { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let result = event.payload(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        let config = Clash::_tun_mode(config, tun_mode); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if result.is_none() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                log::warn!("event payload result is none"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        let service = service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log_if_err!(service.set_config(info, config, notice)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let result = result.unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let result: PrfEnhancedResult = serde_json::from_str(result).unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log::info!("profile enhanced status {}", result.status); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if let Some(data) = result.data { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                let mut config = Clash::read_config(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                let filter_data = Clash::loose_filter(data); // loose filter 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      result.error.map(|err| log::error!("{err}")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                for (key, value) in filter_data.into_iter() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    config.insert(key, value); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let silent_start = verge.enable_silent_start.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                let config = Clash::_tun_mode(config, tun_mode); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let closable = unsafe { WINDOW_CLOSABLE }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                let service = service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                log_if_err!(service.set_config(info, config, notice)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if silent_start.unwrap_or(false) && closable { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      unsafe { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        WINDOW_CLOSABLE = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                log::info!("profile enhanced status {}", result.status); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      window.emit("script-handler-close", payload).unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      window.emit("script-handler", payload).unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            result.error.map(|err| log::error!("{err}")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let verge = self.verge.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let silent_start = verge.enable_silent_start.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  // update rule/global/direct/script mode 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub fn update_mode(&self, app_handle: &AppHandle, mode: &str) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut mapping = Mapping::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    mapping.insert(Value::from("mode"), Value::from(mode)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let closable = unsafe { WINDOW_CLOSABLE }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    self.patch_clash(mapping, app_handle); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if silent_start.unwrap_or(false) && closable { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            unsafe { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                WINDOW_CLOSABLE = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let (config, info) = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let config = clash.config.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let info = clash.info.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      (config, info) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            window.emit("script-handler-close", payload).unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            window.emit("script-handler", payload).unwrap(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let notice = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      Notice::from(window.clone()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    service.patch_config(info, config, notice); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // update rule/global/direct/script mode 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub fn update_mode(&self, app_handle: &AppHandle, mode: &str) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mut mapping = Mapping::new(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        mapping.insert(Value::from("mode"), Value::from(mode)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        self.patch_clash(mapping, app_handle); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+impl Core { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// Static function 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  /// update profile item 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  pub async fn update_profile_item( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    core: Core, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    uid: String, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    option: Option<PrfOption>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  ) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let (url, opt) = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let profiles = core.profiles.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      let item = profiles.get_item(&uid)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if let Some(typ) = item.itype.as_ref() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // maybe only valid for `local` profile 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if *typ != "remote" { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+          // reactivate the config 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+          if Some(uid) == profiles.get_current() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            drop(profiles); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return core.activate_enhanced(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+          } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let (config, info) = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let clash = self.clash.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let config = clash.config.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let info = clash.info.clone(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            (config, info) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+          return Ok(()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let notice = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let window = self.window.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            Notice::from(window.clone()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if item.url.is_none() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        bail!("failed to get the profile item url"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let service = self.service.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        service.patch_config(info, config, notice); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      (item.url.clone().unwrap(), item.option.clone()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let merged_opt = PrfOption::merge(opt, option); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let item = PrfItem::from_url(&url, None, None, merged_opt).await?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-impl Core { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// Static function 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /// update profile item 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    pub async fn update_profile_item( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        core: Core, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        uid: String, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        option: Option<PrfOption>, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    ) -> Result<()> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let (url, opt) = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let profiles = core.profiles.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            let item = profiles.get_item(&uid)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if let Some(typ) = item.itype.as_ref() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                // maybe only valid for `local` profile 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                if *typ != "remote" { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    // reactivate the config 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    if Some(uid) == profiles.get_current() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        drop(profiles); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        return core.activate_enhanced(false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    return Ok(()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if item.url.is_none() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                bail!("failed to get the profile item url"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            (item.url.clone().unwrap(), item.option.clone()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let merged_opt = PrfOption::merge(opt, option); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let item = PrfItem::from_url(&url, None, None, merged_opt).await?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        let mut profiles = core.profiles.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        profiles.update_item(uid.clone(), item)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // reactivate the profile 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if Some(uid) == profiles.get_current() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            drop(profiles); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            core.activate_enhanced(false)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    let mut profiles = core.profiles.lock(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    profiles.update_item(uid.clone(), item)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // reactivate the profile 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if Some(uid) == profiles.get_current() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      drop(profiles); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      core.activate_enhanced(false)?; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    Ok(()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |