mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2024-11-16 03:32:36 +08:00
feat: change the naming strategy
This commit is contained in:
parent
03b3a0b8b3
commit
ef3b10fa8f
3
src-tauri/resources/profiles_tmp.yaml
Normal file
3
src-tauri/resources/profiles_tmp.yaml
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Profiles Config for Clash Verge
|
||||
|
||||
current: 0
|
|
@ -1 +1,5 @@
|
|||
pub mod verge;
|
||||
mod operate;
|
||||
mod profiles;
|
||||
|
||||
pub use self::operate::*;
|
||||
pub use self::profiles::*;
|
||||
|
|
68
src-tauri/src/config/operate.rs
Normal file
68
src-tauri/src/config/operate.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
use super::profiles::ProfilesConfig;
|
||||
use crate::init::app_home_dir;
|
||||
|
||||
/// 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 Verge App Config
|
||||
// pub fn read_verge() -> ProfilesConfig {
|
||||
// read_from_yaml::<ProfilesConfig>(app_home_dir().join("verge.yaml"))
|
||||
// }
|
||||
|
||||
// /// 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();
|
||||
// }
|
||||
|
||||
/// 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) {
|
||||
save_yaml(
|
||||
app_home_dir().join("profiles.yaml"),
|
||||
profiles,
|
||||
Some("# Profiles Config for Clash Verge\n\n"),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
|
@ -2,16 +2,16 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
/// Define the verge.yaml's schema
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct VergeConfig {
|
||||
pub struct ProfilesConfig {
|
||||
/// current profile's name
|
||||
pub current: Option<u32>,
|
||||
|
||||
/// profile list
|
||||
pub profiles: Option<Vec<ProfileData>>,
|
||||
pub items: Option<Vec<ProfileItem>>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct ProfileData {
|
||||
pub struct ProfileItem {
|
||||
/// profile name
|
||||
pub name: Option<String>,
|
||||
/// profile file
|
||||
|
@ -23,7 +23,7 @@ pub struct ProfileData {
|
|||
/// selected infomation
|
||||
pub selected: Option<Vec<ProfileSelected>>,
|
||||
/// user info
|
||||
pub user_info: Option<ProfileUserInfo>,
|
||||
pub extra: Option<ProfileExtra>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||
|
@ -33,7 +33,7 @@ pub struct ProfileSelected {
|
|||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy, Deserialize, Serialize)]
|
||||
pub struct ProfileUserInfo {
|
||||
pub struct ProfileExtra {
|
||||
pub upload: u64,
|
||||
pub download: u64,
|
||||
pub total: u64,
|
|
@ -5,7 +5,6 @@ use log4rs::append::console::ConsoleAppender;
|
|||
use log4rs::append::file::FileAppender;
|
||||
use log4rs::config::{Appender, Config, Root};
|
||||
use log4rs::encode::pattern::PatternEncoder;
|
||||
use serde_yaml::Mapping;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
@ -13,8 +12,6 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
|||
use tauri::api::path::{home_dir, resource_dir};
|
||||
use tauri::PackageInfo;
|
||||
|
||||
use crate::config::verge::VergeConfig;
|
||||
|
||||
/// get the verge app home dir
|
||||
pub fn app_home_dir() -> PathBuf {
|
||||
home_dir()
|
||||
|
@ -53,43 +50,45 @@ fn init_log(log_dir: &PathBuf) {
|
|||
log4rs::init_config(config).unwrap();
|
||||
}
|
||||
|
||||
/// Initialize the clash config file
|
||||
fn init_clash_config(app_dir: &PathBuf, res_dir: &PathBuf) {
|
||||
let yaml_path = app_dir.join("config.yaml");
|
||||
let yaml_tmpl = res_dir.join("config_tmp.yaml");
|
||||
|
||||
if !yaml_path.exists() {
|
||||
if yaml_tmpl.exists() {
|
||||
fs::copy(yaml_tmpl, yaml_path).unwrap();
|
||||
} else {
|
||||
let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes();
|
||||
fs::File::create(yaml_path).unwrap().write(content).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize all the files from resources
|
||||
fn init_config_file(app_dir: &PathBuf, res_dir: &PathBuf) {
|
||||
// target path
|
||||
let clash_path = app_dir.join("config.yaml");
|
||||
let verge_path = app_dir.join("verge.yaml");
|
||||
let profile_path = app_dir.join("profiles.yaml");
|
||||
let mmdb_path = app_dir.join("Country.mmdb");
|
||||
|
||||
// template path
|
||||
let clash_tmpl = res_dir.join("config_tmp.yaml");
|
||||
let verge_tmpl = res_dir.join("verge_tmp.yaml");
|
||||
let profiles_tmpl = res_dir.join("profiles_tmp.yaml");
|
||||
let mmdb_tmpl = res_dir.join("Country.mmdb");
|
||||
|
||||
if !clash_path.exists() {
|
||||
if clash_tmpl.exists() {
|
||||
fs::copy(clash_tmpl, clash_path).unwrap();
|
||||
} else {
|
||||
// make sure that the config.yaml not null
|
||||
let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes();
|
||||
fs::File::create(clash_path)
|
||||
.unwrap()
|
||||
.write(content)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
// only copy it
|
||||
if !verge_path.exists() && verge_tmpl.exists() {
|
||||
fs::copy(verge_tmpl, verge_path).unwrap();
|
||||
}
|
||||
if !profile_path.exists() && profiles_tmpl.exists() {
|
||||
fs::copy(profiles_tmpl, profile_path).unwrap();
|
||||
}
|
||||
if !mmdb_path.exists() && mmdb_tmpl.exists() {
|
||||
fs::copy(mmdb_tmpl, mmdb_path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialize the verge app config file
|
||||
fn init_verge_config(app_dir: &PathBuf, res_dir: &PathBuf) {
|
||||
let yaml_path = app_dir.join("verge.yaml");
|
||||
let yaml_tmpl = res_dir.join("verge_tmp.yaml");
|
||||
|
||||
if !yaml_path.exists() {
|
||||
if yaml_tmpl.exists() {
|
||||
fs::copy(yaml_tmpl, yaml_path).unwrap();
|
||||
} else {
|
||||
let content = "".as_bytes();
|
||||
fs::File::create(yaml_path).unwrap().write(content).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// initialize app
|
||||
pub fn init_app(package_info: &PackageInfo) {
|
||||
// create app dir
|
||||
|
@ -110,28 +109,5 @@ pub fn init_app(package_info: &PackageInfo) {
|
|||
}
|
||||
|
||||
init_log(&log_dir);
|
||||
init_clash_config(&app_dir, &res_dir);
|
||||
init_verge_config(&app_dir, &res_dir);
|
||||
}
|
||||
|
||||
/// Get the user config of clash core
|
||||
pub fn read_clash_config() -> Mapping {
|
||||
let yaml_path = app_home_dir().join("config.yaml");
|
||||
let yaml_str = fs::read_to_string(yaml_path).unwrap();
|
||||
serde_yaml::from_str::<Mapping>(&yaml_str).unwrap()
|
||||
}
|
||||
|
||||
/// Get the user config of verge
|
||||
pub fn read_verge_config() -> VergeConfig {
|
||||
let yaml_path = app_home_dir().join("verge.yaml");
|
||||
let yaml_str = fs::read_to_string(yaml_path).unwrap();
|
||||
serde_yaml::from_str::<VergeConfig>(&yaml_str).unwrap()
|
||||
}
|
||||
|
||||
/// Save the user config of verge
|
||||
pub fn save_verge_config(verge_config: &VergeConfig) {
|
||||
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();
|
||||
init_config_file(&app_dir, &res_dir);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
extern crate reqwest;
|
||||
|
||||
use crate::config::verge::{ProfileData, ProfileUserInfo};
|
||||
use crate::init::{app_home_dir, read_verge_config, save_verge_config};
|
||||
use crate::config::{read_profiles, save_profiles, ProfileExtra, ProfileItem};
|
||||
use crate::init::app_home_dir;
|
||||
use std::default::Default;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
|
@ -20,24 +20,24 @@ pub async fn import_profile(profile_url: &str) -> Result<(), reqwest::Error> {
|
|||
.unwrap();
|
||||
let value: Vec<&str> = value.clone().split(';').collect();
|
||||
|
||||
let mut user_info = ProfileUserInfo::default();
|
||||
|
||||
// parse the Subscription Userinfo
|
||||
let mut extra = ProfileExtra::default();
|
||||
for each in value.iter() {
|
||||
let each = each.clone().trim();
|
||||
if let Some(val) = each.strip_prefix("upload=") {
|
||||
user_info.upload = val.parse().unwrap_or(0u64);
|
||||
extra.upload = val.parse().unwrap_or(0u64);
|
||||
continue;
|
||||
}
|
||||
if let Some(val) = each.strip_prefix("download=") {
|
||||
user_info.download = val.parse().unwrap_or(0u64);
|
||||
extra.download = val.parse().unwrap_or(0u64);
|
||||
continue;
|
||||
}
|
||||
if let Some(val) = each.strip_prefix("total=") {
|
||||
user_info.total = val.parse().unwrap_or(0u64);
|
||||
extra.total = val.parse().unwrap_or(0u64);
|
||||
continue;
|
||||
}
|
||||
if let Some(val) = each.strip_prefix("expire=") {
|
||||
user_info.expire = val.parse().unwrap_or(0u64);
|
||||
extra.expire = val.parse().unwrap_or(0u64);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
@ -56,35 +56,35 @@ pub async fn import_profile(profile_url: &str) -> Result<(), reqwest::Error> {
|
|||
.write(file_data.as_bytes())
|
||||
.unwrap();
|
||||
|
||||
let mut verge = read_verge_config();
|
||||
|
||||
let mut profiles = if verge.profiles.is_some() {
|
||||
verge.profiles.unwrap()
|
||||
// update profiles.yaml
|
||||
let mut profiles = read_profiles();
|
||||
let mut items = if profiles.items.is_some() {
|
||||
profiles.items.unwrap()
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
|
||||
let profile = ProfileData {
|
||||
let profile = ProfileItem {
|
||||
name: Some(file_name.clone()),
|
||||
file: Some(file_name.clone()),
|
||||
mode: Some(String::from("rule")),
|
||||
url: Some(String::from(profile_url)),
|
||||
selected: Some(vec![]),
|
||||
user_info: Some(user_info),
|
||||
extra: Some(extra),
|
||||
};
|
||||
|
||||
let target_index = profiles
|
||||
let target_index = items
|
||||
.iter()
|
||||
.position(|x| x.name.is_some() && x.name.as_ref().unwrap().as_str() == file_name.as_str());
|
||||
|
||||
if target_index.is_none() {
|
||||
profiles.push(profile)
|
||||
items.push(profile)
|
||||
} else {
|
||||
profiles[target_index.unwrap()] = profile;
|
||||
items[target_index.unwrap()] = profile;
|
||||
}
|
||||
|
||||
verge.profiles = Some(profiles);
|
||||
save_verge_config(&verge);
|
||||
profiles.items = Some(items);
|
||||
save_profiles(&profiles);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user