From c73b3543860059be0a777a417bf5de12cdf8c9eb Mon Sep 17 00:00:00 2001
From: GyDi <segydi@foxmail.com>
Date: Sat, 12 Mar 2022 21:03:17 +0800
Subject: [PATCH] fix: config file case close #18

---
 src-tauri/src/core/clash.rs    | 28 +++++++++++++++++-----------
 src-tauri/src/core/profiles.rs | 18 +++++++++++-------
 2 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/src-tauri/src/core/clash.rs b/src-tauri/src/core/clash.rs
index 499191e..0091c85 100644
--- a/src-tauri/src/core/clash.rs
+++ b/src-tauri/src/core/clash.rs
@@ -328,7 +328,7 @@ impl Clash {
         if let Some(data) = result.data {
           // all of these can not be revised by script
           // http/https/socks port should be under control
-          let not_allow: Vec<Value> = vec![
+          let not_allow = vec![
             "port",
             "socks-port",
             "mixed-port",
@@ -337,23 +337,29 @@ impl Clash {
             "external-controller",
             "secret",
             "log-level",
-          ]
-          .iter()
-          .map(|&i| Value::from(i))
-          .collect();
+          ];
 
           for (key, value) in data.into_iter() {
-            if not_allow.iter().find(|&i| i == &key).is_none() {
-              config.insert(key, value);
-            }
+            key.as_str().map(|key_str| {
+              // change to lowercase
+              let mut key_str = String::from(key_str);
+              key_str.make_ascii_lowercase();
+
+              // filter
+              if !not_allow.contains(&&*key_str) {
+                config.insert(Value::String(key_str), value);
+              }
+            });
           }
 
+          log::info!("profile enhanced status {}", result.status);
+
           Self::_activate(info, config).unwrap();
         }
 
-        log::info!("profile enhanced status {}", result.status);
-
-        result.error.map(|error| log::error!("{error}"));
+        if let Some(error) = result.error {
+          log::error!("{error}");
+        }
       }
     });
 
diff --git a/src-tauri/src/core/profiles.rs b/src-tauri/src/core/profiles.rs
index afc1bf5..a7211c1 100644
--- a/src-tauri/src/core/profiles.rs
+++ b/src-tauri/src/core/profiles.rs
@@ -566,13 +566,17 @@ impl Profiles {
           "rules",
         ];
 
-        valid_keys.iter().for_each(|key| {
-          let key = Value::String(key.to_string());
-          if def_config.contains_key(&key) {
-            let value = def_config[&key].clone();
-            new_config.insert(key, value);
-          }
-        });
+        for (key, value) in def_config.into_iter() {
+          key.as_str().map(|key_str| {
+            // change to lowercase
+            let mut key_str = String::from(key_str);
+            key_str.make_ascii_lowercase();
+
+            if valid_keys.contains(&&*key_str) {
+              new_config.insert(Value::String(key_str), value);
+            }
+          });
+        }
 
         return Ok(new_config);
       }
-- 
GitLab