Files
clash-proxy/src-tauri/src/config/runtime.rs

50 lines
1.8 KiB
Rust
Raw Normal View History

2024-02-20 23:27:03 +08:00
use crate::enhance::field::use_keys;
2022-11-18 18:18:41 +08:00
use serde::{Deserialize, Serialize};
2024-02-20 23:27:03 +08:00
use serde_yaml::{Mapping, Value};
2022-11-18 18:18:41 +08:00
use std::collections::HashMap;
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct IRuntime {
pub config: Option<Mapping>,
// 记录在订阅中包括merge和script生成的出现过的keys
2022-11-18 18:18:41 +08:00
// 这些keys不一定都生效
pub exists_keys: Vec<String>,
pub chain_logs: HashMap<String, Vec<(String, String)>>,
}
impl IRuntime {
pub fn new() -> Self {
Self::default()
}
2024-02-20 23:27:03 +08:00
// 这里只更改 allow-lan | ipv6 | log-level | tun
pub fn patch_config(&mut self, patch: Mapping) {
if let Some(config) = self.config.as_mut() {
2024-11-05 16:24:58 +08:00
["allow-lan", "ipv6", "log-level", "unified-delay"]
.into_iter()
.for_each(|key| {
if let Some(value) = patch.get(key).to_owned() {
config.insert(key.into(), value.clone());
}
});
2024-10-23 10:34:14 +08:00
2024-02-20 23:27:03 +08:00
let patch_tun = patch.get("tun");
2024-10-23 10:34:14 +08:00
if patch_tun.is_some() {
let tun = config.get("tun");
let mut tun = tun.map_or(Mapping::new(), |val| {
val.as_mapping().cloned().unwrap_or(Mapping::new())
});
let patch_tun = patch_tun.map_or(Mapping::new(), |val| {
val.as_mapping().cloned().unwrap_or(Mapping::new())
});
use_keys(&patch_tun).into_iter().for_each(|key| {
if let Some(value) = patch_tun.get(&key).to_owned() {
tun.insert(key.into(), value.clone());
}
});
config.insert("tun".into(), Value::from(tun));
}
}
}
2022-11-18 18:18:41 +08:00
}