feat: change system porxy when changed port

This commit is contained in:
GyDi 2022-01-21 02:50:13 +08:00
parent d0b87fd7c3
commit 47155a4a29
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
3 changed files with 38 additions and 4 deletions

View File

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

View File

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

View File

@ -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 /// patch verge config
/// There should be only one update at a time here /// There should be only one update at a time here
/// so call the save_file at the end is savely /// so call the save_file at the end is savely