Jelajahi Sumber

feat: change system porxy when changed port

GyDi 3 tahun lalu
induk
melakukan
47155a4a29
3 mengubah file dengan 38 tambahan dan 4 penghapusan
  1. 3 1
      src-tauri/src/cmds.rs
  2. 17 3
      src-tauri/src/core/clash.rs
  3. 18 0
      src-tauri/src/core/verge.rs

+ 3 - 1
src-tauri/src/cmds.rs

@@ -202,11 +202,13 @@ pub fn get_clash_info(clash_state: State<'_, ClashState>) -> Result<ClashInfo, S
 pub fn patch_clash_config(
   payload: Mapping,
   clash_state: State<'_, ClashState>,
+  verge_state: State<'_, VergeState>,
   profiles_state: State<'_, ProfilesState>,
 ) -> Result<(), String> {
   let mut clash = clash_state.0.lock().unwrap();
+  let mut verge = verge_state.0.lock().unwrap();
   let mut profiles = profiles_state.0.lock().unwrap();
-  clash.patch_config(payload, &mut profiles)
+  clash.patch_config(payload, &mut verge, &mut profiles)
 }
 
 /// get the system proxy

+ 17 - 3
src-tauri/src/core/clash.rs

@@ -1,4 +1,4 @@
-use super::ProfilesConfig;
+use super::{ProfilesConfig, Verge};
 use crate::utils::{config, dirs};
 use serde::{Deserialize, Serialize};
 use serde_yaml::{Mapping, Value};
@@ -170,6 +170,7 @@ impl Clash {
   pub fn patch_config(
     &mut self,
     patch: Mapping,
+    verge: &mut Verge,
     profiles: &mut ProfilesConfig,
   ) -> Result<(), String> {
     for (key, value) in patch.iter() {
@@ -179,12 +180,25 @@ impl Clash {
       // restart the clash
       if key_str == "mixed-port" {
         self.restart_sidecar(profiles)?;
+
+        let port = if value.is_number() {
+          match value.as_i64().clone() {
+            Some(num) => Some(format!("{num}")),
+            None => None,
+          }
+        } else {
+          match value.as_str().clone() {
+            Some(num) => Some(num.into()),
+            None => None,
+          }
+        };
+        verge.init_sysproxy(port);
       }
 
       if self.config.contains_key(key) {
-        self.config[key] = value.clone();
+        self.config[key] = value;
       } else {
-        self.config.insert(key.clone(), value.clone());
+        self.config.insert(key.clone(), value);
       }
     }
     self.save_config()

+ 18 - 0
src-tauri/src/core/verge.rs

@@ -161,6 +161,24 @@ impl Verge {
     }
   }
 
+  // fn guard_thread(&mut self) -> Result<(), String> {
+  //   let sysproxy = self.cur_sysproxy.clone();
+
+  //   use std::{thread, time};
+  //   tauri::async_runtime::spawn(async move {
+  //     if let Some(sysproxy) = sysproxy {
+  //       sysproxy.set_sys();
+  //     }
+
+  //     let ten_millis = time::Duration::from_millis(10);
+  //     let now = time::Instant::now();
+
+  //     thread::sleep(ten_millis);
+  //   });
+
+  //   Ok(())
+  // }
+
   /// patch verge config
   /// There should be only one update at a time here
   /// so call the save_file at the end is savely