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
|
/// Define the verge.yaml's schema
|
||||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct VergeConfig {
|
pub struct ProfilesConfig {
|
||||||
/// current profile's name
|
/// current profile's name
|
||||||
pub current: Option<u32>,
|
pub current: Option<u32>,
|
||||||
|
|
||||||
/// profile list
|
/// profile list
|
||||||
pub profiles: Option<Vec<ProfileData>>,
|
pub items: Option<Vec<ProfileItem>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct ProfileData {
|
pub struct ProfileItem {
|
||||||
/// profile name
|
/// profile name
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
/// profile file
|
/// profile file
|
||||||
@ -23,7 +23,7 @@ pub struct ProfileData {
|
|||||||
/// selected infomation
|
/// selected infomation
|
||||||
pub selected: Option<Vec<ProfileSelected>>,
|
pub selected: Option<Vec<ProfileSelected>>,
|
||||||
/// user info
|
/// user info
|
||||||
pub user_info: Option<ProfileUserInfo>,
|
pub extra: Option<ProfileExtra>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||||
@ -33,7 +33,7 @@ pub struct ProfileSelected {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Copy, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Copy, Deserialize, Serialize)]
|
||||||
pub struct ProfileUserInfo {
|
pub struct ProfileExtra {
|
||||||
pub upload: u64,
|
pub upload: u64,
|
||||||
pub download: u64,
|
pub download: u64,
|
||||||
pub total: u64,
|
pub total: u64,
|
@ -5,7 +5,6 @@ use log4rs::append::console::ConsoleAppender;
|
|||||||
use log4rs::append::file::FileAppender;
|
use log4rs::append::file::FileAppender;
|
||||||
use log4rs::config::{Appender, Config, Root};
|
use log4rs::config::{Appender, Config, Root};
|
||||||
use log4rs::encode::pattern::PatternEncoder;
|
use log4rs::encode::pattern::PatternEncoder;
|
||||||
use serde_yaml::Mapping;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::{Path, PathBuf};
|
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::api::path::{home_dir, resource_dir};
|
||||||
use tauri::PackageInfo;
|
use tauri::PackageInfo;
|
||||||
|
|
||||||
use crate::config::verge::VergeConfig;
|
|
||||||
|
|
||||||
/// get the verge app home dir
|
/// get the verge app home dir
|
||||||
pub fn app_home_dir() -> PathBuf {
|
pub fn app_home_dir() -> PathBuf {
|
||||||
home_dir()
|
home_dir()
|
||||||
@ -53,43 +50,45 @@ fn init_log(log_dir: &PathBuf) {
|
|||||||
log4rs::init_config(config).unwrap();
|
log4rs::init_config(config).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize the clash config file
|
/// Initialize all the files from resources
|
||||||
fn init_clash_config(app_dir: &PathBuf, res_dir: &PathBuf) {
|
fn init_config_file(app_dir: &PathBuf, res_dir: &PathBuf) {
|
||||||
let yaml_path = app_dir.join("config.yaml");
|
// target path
|
||||||
let yaml_tmpl = res_dir.join("config_tmp.yaml");
|
let clash_path = app_dir.join("config.yaml");
|
||||||
|
let verge_path = app_dir.join("verge.yaml");
|
||||||
if !yaml_path.exists() {
|
let profile_path = app_dir.join("profiles.yaml");
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mmdb_path = app_dir.join("Country.mmdb");
|
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");
|
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() {
|
if !mmdb_path.exists() && mmdb_tmpl.exists() {
|
||||||
fs::copy(mmdb_tmpl, mmdb_path).unwrap();
|
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
|
/// initialize app
|
||||||
pub fn init_app(package_info: &PackageInfo) {
|
pub fn init_app(package_info: &PackageInfo) {
|
||||||
// create app dir
|
// create app dir
|
||||||
@ -110,28 +109,5 @@ pub fn init_app(package_info: &PackageInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init_log(&log_dir);
|
init_log(&log_dir);
|
||||||
init_clash_config(&app_dir, &res_dir);
|
init_config_file(&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();
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
extern crate reqwest;
|
extern crate reqwest;
|
||||||
|
|
||||||
use crate::config::verge::{ProfileData, ProfileUserInfo};
|
use crate::config::{read_profiles, save_profiles, ProfileExtra, ProfileItem};
|
||||||
use crate::init::{app_home_dir, read_verge_config, save_verge_config};
|
use crate::init::app_home_dir;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@ -20,24 +20,24 @@ pub async fn import_profile(profile_url: &str) -> Result<(), reqwest::Error> {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
let value: Vec<&str> = value.clone().split(';').collect();
|
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() {
|
for each in value.iter() {
|
||||||
let each = each.clone().trim();
|
let each = each.clone().trim();
|
||||||
if let Some(val) = each.strip_prefix("upload=") {
|
if let Some(val) = each.strip_prefix("upload=") {
|
||||||
user_info.upload = val.parse().unwrap_or(0u64);
|
extra.upload = val.parse().unwrap_or(0u64);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(val) = each.strip_prefix("download=") {
|
if let Some(val) = each.strip_prefix("download=") {
|
||||||
user_info.download = val.parse().unwrap_or(0u64);
|
extra.download = val.parse().unwrap_or(0u64);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(val) = each.strip_prefix("total=") {
|
if let Some(val) = each.strip_prefix("total=") {
|
||||||
user_info.total = val.parse().unwrap_or(0u64);
|
extra.total = val.parse().unwrap_or(0u64);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(val) = each.strip_prefix("expire=") {
|
if let Some(val) = each.strip_prefix("expire=") {
|
||||||
user_info.expire = val.parse().unwrap_or(0u64);
|
extra.expire = val.parse().unwrap_or(0u64);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,35 +56,35 @@ pub async fn import_profile(profile_url: &str) -> Result<(), reqwest::Error> {
|
|||||||
.write(file_data.as_bytes())
|
.write(file_data.as_bytes())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut verge = read_verge_config();
|
// update profiles.yaml
|
||||||
|
let mut profiles = read_profiles();
|
||||||
let mut profiles = if verge.profiles.is_some() {
|
let mut items = if profiles.items.is_some() {
|
||||||
verge.profiles.unwrap()
|
profiles.items.unwrap()
|
||||||
} else {
|
} else {
|
||||||
vec![]
|
vec![]
|
||||||
};
|
};
|
||||||
|
|
||||||
let profile = ProfileData {
|
let profile = ProfileItem {
|
||||||
name: Some(file_name.clone()),
|
name: Some(file_name.clone()),
|
||||||
file: Some(file_name.clone()),
|
file: Some(file_name.clone()),
|
||||||
mode: Some(String::from("rule")),
|
mode: Some(String::from("rule")),
|
||||||
url: Some(String::from(profile_url)),
|
url: Some(String::from(profile_url)),
|
||||||
selected: Some(vec![]),
|
selected: Some(vec![]),
|
||||||
user_info: Some(user_info),
|
extra: Some(extra),
|
||||||
};
|
};
|
||||||
|
|
||||||
let target_index = profiles
|
let target_index = items
|
||||||
.iter()
|
.iter()
|
||||||
.position(|x| x.name.is_some() && x.name.as_ref().unwrap().as_str() == file_name.as_str());
|
.position(|x| x.name.is_some() && x.name.as_ref().unwrap().as_str() == file_name.as_str());
|
||||||
|
|
||||||
if target_index.is_none() {
|
if target_index.is_none() {
|
||||||
profiles.push(profile)
|
items.push(profile)
|
||||||
} else {
|
} else {
|
||||||
profiles[target_index.unwrap()] = profile;
|
items[target_index.unwrap()] = profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
verge.profiles = Some(profiles);
|
profiles.items = Some(items);
|
||||||
save_verge_config(&verge);
|
save_profiles(&profiles);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user