Files
clash-proxy/src-tauri/src/utils/init.rs

115 lines
3.4 KiB
Rust
Raw Normal View History

2022-02-18 23:57:13 +08:00
use crate::utils::{dirs, tmpl};
2022-07-30 23:32:52 +08:00
use anyhow::Result;
2021-12-19 17:50:25 +08:00
use chrono::Local;
2021-12-08 23:40:52 +08:00
use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
2022-07-17 17:39:44 +08:00
use log4rs::config::{Appender, Config, Logger, Root};
2021-12-08 23:40:52 +08:00
use log4rs::encode::pattern::PatternEncoder;
2022-02-28 01:34:25 +08:00
use std::fs;
2021-12-08 23:40:52 +08:00
use std::io::Write;
2021-12-12 17:46:16 +08:00
use tauri::PackageInfo;
2021-12-08 23:40:52 +08:00
/// initialize this instance's log file
2022-10-28 01:02:47 +08:00
fn init_log() -> Result<()> {
2022-11-12 11:37:23 +08:00
let log_dir = dirs::app_logs_dir();
if !log_dir.exists() {
let _ = fs::create_dir_all(&log_dir);
}
2022-10-28 01:02:47 +08:00
2022-11-12 11:37:23 +08:00
let local_time = Local::now().format("%Y-%m-%d-%H%M%S").to_string();
let log_file = format!("{}.log", local_time);
let log_file = log_dir.join(log_file);
2021-12-08 23:40:52 +08:00
2022-11-12 11:37:23 +08:00
let time_format = "{d(%Y-%m-%d %H:%M:%S)} - {m}{n}";
let stdout = ConsoleAppender::builder()
.encoder(Box::new(PatternEncoder::new(time_format)))
.build();
let tofile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new(time_format)))
.build(log_file)?;
2021-12-08 23:40:52 +08:00
2022-11-12 11:37:23 +08:00
let config = Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout)))
.appender(Appender::builder().build("file", Box::new(tofile)))
.logger(
Logger::builder()
.appenders(["file", "stdout"])
.additive(false)
.build("app", LevelFilter::Info),
)
.build(Root::builder().appender("stdout").build(LevelFilter::Info))?;
2021-12-08 23:40:52 +08:00
2022-11-12 11:37:23 +08:00
log4rs::init_config(config)?;
2022-07-30 23:32:52 +08:00
2022-11-12 11:37:23 +08:00
Ok(())
2021-12-08 23:40:52 +08:00
}
2021-12-14 00:40:41 +08:00
/// Initialize all the files from resources
2022-10-28 01:02:47 +08:00
pub fn init_config() -> Result<()> {
2022-11-12 11:37:23 +08:00
let _ = init_log();
2022-10-28 01:02:47 +08:00
2022-11-12 11:37:23 +08:00
let app_dir = dirs::app_home_dir();
let profiles_dir = dirs::app_profiles_dir();
2022-10-28 01:02:47 +08:00
2022-11-12 11:37:23 +08:00
if !app_dir.exists() {
let _ = fs::create_dir_all(&app_dir);
}
if !profiles_dir.exists() {
let _ = fs::create_dir_all(&profiles_dir);
}
2022-10-28 01:02:47 +08:00
2022-11-12 11:37:23 +08:00
// 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");
2021-12-08 23:40:52 +08:00
2022-11-12 11:37:23 +08:00
if !clash_path.exists() {
fs::File::create(clash_path)?.write(tmpl::CLASH_CONFIG)?;
}
if !verge_path.exists() {
fs::File::create(verge_path)?.write(tmpl::VERGE_CONFIG)?;
}
if !profile_path.exists() {
fs::File::create(profile_path)?.write(tmpl::PROFILES_CONFIG)?;
}
Ok(())
2021-12-08 23:40:52 +08:00
}
/// initialize app
2022-10-28 01:02:47 +08:00
pub fn init_resources(package_info: &PackageInfo) {
2022-11-12 11:37:23 +08:00
// create app dir
let app_dir = dirs::app_home_dir();
let res_dir = dirs::app_resources_dir(package_info);
2021-12-08 23:40:52 +08:00
2022-11-12 11:37:23 +08:00
if !app_dir.exists() {
let _ = fs::create_dir_all(&app_dir);
}
2022-02-18 23:57:13 +08:00
2022-11-12 11:37:23 +08:00
// copy the resource file
2022-11-14 01:26:33 +08:00
for file in ["Country.mmdb", "geoip.dat", "geosite.dat", "wintun.dll"].iter() {
let src_path = res_dir.join(file);
let target_path = app_dir.join(file);
if src_path.exists() {
let _ = fs::copy(src_path, target_path);
2022-11-12 11:37:23 +08:00
}
2022-03-18 11:49:00 +08:00
}
2022-11-14 01:26:33 +08:00
// // copy the resource file
// let mmdb_path = app_dir.join("Country.mmdb");
// let mmdb_tmpl = res_dir.join("Country.mmdb");
// if !mmdb_path.exists() && mmdb_tmpl.exists() {
// let _ = fs::copy(mmdb_tmpl, mmdb_path);
// }
// // copy the wintun.dll
// #[cfg(target_os = "windows")]
// {
// let wintun_path = app_dir.join("wintun.dll");
// let wintun_tmpl = res_dir.join("wintun.dll");
// if !wintun_path.exists() && wintun_tmpl.exists() {
// let _ = fs::copy(wintun_tmpl, wintun_path);
// }
// }
2021-12-08 23:40:52 +08:00
}