mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
refactor: wip
This commit is contained in:
@@ -50,7 +50,7 @@ pub fn save_yaml<T: Serialize>(path: PathBuf, data: &T, prefix: Option<&str>) ->
|
||||
let data_str = serde_yaml::to_string(data)?;
|
||||
|
||||
let yaml_str = match prefix {
|
||||
Some(prefix) => format!("{prefix}{data_str}"),
|
||||
Some(prefix) => format!("{prefix}\n{data_str}"),
|
||||
None => data_str,
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use std::env::temp_dir;
|
||||
use std::path::PathBuf;
|
||||
use tauri::{
|
||||
api::path::{home_dir, resource_dir},
|
||||
@@ -13,7 +12,7 @@ static APP_DIR: &str = "clash-verge-dev";
|
||||
static CLASH_CONFIG: &str = "config.yaml";
|
||||
static VERGE_CONFIG: &str = "verge.yaml";
|
||||
static PROFILE_YAML: &str = "profiles.yaml";
|
||||
static PROFILE_TEMP: &str = "clash-verge-runtime.yaml";
|
||||
static CLASH_RUNTIME_YAML: &str = "clash-verge-runtime.yaml";
|
||||
|
||||
static mut RESOURCE_DIR: Option<PathBuf> = None;
|
||||
|
||||
@@ -99,12 +98,8 @@ pub fn profiles_path() -> PathBuf {
|
||||
app_home_dir().join(PROFILE_YAML)
|
||||
}
|
||||
|
||||
pub fn profiles_temp_path() -> PathBuf {
|
||||
#[cfg(not(feature = "debug-yml"))]
|
||||
return temp_dir().join(PROFILE_TEMP);
|
||||
|
||||
#[cfg(feature = "debug-yml")]
|
||||
return app_home_dir().join(PROFILE_TEMP);
|
||||
pub fn clash_runtime_yaml() -> PathBuf {
|
||||
app_home_dir().join(CLASH_RUNTIME_YAML)
|
||||
}
|
||||
|
||||
pub fn clash_pid_path() -> PathBuf {
|
||||
@@ -136,3 +131,11 @@ pub fn service_log_file() -> PathBuf {
|
||||
|
||||
log_file
|
||||
}
|
||||
|
||||
pub fn path_to_str(path: &PathBuf) -> anyhow::Result<&str> {
|
||||
let path_str = path
|
||||
.as_os_str()
|
||||
.to_str()
|
||||
.ok_or(anyhow::anyhow!("failed to get path from {:?}", path))?;
|
||||
Ok(path_str)
|
||||
}
|
||||
|
||||
@@ -69,28 +69,50 @@ pub fn open_file(path: PathBuf) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! error {
|
||||
($result: expr) => {
|
||||
log::error!(target: "app", "{}", $result);
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_if_err {
|
||||
($result: expr) => {
|
||||
if let Err(err) = $result {
|
||||
log::error!(target: "app", "{err}");
|
||||
}
|
||||
};
|
||||
($result: expr) => {
|
||||
if let Err(err) = $result {
|
||||
log::error!(target: "app", "{err}");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_err {
|
||||
($result: expr) => {
|
||||
if let Err(err) = $result {
|
||||
log::error!(target: "app", "{err}");
|
||||
}
|
||||
};
|
||||
|
||||
($result: expr, $err_str: expr) => {
|
||||
if let Err(_) = $result {
|
||||
log::error!(target: "app", "{}", $err_str);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// wrap the anyhow error
|
||||
/// transform the error to String
|
||||
#[macro_export]
|
||||
macro_rules! wrap_err {
|
||||
($stat: expr) => {
|
||||
match $stat {
|
||||
Ok(a) => Ok(a),
|
||||
Err(err) => {
|
||||
log::error!(target: "app", "{}", err.to_string());
|
||||
Err(format!("{}", err.to_string()))
|
||||
}
|
||||
}
|
||||
};
|
||||
($stat: expr) => {
|
||||
match $stat {
|
||||
Ok(a) => Ok(a),
|
||||
Err(err) => {
|
||||
log::error!(target: "app", "{}", err.to_string());
|
||||
Err(format!("{}", err.to_string()))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// return the string literal error
|
||||
|
||||
@@ -87,19 +87,28 @@ pub fn init_resources(package_info: &PackageInfo) {
|
||||
}
|
||||
|
||||
// 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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// // 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);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
use crate::{
|
||||
core::{tray, Core},
|
||||
data::Data,
|
||||
utils::init,
|
||||
utils::server,
|
||||
};
|
||||
use crate::log_err;
|
||||
use crate::{config::VergeN, core::*, utils::init, utils::server};
|
||||
use tauri::{App, AppHandle, Manager};
|
||||
|
||||
/// handle something when start app
|
||||
pub fn resolve_setup(app: &App) {
|
||||
let _ = app
|
||||
.tray_handle()
|
||||
.set_menu(tray::Tray::tray_menu(&app.app_handle()));
|
||||
pub fn resolve_setup(app: &mut App) {
|
||||
#[cfg(target_os = "macos")]
|
||||
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
|
||||
|
||||
handle::Handle::global().init(app.app_handle());
|
||||
|
||||
init::init_resources(app.package_info());
|
||||
|
||||
// 启动核心
|
||||
log_err!(CoreManager::global().init());
|
||||
|
||||
// log_err!(app
|
||||
// .tray_handle()
|
||||
// .set_menu(tray::Tray::tray_menu(&app.app_handle())));
|
||||
|
||||
log_err!(tray::Tray::update_systray(&app.app_handle()));
|
||||
|
||||
// setup a simple http server for singleton
|
||||
server::embed_server(app.app_handle());
|
||||
|
||||
let silent_start = {
|
||||
let global = Data::global();
|
||||
let verge = global.verge.lock();
|
||||
let singleton = verge.app_singleton_port.clone();
|
||||
|
||||
// setup a simple http server for singleton
|
||||
server::embed_server(&app.app_handle(), singleton);
|
||||
|
||||
verge.enable_silent_start.clone().unwrap_or(false)
|
||||
let verge = VergeN::global().config.lock();
|
||||
verge.enable_silent_start.clone()
|
||||
};
|
||||
|
||||
// core should be initialized after init_app fix #122
|
||||
let core = Core::global();
|
||||
core.init(app.app_handle());
|
||||
|
||||
if !silent_start {
|
||||
if !silent_start.unwrap_or(false) {
|
||||
create_window(&app.app_handle());
|
||||
}
|
||||
|
||||
log_err!(sysopt::Sysopt::global().init_launch());
|
||||
log_err!(sysopt::Sysopt::global().init_sysproxy());
|
||||
|
||||
log_err!(handle::Handle::update_systray_part());
|
||||
log_err!(hotkey::Hotkey::global().init(app.app_handle()));
|
||||
log_err!(timer::Timer::global().init());
|
||||
}
|
||||
|
||||
/// reset system proxy
|
||||
pub fn resolve_reset() {
|
||||
let core = Core::global();
|
||||
let mut sysopt = core.sysopt.lock();
|
||||
crate::log_if_err!(sysopt.reset_sysproxy());
|
||||
drop(sysopt);
|
||||
|
||||
let mut service = core.service.lock();
|
||||
crate::log_if_err!(service.stop());
|
||||
log_err!(sysopt::Sysopt::global().reset_sysproxy());
|
||||
log_err!(CoreManager::global().stop_core());
|
||||
}
|
||||
|
||||
/// create main window
|
||||
|
||||
@@ -1,20 +1,14 @@
|
||||
extern crate warp;
|
||||
|
||||
use super::resolve;
|
||||
use crate::data::Verge;
|
||||
use crate::config::VergeN;
|
||||
use port_scanner::local_port_available;
|
||||
use tauri::AppHandle;
|
||||
use warp::Filter;
|
||||
|
||||
#[cfg(not(feature = "verge-dev"))]
|
||||
const SERVER_PORT: u16 = 33331;
|
||||
#[cfg(feature = "verge-dev")]
|
||||
const SERVER_PORT: u16 = 11233;
|
||||
|
||||
/// check whether there is already exists
|
||||
pub fn check_singleton() -> Result<(), ()> {
|
||||
let verge = Verge::new();
|
||||
let port = verge.app_singleton_port.unwrap_or(SERVER_PORT);
|
||||
let port = VergeN::get_singleton_port();
|
||||
|
||||
if !local_port_available(port) {
|
||||
tauri::async_runtime::block_on(async {
|
||||
@@ -29,14 +23,14 @@ pub fn check_singleton() -> Result<(), ()> {
|
||||
|
||||
/// The embed server only be used to implement singleton process
|
||||
/// maybe it can be used as pac server later
|
||||
pub fn embed_server(app_handle: &AppHandle, port: Option<u16>) {
|
||||
pub fn embed_server(app_handle: AppHandle) {
|
||||
let app_handle = app_handle.clone();
|
||||
let port = port.unwrap_or(SERVER_PORT);
|
||||
let port = VergeN::get_singleton_port();
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let commands = warp::path!("commands" / "visible").map(move || {
|
||||
resolve::create_window(&app_handle);
|
||||
return format!("ok");
|
||||
format!("ok")
|
||||
});
|
||||
|
||||
warp::serve(commands).bind(([127, 0, 0, 1], port)).await;
|
||||
|
||||
Reference in New Issue
Block a user