|
@@ -10,18 +10,8 @@ use std::fs;
|
|
use std::io::Write;
|
|
use std::io::Write;
|
|
use std::path::{Path, PathBuf};
|
|
use std::path::{Path, PathBuf};
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
-use tauri::api::path::home_dir;
|
|
|
|
-
|
|
|
|
-const CLASH_CONFIG: &str = r#"
|
|
|
|
-mixed-port: 7890
|
|
|
|
-allow-lan: false
|
|
|
|
-external-controller: 127.0.0.1:9090
|
|
|
|
-secret: ''
|
|
|
|
-"#;
|
|
|
|
-
|
|
|
|
-const VERGE_CONFIG: &str = r#"
|
|
|
|
-nothing: ohh!
|
|
|
|
-"#;
|
|
|
|
|
|
+use tauri::api::path::{home_dir, resource_dir};
|
|
|
|
+use tauri::PackageInfo;
|
|
|
|
|
|
/// get the verge app home dir
|
|
/// get the verge app home dir
|
|
pub fn app_home_dir() -> PathBuf {
|
|
pub fn app_home_dir() -> PathBuf {
|
|
@@ -31,27 +21,8 @@ pub fn app_home_dir() -> PathBuf {
|
|
.join(Path::new("clash-verge"))
|
|
.join(Path::new("clash-verge"))
|
|
}
|
|
}
|
|
|
|
|
|
-/// initialize the app home dir
|
|
|
|
-fn init_app_dir() -> PathBuf {
|
|
|
|
- let app_dir = app_home_dir();
|
|
|
|
- if !app_dir.exists() {
|
|
|
|
- fs::create_dir(&app_dir).unwrap();
|
|
|
|
- }
|
|
|
|
- app_dir
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/// initialize the logs dir
|
|
|
|
-fn init_log_dir() -> PathBuf {
|
|
|
|
- let log_dir = app_home_dir().join("logs");
|
|
|
|
- if !log_dir.exists() {
|
|
|
|
- fs::create_dir(&log_dir).unwrap();
|
|
|
|
- }
|
|
|
|
- log_dir
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
/// initialize this instance's log file
|
|
/// initialize this instance's log file
|
|
-fn init_log() {
|
|
|
|
- let log_dir = init_log_dir();
|
|
|
|
|
|
+fn init_log(log_dir: &PathBuf) {
|
|
let log_time = SystemTime::now()
|
|
let log_time = SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.duration_since(UNIX_EPOCH)
|
|
.unwrap()
|
|
.unwrap()
|
|
@@ -80,64 +51,77 @@ fn init_log() {
|
|
log4rs::init_config(config).unwrap();
|
|
log4rs::init_config(config).unwrap();
|
|
}
|
|
}
|
|
|
|
|
|
-/// Initialize & Get the clash config
|
|
|
|
-fn init_clash_config() -> Mapping {
|
|
|
|
- let app_dir = app_home_dir();
|
|
|
|
|
|
+/// 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_path = app_dir.join("config.yaml");
|
|
- let mut yaml_obj = serde_yaml::from_str::<Mapping>(CLASH_CONFIG).unwrap();
|
|
|
|
|
|
+ let yaml_tmpl = res_dir.join("config_tmp.yaml");
|
|
|
|
|
|
if !yaml_path.exists() {
|
|
if !yaml_path.exists() {
|
|
- fs::File::create(yaml_path)
|
|
|
|
- .unwrap()
|
|
|
|
- .write(CLASH_CONFIG.as_bytes())
|
|
|
|
- .unwrap();
|
|
|
|
- } else {
|
|
|
|
- let yaml_str = fs::read_to_string(yaml_path).unwrap();
|
|
|
|
- let user_obj = serde_yaml::from_str::<Mapping>(&yaml_str).unwrap();
|
|
|
|
- for (key, value) in user_obj.iter() {
|
|
|
|
- yaml_obj.insert(key.clone(), value.clone());
|
|
|
|
|
|
+ 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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- yaml_obj
|
|
|
|
|
|
+
|
|
|
|
+ let mmdb_path = app_dir.join("Country.mmdb");
|
|
|
|
+ let mmdb_tmpl = res_dir.join("Country.mmdb");
|
|
|
|
+
|
|
|
|
+ if !mmdb_path.exists() && mmdb_tmpl.exists() {
|
|
|
|
+ fs::copy(mmdb_tmpl, mmdb_path).unwrap();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-/// Initialize & Get the app config
|
|
|
|
-fn init_verge_config() -> Mapping {
|
|
|
|
- let app_dir = app_home_dir();
|
|
|
|
|
|
+/// 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_path = app_dir.join("verge.yaml");
|
|
- let mut yaml_obj = serde_yaml::from_str::<Mapping>(VERGE_CONFIG).unwrap();
|
|
|
|
|
|
+ let yaml_tmpl = res_dir.join("verge_tmp.yaml");
|
|
|
|
|
|
if !yaml_path.exists() {
|
|
if !yaml_path.exists() {
|
|
- fs::File::create(yaml_path)
|
|
|
|
- .unwrap()
|
|
|
|
- .write(VERGE_CONFIG.as_bytes())
|
|
|
|
- .unwrap();
|
|
|
|
- } else {
|
|
|
|
- let yaml_str = fs::read_to_string(yaml_path).unwrap();
|
|
|
|
- let user_obj = serde_yaml::from_str::<Mapping>(&yaml_str).unwrap();
|
|
|
|
- for (key, value) in user_obj.iter() {
|
|
|
|
- yaml_obj.insert(key.clone(), value.clone());
|
|
|
|
|
|
+ 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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- yaml_obj
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-#[derive(Debug)]
|
|
|
|
-pub struct InitApp {
|
|
|
|
- pub clash_config: Mapping,
|
|
|
|
- pub verge_config: Mapping,
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/// initialize app
|
|
/// initialize app
|
|
-pub fn init_app() -> InitApp {
|
|
|
|
- init_app_dir();
|
|
|
|
- init_log();
|
|
|
|
|
|
+pub fn init_app(package_info: &PackageInfo) {
|
|
|
|
+ // create app dir
|
|
|
|
+ let app_dir = app_home_dir();
|
|
|
|
+ let log_dir = app_dir.join("logs");
|
|
|
|
+ let profiles_dir = app_dir.join("profiles");
|
|
|
|
|
|
- let clash_config = init_clash_config();
|
|
|
|
- let verge_config = init_verge_config();
|
|
|
|
|
|
+ let res_dir = resource_dir(package_info).unwrap().join("resources");
|
|
|
|
|
|
- InitApp {
|
|
|
|
- clash_config,
|
|
|
|
- verge_config,
|
|
|
|
|
|
+ if !app_dir.exists() {
|
|
|
|
+ fs::create_dir(&app_dir).unwrap();
|
|
|
|
+ }
|
|
|
|
+ if !log_dir.exists() {
|
|
|
|
+ fs::create_dir(&log_dir).unwrap();
|
|
}
|
|
}
|
|
|
|
+ if !profiles_dir.exists() {
|
|
|
|
+ fs::create_dir(&profiles_dir).unwrap();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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() -> Mapping {
|
|
|
|
+ let yaml_path = app_home_dir().join("verge.yaml");
|
|
|
|
+ let yaml_str = fs::read_to_string(yaml_path).unwrap();
|
|
|
|
+ serde_yaml::from_str::<Mapping>(&yaml_str).unwrap()
|
|
}
|
|
}
|