Skip to content
Snippets Groups Projects
Commit 2a986a3d authored by GyDi's avatar GyDi
Browse files

feat: read clash config

parent b5d0c2b7
No related branches found
No related tags found
No related merge requests found
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>,
}
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use serde_yaml::{Mapping, Value};
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
use super::profiles::ProfilesConfig; use super::{profiles::ProfilesConfig, ClashController};
use crate::init::app_home_dir; use crate::init::app_home_dir;
/// read data from yaml as struct T /// read data from yaml as struct T
...@@ -34,23 +35,37 @@ pub fn save_yaml<T: Serialize>( ...@@ -34,23 +35,37 @@ pub fn save_yaml<T: Serialize>(
} }
} }
// /// Get Clash Core Config /// Get Clash Core Config
// pub fn read_clash() -> Mapping { pub fn read_clash() -> Mapping {
// read_yaml::<Mapping>(app_home_dir().join("config.yaml")) read_yaml::<Mapping>(app_home_dir().join("config.yaml"))
// } }
/// Get infomation of the clash's `external-controller` and `secret`
pub fn read_clash_controller() -> ClashController {
let config = read_clash();
let key_server = Value::String("external-controller".to_string());
let key_secret = Value::String("secret".to_string());
// /// Get Verge App Config let server = match config.get(&key_server) {
// pub fn read_verge() -> ProfilesConfig { Some(value) => match value {
// read_from_yaml::<ProfilesConfig>(app_home_dir().join("verge.yaml")) 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,
};
// /// Save Verge App Config ClashController { server, secret }
// 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();
// }
/// Get Profiles Config /// Get Profiles Config
pub fn read_profiles() -> ProfilesConfig { pub fn read_profiles() -> ProfilesConfig {
...@@ -66,3 +81,8 @@ pub fn save_profiles(profiles: &ProfilesConfig) { ...@@ -66,3 +81,8 @@ pub fn save_profiles(profiles: &ProfilesConfig) {
) )
.unwrap(); .unwrap();
} }
#[test]
fn test_print_config() {
println!("{:?}", read_clash_controller());
}
use serde::{Deserialize, Serialize};
/// ### `verge.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct VergeConfig {
pub something: Option<String>,
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment