Преглед на файлове

feat: adjust tun mode config

GyDi преди 2 години
родител
ревизия
178fd8e828
променени са 4 файла, в които са добавени 152 реда и са изтрити 68 реда
  1. 4 2
      src-tauri/src/config/mod.rs
  2. 81 0
      src-tauri/src/config/tun.rs
  3. 66 66
      src-tauri/src/core/clash.rs
  4. 1 0
      src-tauri/src/core/mod.rs

+ 4 - 2
src-tauri/src/config/mod.rs

@@ -1,10 +1,12 @@
 mod field;
 mod merge;
 mod script;
+mod tun;
 
 pub(self) use self::field::*;
 use self::merge::*;
 use self::script::*;
+use self::tun::*;
 use crate::core::PrfData;
 use serde_yaml::Mapping;
 use std::collections::HashMap;
@@ -16,10 +18,9 @@ pub fn runtime_config(
   profile_config: Mapping,
   profile_enhanced: Vec<PrfData>,
   valid: Vec<String>,
-  //   tun_enable: bool,
+  tun_mode: bool,
 ) -> (Mapping, HashMap<String, ResultLog>) {
   let mut config = profile_config;
-
   let mut result_map = HashMap::new();
 
   profile_enhanced.into_iter().for_each(|data| {
@@ -51,6 +52,7 @@ pub fn runtime_config(
   }
 
   config = use_filter(config, use_clash_fields());
+  config = use_tun(config, tun_mode);
   config = use_sort(config);
 
   (config, result_map)

+ 81 - 0
src-tauri/src/config/tun.rs

@@ -0,0 +1,81 @@
+use serde_yaml::{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));
+  };
+}
+
+// if key not exists then append value
+macro_rules! append {
+  ($map: expr, $key: expr, $val: expr) => {
+    let ret_key = Value::String($key.into());
+    if !$map.contains_key(&ret_key) {
+      $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);
+
+  if !enable && tun_val.is_none() {
+    return config;
+  }
+
+  let mut tun_val = tun_val.map_or(Mapping::new(), |val| {
+    val.as_mapping().cloned().unwrap_or(Mapping::new())
+  });
+
+  revise!(tun_val, "enable", enable);
+  if enable {
+    append!(tun_val, "stack", "gvisor");
+    append!(tun_val, "dns-hijack", vec!["198.18.0.2:53"]);
+    append!(tun_val, "auto-route", true);
+    append!(tun_val, "auto-detect-interface", true);
+  }
+
+  revise!(config, "tun", tun_val);
+
+  if enable {
+    use_dns_for_tun(config)
+  } else {
+    config
+  }
+}
+
+fn use_dns_for_tun(mut config: Mapping) -> Mapping {
+  let dns_key = Value::from("dns");
+  let dns_val = config.get(&dns_key);
+
+  let mut dns_val = dns_val.map_or(Mapping::new(), |val| {
+    val.as_mapping().cloned().unwrap_or(Mapping::new())
+  });
+
+  // 开启tun将同时开启dns
+  revise!(dns_val, "enable", true);
+
+  // 借鉴cfw的默认配置
+  append!(dns_val, "enhanced-mode", "fake-ip");
+  append!(
+    dns_val,
+    "nameserver",
+    vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"]
+  );
+  append!(dns_val, "fallback", vec![] as Vec<&str>);
+
+  #[cfg(target_os = "windows")]
+  append!(
+    dns_val,
+    "fake-ip-filter",
+    vec![
+      "dns.msftncsi.com",
+      "www.msftncsi.com",
+      "www.msftconnecttest.com"
+    ]
+  );
+  revise!(config, "dns", dns_val);
+  config
+}

+ 66 - 66
src-tauri/src/core/clash.rs

@@ -163,79 +163,79 @@ impl Clash {
     Ok((change_port, change_mode))
   }
 
-  /// revise the `tun` and `dns` config
-  pub fn _tun_mode(mut config: Mapping, enable: bool) -> Mapping {
-    macro_rules! revise {
-      ($map: expr, $key: expr, $val: expr) => {
-        let ret_key = Value::String($key.into());
-        $map.insert(ret_key, Value::from($val));
-      };
-    }
-
-    // if key not exists then append value
-    macro_rules! append {
-      ($map: expr, $key: expr, $val: expr) => {
-        let ret_key = Value::String($key.into());
-        if !$map.contains_key(&ret_key) {
-          $map.insert(ret_key, Value::from($val));
-        }
-      };
-    }
-
-    // tun config
-    let tun_val = config.get(&Value::from("tun"));
-    let mut new_tun = Mapping::new();
+  // /// revise the `tun` and `dns` config
+  // pub fn _tun_mode(mut config: Mapping, enable: bool) -> Mapping {
+  //   macro_rules! revise {
+  //     ($map: expr, $key: expr, $val: expr) => {
+  //       let ret_key = Value::String($key.into());
+  //       $map.insert(ret_key, Value::from($val));
+  //     };
+  //   }
 
-    if tun_val.is_some() && tun_val.as_ref().unwrap().is_mapping() {
-      new_tun = tun_val.as_ref().unwrap().as_mapping().unwrap().clone();
-    }
+  //   // if key not exists then append value
+  //   macro_rules! append {
+  //     ($map: expr, $key: expr, $val: expr) => {
+  //       let ret_key = Value::String($key.into());
+  //       if !$map.contains_key(&ret_key) {
+  //         $map.insert(ret_key, Value::from($val));
+  //       }
+  //     };
+  //   }
 
-    revise!(new_tun, "enable", enable);
+  //   // tun config
+  //   let tun_val = config.get(&Value::from("tun"));
+  //   let mut new_tun = Mapping::new();
 
-    if enable {
-      append!(new_tun, "stack", "gvisor");
-      append!(new_tun, "dns-hijack", vec!["198.18.0.2:53"]);
-      append!(new_tun, "auto-route", true);
-      append!(new_tun, "auto-detect-interface", true);
-    }
+  //   if tun_val.is_some() && tun_val.as_ref().unwrap().is_mapping() {
+  //     new_tun = tun_val.as_ref().unwrap().as_mapping().unwrap().clone();
+  //   }
 
-    revise!(config, "tun", new_tun);
+  //   revise!(new_tun, "enable", enable);
 
-    if enable {
-      // dns config
-      let dns_val = config.get(&Value::from("dns"));
-      let mut new_dns = Mapping::new();
+  //   if enable {
+  //     append!(new_tun, "stack", "gvisor");
+  //     append!(new_tun, "dns-hijack", vec!["198.18.0.2:53"]);
+  //     append!(new_tun, "auto-route", true);
+  //     append!(new_tun, "auto-detect-interface", true);
+  //   }
 
-      if dns_val.is_some() && dns_val.as_ref().unwrap().is_mapping() {
-        new_dns = dns_val.as_ref().unwrap().as_mapping().unwrap().clone();
-      }
-      revise!(new_dns, "enable", enable);
-
-      // 借鉴cfw的默认配置
-      append!(new_dns, "enhanced-mode", "fake-ip");
-      append!(
-        new_dns,
-        "nameserver",
-        vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"]
-      );
-      append!(new_dns, "fallback", vec![] as Vec<&str>);
-
-      #[cfg(target_os = "windows")]
-      append!(
-        new_dns,
-        "fake-ip-filter",
-        vec![
-          "dns.msftncsi.com",
-          "www.msftncsi.com",
-          "www.msftconnecttest.com"
-        ]
-      );
-
-      revise!(config, "dns", new_dns);
-    }
+  //   revise!(config, "tun", new_tun);
+
+  //   if enable {
+  //     // dns config
+  //     let dns_val = config.get(&Value::from("dns"));
+  //     let mut new_dns = Mapping::new();
+
+  //     if dns_val.is_some() && dns_val.as_ref().unwrap().is_mapping() {
+  //       new_dns = dns_val.as_ref().unwrap().as_mapping().unwrap().clone();
+  //     }
+  //     revise!(new_dns, "enable", enable);
+
+  //     // 借鉴cfw的默认配置
+  //     append!(new_dns, "enhanced-mode", "fake-ip");
+  //     append!(
+  //       new_dns,
+  //       "nameserver",
+  //       vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"]
+  //     );
+  //     append!(new_dns, "fallback", vec![] as Vec<&str>);
+
+  //     #[cfg(target_os = "windows")]
+  //     append!(
+  //       new_dns,
+  //       "fake-ip-filter",
+  //       vec![
+  //         "dns.msftncsi.com",
+  //         "www.msftncsi.com",
+  //         "www.msftconnecttest.com"
+  //       ]
+  //     );
+
+  //     revise!(config, "dns", new_dns);
+  //   }
 
-    config
-  }
+  //   config
+  // }
 
   // /// only 5 default fields available (clash config fields)
   // /// convert to lowercase

+ 1 - 0
src-tauri/src/core/mod.rs

@@ -363,6 +363,7 @@ impl Core {
       profile_config,
       profile_enhanced.chain,
       profile_enhanced.valid,
+      tun_mode,
     );
 
     dbg!(result);