From 2a986a3d2fa19840e47eca78296cd3fde57663d7 Mon Sep 17 00:00:00 2001
From: GyDi <segydi@foxmail.com>
Date: Tue, 14 Dec 2021 15:55:29 +0800
Subject: [PATCH] feat: read clash config

---
 src-tauri/src/config/clash.rs   | 27 +++++++++++++++++
 src-tauri/src/config/operate.rs | 52 +++++++++++++++++++++++----------
 src-tauri/src/config/verge.rs   |  7 +++++
 3 files changed, 70 insertions(+), 16 deletions(-)
 create mode 100644 src-tauri/src/config/clash.rs
 create mode 100644 src-tauri/src/config/verge.rs

diff --git a/src-tauri/src/config/clash.rs b/src-tauri/src/config/clash.rs
new file mode 100644
index 0000000..4c8760d
--- /dev/null
+++ b/src-tauri/src/config/clash.rs
@@ -0,0 +1,27 @@
+use serde::{Deserialize, Serialize};
+
+/// ### `config.yaml` schema
+/// here should contain all configuration options.
+/// See: https://github.com/Dreamacro/clash/wiki/configuration for details
+#[derive(Default, Debug, Clone, Deserialize, Serialize)]
+pub struct ClashConfig {
+  pub port: Option<u32>,
+
+  /// alias to `mixed-port`
+  pub mixed_port: Option<u32>,
+
+  /// alias to `allow-lan`
+  pub allow_lan: Option<bool>,
+
+  /// alias to `external-controller`
+  pub external_ctrl: Option<String>,
+
+  pub secret: Option<String>,
+}
+
+#[derive(Default, Debug, Clone, Deserialize, Serialize)]
+pub struct ClashController {
+  /// same as `external-controller`
+  pub server: Option<String>,
+  pub secret: Option<String>,
+}
diff --git a/src-tauri/src/config/operate.rs b/src-tauri/src/config/operate.rs
index 9c83360..1048eec 100644
--- a/src-tauri/src/config/operate.rs
+++ b/src-tauri/src/config/operate.rs
@@ -1,7 +1,8 @@
 use serde::{de::DeserializeOwned, Serialize};
+use serde_yaml::{Mapping, Value};
 use std::{fs, path::PathBuf};
 
-use super::profiles::ProfilesConfig;
+use super::{profiles::ProfilesConfig, ClashController};
 use crate::init::app_home_dir;
 
 /// read data from yaml as struct T
@@ -34,23 +35,37 @@ pub fn save_yaml<T: Serialize>(
   }
 }
 
-// /// Get Clash Core Config
-// pub fn read_clash() -> Mapping {
-//   read_yaml::<Mapping>(app_home_dir().join("config.yaml"))
-// }
+/// Get Clash Core Config
+pub fn read_clash() -> Mapping {
+  read_yaml::<Mapping>(app_home_dir().join("config.yaml"))
+}
 
-// /// Get Verge App Config
-// pub fn read_verge() -> ProfilesConfig {
-//   read_from_yaml::<ProfilesConfig>(app_home_dir().join("verge.yaml"))
-// }
+/// Get infomation of the clash's `external-controller` and `secret`
+pub fn read_clash_controller() -> ClashController {
+  let config = read_clash();
 
-// /// Save Verge App Config
-// pub fn save_verge(verge_config: &ProfilesConfig) {
-//   let yaml_path = app_home_dir().join("verge.yaml");
-//   let yaml_str = serde_yaml::to_string(&verge_config).unwrap();
-//   let yaml_str = String::from("# Config File for Clash Verge\n\n") + &yaml_str;
-//   fs::write(yaml_path, yaml_str.as_bytes()).unwrap();
-// }
+  let key_server = Value::String("external-controller".to_string());
+  let key_secret = Value::String("secret".to_string());
+
+  let server = match config.get(&key_server) {
+    Some(value) => match value {
+      Value::String(val_str) => Some(val_str.clone()),
+      _ => None,
+    },
+    _ => None,
+  };
+  let secret = match config.get(&key_secret) {
+    Some(value) => match value {
+      Value::String(val_str) => Some(val_str.clone()),
+      Value::Bool(val_bool) => Some(val_bool.to_string()),
+      Value::Number(val_num) => Some(val_num.to_string()),
+      _ => None,
+    },
+    _ => None,
+  };
+
+  ClashController { server, secret }
+}
 
 /// Get Profiles Config
 pub fn read_profiles() -> ProfilesConfig {
@@ -66,3 +81,8 @@ pub fn save_profiles(profiles: &ProfilesConfig) {
   )
   .unwrap();
 }
+
+#[test]
+fn test_print_config() {
+  println!("{:?}", read_clash_controller());
+}
diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/verge.rs
new file mode 100644
index 0000000..830924b
--- /dev/null
+++ b/src-tauri/src/config/verge.rs
@@ -0,0 +1,7 @@
+use serde::{Deserialize, Serialize};
+
+/// ### `verge.yaml` schema
+#[derive(Default, Debug, Clone, Deserialize, Serialize)]
+pub struct VergeConfig {
+  pub something: Option<String>,
+}