mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
feat: adjust code
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
extern crate log;
|
||||
|
||||
use crate::{
|
||||
config::{read_clash_controller, read_profiles},
|
||||
events::emit::{clash_start, ClashInfoPayload},
|
||||
utils::app_home_dir,
|
||||
utils::{
|
||||
app_home_dir,
|
||||
config::{read_clash_controller, read_profiles},
|
||||
},
|
||||
};
|
||||
use reqwest::header::HeaderMap;
|
||||
use std::{collections::HashMap, env::temp_dir, fs};
|
||||
@@ -67,7 +69,7 @@ pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload {
|
||||
pub async fn put_clash_profile(payload: &ClashInfoPayload) -> Result<(), String> {
|
||||
let profile = {
|
||||
let profiles = read_profiles();
|
||||
let current = profiles.current.unwrap_or(0u32) as usize;
|
||||
let current = profiles.current.unwrap_or(0) as usize;
|
||||
match profiles.items {
|
||||
Some(items) => {
|
||||
if items.len() == 0 {
|
||||
|
||||
109
src-tauri/src/utils/config.rs
Normal file
109
src-tauri/src/utils/config.rs
Normal file
@@ -0,0 +1,109 @@
|
||||
use crate::{
|
||||
config::{ClashController, ProfilesConfig},
|
||||
utils::app_home_dir,
|
||||
};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use serde_yaml::{Mapping, Value};
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
/// read data from yaml as struct T
|
||||
pub fn read_yaml<T: DeserializeOwned>(path: PathBuf) -> T {
|
||||
let yaml_str = fs::read_to_string(path).unwrap();
|
||||
serde_yaml::from_str::<T>(&yaml_str).unwrap()
|
||||
}
|
||||
|
||||
/// - save the data to the file
|
||||
/// - can set `prefix` string to add some comments
|
||||
pub fn save_yaml<T: Serialize>(
|
||||
path: PathBuf,
|
||||
data: &T,
|
||||
prefix: Option<&str>,
|
||||
) -> Result<(), String> {
|
||||
if let Ok(data_str) = serde_yaml::to_string(data) {
|
||||
let yaml_str = if prefix.is_some() {
|
||||
prefix.unwrap().to_string() + &data_str
|
||||
} else {
|
||||
data_str
|
||||
};
|
||||
|
||||
if fs::write(path.clone(), yaml_str.as_bytes()).is_err() {
|
||||
Err(format!("can not save file `{:?}`", path))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Err(String::from("can not convert the data to yaml"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Get Clash Core Config
|
||||
pub fn read_clash() -> Mapping {
|
||||
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_port_1 = Value::String("port".to_string());
|
||||
let key_port_2 = Value::String("mixed-port".to_string());
|
||||
let key_server = Value::String("external-controller".to_string());
|
||||
let key_secret = Value::String("secret".to_string());
|
||||
|
||||
let port = match config.get(&key_port_1) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
let port = match port {
|
||||
Some(_) => port,
|
||||
None => match config.get(&key_port_2) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
},
|
||||
};
|
||||
|
||||
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 {
|
||||
port,
|
||||
server,
|
||||
secret,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get Profiles Config
|
||||
pub fn read_profiles() -> ProfilesConfig {
|
||||
read_yaml::<ProfilesConfig>(app_home_dir().join("profiles.yaml"))
|
||||
}
|
||||
|
||||
/// Save Verge App Config
|
||||
pub fn save_profiles(profiles: &ProfilesConfig) -> Result<(), String> {
|
||||
save_yaml(
|
||||
app_home_dir().join("profiles.yaml"),
|
||||
profiles,
|
||||
Some("# Profiles Config for Clash Verge\n\n"),
|
||||
)
|
||||
}
|
||||
@@ -1,15 +1,21 @@
|
||||
use crate::config::{ProfileExtra, ProfileResponse};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{
|
||||
str::FromStr,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
/// parse the string
|
||||
fn parse_string<'a>(target: &'a str, key: &'a str) -> Option<&'a str> {
|
||||
fn parse_string<T: FromStr>(target: &str, key: &str) -> Option<T> {
|
||||
match target.find(key) {
|
||||
Some(idx) => {
|
||||
let idx = idx + key.len();
|
||||
let value = &target[idx..];
|
||||
match value.split(';').nth(0) {
|
||||
Some(value) => Some(value.trim()),
|
||||
None => Some(value.trim()),
|
||||
match match value.split(';').nth(0) {
|
||||
Some(value) => value.trim().parse(),
|
||||
None => value.trim().parse(),
|
||||
} {
|
||||
Ok(r) => Some(r),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
@@ -33,22 +39,10 @@ pub async fn fetch_profile(url: &str) -> Option<ProfileResponse> {
|
||||
.unwrap();
|
||||
|
||||
ProfileExtra {
|
||||
upload: parse_string(sub_info, "upload=")
|
||||
.unwrap_or("0")
|
||||
.parse()
|
||||
.unwrap_or(0u64),
|
||||
download: parse_string(sub_info, "download=")
|
||||
.unwrap_or("0")
|
||||
.parse()
|
||||
.unwrap_or(0u64),
|
||||
total: parse_string(sub_info, "total=")
|
||||
.unwrap_or("0")
|
||||
.parse()
|
||||
.unwrap_or(0u64),
|
||||
expire: parse_string(sub_info, "expire=")
|
||||
.unwrap_or("0")
|
||||
.parse()
|
||||
.unwrap_or(0u64),
|
||||
upload: parse_string(sub_info, "upload=").unwrap_or(0),
|
||||
download: parse_string(sub_info, "download=").unwrap_or(0),
|
||||
total: parse_string(sub_info, "total=").unwrap_or(0),
|
||||
expire: parse_string(sub_info, "expire=").unwrap_or(0),
|
||||
}
|
||||
};
|
||||
|
||||
@@ -60,10 +54,10 @@ pub async fn fetch_profile(url: &str) -> Option<ProfileResponse> {
|
||||
.as_secs();
|
||||
let file = format!("{}.yaml", now);
|
||||
let name = header.get("Content-Disposition").unwrap().to_str().unwrap();
|
||||
let name = parse_string(name, "filename=");
|
||||
let name = parse_string::<String>(name, "filename=");
|
||||
|
||||
match name {
|
||||
Some(f) => (f.to_string(), file),
|
||||
Some(f) => (f, file),
|
||||
None => (file.clone(), file),
|
||||
}
|
||||
};
|
||||
@@ -87,14 +81,17 @@ fn test_parse_value() {
|
||||
let test_1 = "upload=111; download=2222; total=3333; expire=444";
|
||||
let test_2 = "attachment; filename=Clash.yaml";
|
||||
|
||||
assert_eq!(parse_string(test_1, "upload="), Some("111"));
|
||||
assert_eq!(parse_string(test_1, "download="), Some("2222"));
|
||||
assert_eq!(parse_string(test_1, "total="), Some("3333"));
|
||||
assert_eq!(parse_string(test_1, "expire="), Some("444"));
|
||||
assert_eq!(parse_string(test_2, "filename="), Some("Clash.yaml"));
|
||||
assert_eq!(parse_string::<usize>(test_1, "upload=").unwrap(), 111);
|
||||
assert_eq!(parse_string::<usize>(test_1, "download=").unwrap(), 2222);
|
||||
assert_eq!(parse_string::<usize>(test_1, "total=").unwrap(), 3333);
|
||||
assert_eq!(parse_string::<usize>(test_1, "expire=").unwrap(), 444);
|
||||
assert_eq!(
|
||||
parse_string::<String>(test_2, "filename=").unwrap(),
|
||||
format!("Clash.yaml")
|
||||
);
|
||||
|
||||
assert_eq!(parse_string(test_1, "aaa="), None);
|
||||
assert_eq!(parse_string(test_1, "upload1="), None);
|
||||
assert_eq!(parse_string(test_1, "expire1="), None);
|
||||
assert_eq!(parse_string(test_2, "attachment="), None);
|
||||
assert_eq!(parse_string::<usize>(test_1, "aaa="), None);
|
||||
assert_eq!(parse_string::<usize>(test_1, "upload1="), None);
|
||||
assert_eq!(parse_string::<usize>(test_1, "expire1="), None);
|
||||
assert_eq!(parse_string::<usize>(test_2, "attachment="), None);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ mod dirs;
|
||||
pub use self::dirs::*;
|
||||
|
||||
pub mod clash;
|
||||
pub mod config;
|
||||
pub mod fetch;
|
||||
pub mod init;
|
||||
pub mod sysopt;
|
||||
|
||||
Reference in New Issue
Block a user