270 lines
7.2 KiB
Rust
Raw Normal View History

2022-01-07 23:29:20 +08:00
use crate::{
2022-11-14 01:26:33 +08:00
config::*,
core::*,
feat,
2022-11-12 11:37:23 +08:00
utils::{dirs, help},
2022-01-07 23:29:20 +08:00
};
2022-11-14 01:26:33 +08:00
use crate::{ret_err, wrap_err};
2022-11-18 18:18:41 +08:00
use anyhow::{Context, Result};
2022-01-07 23:29:20 +08:00
use serde_yaml::Mapping;
2022-09-05 20:30:39 +08:00
use std::collections::{HashMap, VecDeque};
2022-09-11 20:58:55 +08:00
use sysproxy::Sysproxy;
2022-01-07 23:29:20 +08:00
2022-04-19 01:41:20 +08:00
type CmdResult<T = ()> = Result<T, String>;
2022-01-07 23:29:20 +08:00
#[tauri::command]
2022-11-14 01:26:33 +08:00
pub fn get_profiles() -> CmdResult<IProfiles> {
2022-11-17 17:07:13 +08:00
Ok(Config::profiles().data().clone())
2022-01-07 23:29:20 +08:00
}
#[tauri::command]
2022-11-14 01:26:33 +08:00
pub async fn enhance_profiles() -> CmdResult {
2022-11-18 18:18:41 +08:00
wrap_err!(CoreManager::global().update_config().await)?;
handle::Handle::refresh_clash();
Ok(())
2022-01-07 23:29:20 +08:00
}
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub async fn import_profile(url: String, option: Option<PrfOption>) -> CmdResult {
2022-11-12 11:37:23 +08:00
let item = wrap_err!(PrfItem::from_url(&url, None, None, option).await)?;
2022-11-17 17:07:13 +08:00
wrap_err!(Config::profiles().data().append_item(item))
2022-01-07 23:29:20 +08:00
}
2022-02-07 17:26:05 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub async fn create_profile(item: PrfItem, file_data: Option<String>) -> CmdResult {
2022-11-12 11:37:23 +08:00
let item = wrap_err!(PrfItem::from(item, file_data).await)?;
2022-11-17 17:07:13 +08:00
wrap_err!(Config::profiles().data().append_item(item))
2022-02-07 17:26:05 +08:00
}
2022-01-07 23:29:20 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResult {
2022-11-17 17:07:13 +08:00
wrap_err!(feat::update_profile(index, option).await)
2022-01-07 23:29:20 +08:00
}
#[tauri::command]
2022-11-18 18:18:41 +08:00
pub async fn delete_profile(index: String) -> CmdResult {
let should_update = wrap_err!({ Config::profiles().data().delete_item(index) })?;
if should_update {
wrap_err!(CoreManager::global().update_config().await)?;
handle::Handle::refresh_clash();
2022-11-17 17:07:13 +08:00
}
2022-11-18 18:18:41 +08:00
Ok(())
2022-03-06 14:59:25 +08:00
}
2022-11-18 18:18:41 +08:00
/// 修改profiles的
#[tauri::command]
2022-11-18 18:18:41 +08:00
pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult {
wrap_err!({ Config::profiles().draft().patch_config(profiles) })?;
2022-11-17 17:07:13 +08:00
2022-11-18 18:18:41 +08:00
match CoreManager::global().update_config().await {
2022-11-17 17:07:13 +08:00
Ok(_) => {
2022-11-18 18:18:41 +08:00
handle::Handle::refresh_clash();
2022-11-17 17:07:13 +08:00
Config::profiles().apply();
wrap_err!(Config::profiles().data().save_file())?;
Ok(())
}
Err(err) => {
Config::profiles().discard();
log::error!(target: "app", "{err}");
Err(format!("{err}"))
}
}
2022-03-06 14:59:25 +08:00
}
2022-11-18 18:18:41 +08:00
/// 修改某个profile item的
2022-01-07 23:29:20 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
2022-11-17 17:07:13 +08:00
wrap_err!(Config::profiles().data().patch_item(index, profile))?;
2022-11-14 01:26:33 +08:00
wrap_err!(timer::Timer::global().refresh())
2022-01-07 23:29:20 +08:00
}
2022-01-17 02:16:17 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn view_profile(index: String) -> CmdResult {
2022-11-17 17:07:13 +08:00
let file = {
wrap_err!(Config::profiles().latest().get_item(&index))?
.file
.clone()
.ok_or("the file field is null")
}?;
2022-01-17 02:16:17 +08:00
let path = wrap_err!(dirs::app_profiles_dir())?.join(file);
2022-11-12 11:37:23 +08:00
if !path.exists() {
2022-11-14 01:26:33 +08:00
ret_err!("the file not found");
2022-11-12 11:37:23 +08:00
}
2022-01-17 02:16:17 +08:00
2022-11-12 11:37:23 +08:00
wrap_err!(help::open_file(path))
2022-01-17 02:16:17 +08:00
}
2022-03-27 00:58:17 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn read_profile_file(index: String) -> CmdResult<String> {
2022-11-17 17:07:13 +08:00
let profiles = Config::profiles();
let profiles = profiles.latest();
2022-11-12 11:37:23 +08:00
let item = wrap_err!(profiles.get_item(&index))?;
let data = wrap_err!(item.read_file())?;
Ok(data)
2022-03-27 00:58:17 +08:00
}
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn save_profile_file(index: String, file_data: Option<String>) -> CmdResult {
2022-11-12 11:37:23 +08:00
if file_data.is_none() {
return Ok(());
}
2022-03-27 00:58:17 +08:00
2022-11-17 17:07:13 +08:00
let profiles = Config::profiles();
let profiles = profiles.latest();
2022-11-12 11:37:23 +08:00
let item = wrap_err!(profiles.get_item(&index))?;
wrap_err!(item.save_file(file_data.unwrap()))
2022-03-27 00:58:17 +08:00
}
2022-01-07 23:29:20 +08:00
#[tauri::command]
pub fn get_clash_info() -> CmdResult<ClashInfo> {
Ok(Config::clash().latest().get_client_info())
2022-01-07 23:29:20 +08:00
}
2022-07-25 01:20:13 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn get_runtime_config() -> CmdResult<Option<Mapping>> {
2022-11-18 18:18:41 +08:00
Ok(Config::runtime().latest().config.clone())
2022-08-12 03:20:55 +08:00
}
#[tauri::command]
2022-11-18 18:18:41 +08:00
pub fn get_runtime_yaml() -> CmdResult<String> {
let runtime = Config::runtime();
let runtime = runtime.latest();
let config = runtime.config.as_ref();
wrap_err!(config
.ok_or(anyhow::anyhow!("failed to parse config to yaml file"))
.and_then(
|config| serde_yaml::to_string(config).context("failed to convert config to yaml")
))
2022-08-12 03:20:55 +08:00
}
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn get_runtime_exists() -> CmdResult<Vec<String>> {
2022-11-18 18:18:41 +08:00
Ok(Config::runtime().latest().exists_keys.clone())
2022-08-12 03:20:55 +08:00
}
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn get_runtime_logs() -> CmdResult<HashMap<String, Vec<(String, String)>>> {
2022-11-18 18:18:41 +08:00
Ok(Config::runtime().latest().chain_logs.clone())
2022-07-25 01:20:13 +08:00
}
2022-01-07 23:29:20 +08:00
#[tauri::command]
2022-11-17 17:07:13 +08:00
pub async fn patch_clash_config(payload: Mapping) -> CmdResult {
wrap_err!(feat::patch_clash(payload).await)
2022-01-07 23:29:20 +08:00
}
#[tauri::command]
2022-11-14 01:26:33 +08:00
pub fn get_verge_config() -> CmdResult<IVerge> {
2022-11-17 17:07:13 +08:00
Ok(Config::verge().data().clone())
2022-04-20 01:44:47 +08:00
}
2022-04-13 01:09:51 +08:00
2022-09-14 01:19:02 +08:00
#[tauri::command]
2022-11-17 17:07:13 +08:00
pub async fn patch_verge_config(payload: IVerge) -> CmdResult {
wrap_err!(feat::patch_verge(payload).await)
2022-09-14 01:19:02 +08:00
}
2022-05-17 01:59:49 +08:00
#[tauri::command]
2022-11-14 22:50:47 +08:00
pub async fn change_clash_core(clash_core: Option<String>) -> CmdResult {
wrap_err!(CoreManager::global().change_core(clash_core).await)
2022-05-17 01:59:49 +08:00
}
2022-04-20 01:44:47 +08:00
/// restart the sidecar
#[tauri::command]
2022-11-17 20:19:40 +08:00
pub async fn restart_sidecar() -> CmdResult {
wrap_err!(CoreManager::global().run_core().await)
2022-02-17 02:10:25 +08:00
}
#[tauri::command]
pub fn grant_permission(core: String) -> CmdResult {
2023-03-16 23:57:34 +08:00
#[cfg(any(target_os = "macos", target_os = "linux"))]
return wrap_err!(manager::grant_permission(core));
2023-03-16 23:57:34 +08:00
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
2023-03-16 13:51:46 +08:00
return Err("Unsupported target".into());
}
2022-04-20 01:44:47 +08:00
/// get the system proxy
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn get_sys_proxy() -> CmdResult<Mapping> {
2022-11-12 11:37:23 +08:00
let current = wrap_err!(Sysproxy::get_system_proxy())?;
2022-04-20 01:44:47 +08:00
2022-11-12 11:37:23 +08:00
let mut map = Mapping::new();
map.insert("enable".into(), current.enable.into());
map.insert(
"server".into(),
format!("{}:{}", current.host, current.port).into(),
);
map.insert("bypass".into(), current.bypass.into());
2022-09-11 20:58:55 +08:00
2022-11-12 11:37:23 +08:00
Ok(map)
2022-04-20 01:44:47 +08:00
}
2022-09-05 20:30:39 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn get_clash_logs() -> CmdResult<VecDeque<String>> {
2022-11-14 01:26:33 +08:00
Ok(logger::Logger::global().get_log())
2022-09-05 20:30:39 +08:00
}
2022-02-16 03:21:34 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn open_app_dir() -> CmdResult<()> {
let app_dir = wrap_err!(dirs::app_home_dir())?;
2022-11-12 11:37:23 +08:00
wrap_err!(open::that(app_dir))
2022-02-16 03:21:34 +08:00
}
2022-12-13 00:44:24 +08:00
#[tauri::command]
pub fn open_core_dir() -> CmdResult<()> {
let core_dir = wrap_err!(tauri::utils::platform::current_exe())?;
let core_dir = core_dir.parent().ok_or(format!("failed to get core dir"))?;
wrap_err!(open::that(core_dir))
}
2022-02-16 03:21:34 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn open_logs_dir() -> CmdResult<()> {
let log_dir = wrap_err!(dirs::app_logs_dir())?;
2022-11-12 11:37:23 +08:00
wrap_err!(open::that(log_dir))
2022-02-16 03:21:34 +08:00
}
2022-04-24 21:00:17 +08:00
2022-08-06 21:56:54 +08:00
#[tauri::command]
2022-09-11 20:58:55 +08:00
pub fn open_web_url(url: String) -> CmdResult<()> {
2022-11-12 11:37:23 +08:00
wrap_err!(open::that(url))
2022-08-06 21:56:54 +08:00
}
2022-04-24 21:00:17 +08:00
#[cfg(windows)]
pub mod service {
2022-11-12 11:37:23 +08:00
use super::*;
2022-11-17 20:19:40 +08:00
use crate::core::win_service;
2022-11-12 11:37:23 +08:00
#[tauri::command]
2022-11-17 20:19:40 +08:00
pub async fn check_service() -> CmdResult<win_service::JsonResponse> {
2022-11-17 22:52:22 +08:00
wrap_err!(win_service::check_service().await)
2022-11-12 11:37:23 +08:00
}
#[tauri::command]
2022-11-14 01:26:33 +08:00
pub async fn install_service() -> CmdResult {
2022-11-17 20:19:40 +08:00
wrap_err!(win_service::install_service().await)
2022-11-12 11:37:23 +08:00
}
2022-11-12 11:37:23 +08:00
#[tauri::command]
2022-11-14 01:26:33 +08:00
pub async fn uninstall_service() -> CmdResult {
2022-11-17 20:19:40 +08:00
wrap_err!(win_service::uninstall_service().await)
2022-11-12 11:37:23 +08:00
}
2022-04-24 21:00:17 +08:00
}
#[cfg(not(windows))]
pub mod service {
2022-11-12 11:37:23 +08:00
use super::*;
2022-04-24 21:00:17 +08:00
2022-11-12 11:37:23 +08:00
#[tauri::command]
2022-11-14 01:26:33 +08:00
pub async fn check_service() -> CmdResult {
2022-11-12 11:37:23 +08:00
Ok(())
}
#[tauri::command]
2022-11-14 01:26:33 +08:00
pub async fn install_service() -> CmdResult {
2022-11-12 11:37:23 +08:00
Ok(())
}
#[tauri::command]
2022-11-14 01:26:33 +08:00
pub async fn uninstall_service() -> CmdResult {
2022-11-12 11:37:23 +08:00
Ok(())
}
2022-04-24 21:00:17 +08:00
}