|
@@ -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
|
|
|
|
-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");
|
|
|
|
|
|
+/// 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 !yaml_path.exists() {
|
|
|
|
- if yaml_tmpl.exists() {
|
|
|
|
- fs::copy(yaml_tmpl, yaml_path).unwrap();
|
|
|
|
|
|
+ if !clash_path.exists() {
|
|
|
|
+ if clash_tmpl.exists() {
|
|
|
|
+ fs::copy(clash_tmpl, clash_path).unwrap();
|
|
} else {
|
|
} else {
|
|
|
|
+ // make sure that the config.yaml not null
|
|
let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes();
|
|
let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes();
|
|
- fs::File::create(yaml_path).unwrap().write(content).unwrap();
|
|
|
|
|
|
+ fs::File::create(clash_path)
|
|
|
|
+ .unwrap()
|
|
|
|
+ .write(content)
|
|
|
|
+ .unwrap();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- let mmdb_path = app_dir.join("Country.mmdb");
|
|
|
|
- let mmdb_tmpl = res_dir.join("Country.mmdb");
|
|
|
|
-
|
|
|
|
|
|
+ // 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_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);
|
|
}
|
|
}
|