feat: read clash config

This commit is contained in:
GyDi 2021-12-14 15:55:29 +08:00
parent b5d0c2b78b
commit 2a986a3d2f
3 changed files with 70 additions and 16 deletions

View File

@ -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>,
}

View File

@ -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());
}

View File

@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
/// ### `verge.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct VergeConfig {
pub something: Option<String>,
}