mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-08 02:52:07 +08:00
- Drop the always-true second clause in use_tun's enhanced-mode check: the key is absent only when the unwrap_or default already makes current_mode == "fake-ip" true. - Collapse use_lowercase into a delegation to use_lowercase_owned.
56 lines
1.7 KiB
Rust
56 lines
1.7 KiB
Rust
use serde_yaml_ng::{Mapping, Value};
|
|
|
|
macro_rules! revise {
|
|
($map: expr, $key: expr, $val: expr) => {
|
|
let ret_key = Value::String($key.into());
|
|
$map.insert(ret_key, Value::from($val));
|
|
};
|
|
}
|
|
|
|
pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping {
|
|
let tun_key = Value::from("tun");
|
|
let tun_val = config.get(&tun_key);
|
|
let mut tun_val = tun_val.map_or_else(Mapping::new, |val| {
|
|
val.as_mapping().cloned().unwrap_or_else(Mapping::new)
|
|
});
|
|
|
|
if enable {
|
|
let dns_key = Value::from("dns");
|
|
let dns_val = config.get(&dns_key);
|
|
let mut dns_val = dns_val.map_or_else(Mapping::new, |val| {
|
|
val.as_mapping().cloned().unwrap_or_else(Mapping::new)
|
|
});
|
|
let ipv6_key = Value::from("ipv6");
|
|
let ipv6_val = config.get(&ipv6_key).and_then(|v| v.as_bool()).unwrap_or(false);
|
|
|
|
let current_mode = dns_val
|
|
.get(Value::from("enhanced-mode"))
|
|
.and_then(|v| v.as_str())
|
|
.unwrap_or("fake-ip");
|
|
|
|
if current_mode == "fake-ip" {
|
|
revise!(dns_val, "enable", true);
|
|
revise!(dns_val, "ipv6", ipv6_val);
|
|
|
|
if !dns_val.contains_key(Value::from("enhanced-mode")) {
|
|
revise!(dns_val, "enhanced-mode", "fake-ip");
|
|
}
|
|
|
|
if !dns_val.contains_key(Value::from("fake-ip-range")) {
|
|
revise!(dns_val, "fake-ip-range", "198.18.0.1/16");
|
|
}
|
|
|
|
if ipv6_val && !dns_val.contains_key(Value::from("fake-ip-range6")) {
|
|
revise!(dns_val, "fake-ip-range6", "2001:2::0/64");
|
|
}
|
|
}
|
|
|
|
revise!(config, "dns", dns_val);
|
|
}
|
|
|
|
revise!(tun_val, "enable", enable);
|
|
revise!(config, "tun", tun_val);
|
|
|
|
config
|
|
}
|