clash-verge/src-tauri/src/cmds.rs

330 lines
8.0 KiB
Rust
Raw Normal View History

2022-01-07 18:29:20 +03:00
use crate::{
2022-07-12 19:54:47 +03:00
core::{ClashInfo, Core, PrfItem, PrfOption, Profiles, Verge},
utils::{dirs, help, sysopt::SysProxyConfig},
2022-01-07 18:29:20 +03:00
};
2022-04-18 20:41:20 +03:00
use crate::{log_if_err, ret_err, wrap_err};
2022-02-27 20:34:25 +03:00
use anyhow::Result;
2022-01-07 18:29:20 +03:00
use serde_yaml::Mapping;
2022-04-19 20:44:47 +03:00
use tauri::{api, State};
2022-01-07 18:29:20 +03:00
2022-04-18 20:41:20 +03:00
type CmdResult<T = ()> = Result<T, String>;
2022-01-07 18:29:20 +03:00
/// get all profiles from `profiles.yaml`
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn get_profiles(core: State<'_, Core>) -> CmdResult<Profiles> {
2022-07-12 19:54:47 +03:00
let profiles = core.profiles.lock();
Ok(profiles.clone())
2022-01-07 18:29:20 +03:00
}
2022-04-19 20:44:47 +03:00
/// manually exec enhanced profile
2022-01-07 18:29:20 +03:00
#[tauri::command]
2022-04-19 20:44:47 +03:00
pub fn enhance_profiles(core: State<'_, Core>) -> CmdResult {
2022-07-12 19:54:47 +03:00
wrap_err!(core.activate_enhanced(false))
2022-01-07 18:29:20 +03:00
}
2022-01-16 17:57:42 +03:00
/// import the profile from url
2022-01-07 18:29:20 +03:00
/// and save to `profiles.yaml`
#[tauri::command]
2022-01-16 17:57:42 +03:00
pub async fn import_profile(
2022-07-12 19:54:47 +03:00
url: String,
option: Option<PrfOption>,
core: State<'_, Core>,
2022-04-18 20:41:20 +03:00
) -> CmdResult {
2022-07-12 19:54:47 +03:00
let item = wrap_err!(PrfItem::from_url(&url, None, None, option).await)?;
2022-03-01 03:58:47 +03:00
2022-07-12 19:54:47 +03:00
let mut profiles = core.profiles.lock();
wrap_err!(profiles.append_item(item))
2022-01-07 18:29:20 +03:00
}
2022-02-07 12:26:05 +03:00
/// new a profile
/// append a temp profile item file to the `profiles` dir
/// view the temp profile file by using vscode or other editor
#[tauri::command]
2022-03-05 14:04:20 +03:00
pub async fn create_profile(
2022-07-12 19:54:47 +03:00
item: PrfItem, // partial
file_data: Option<String>,
core: State<'_, Core>,
2022-04-18 20:41:20 +03:00
) -> CmdResult {
2022-07-12 19:54:47 +03:00
let item = wrap_err!(PrfItem::from(item, file_data).await)?;
2022-03-01 03:58:47 +03:00
2022-07-12 19:54:47 +03:00
let mut profiles = core.profiles.lock();
wrap_err!(profiles.append_item(item))
2022-02-07 12:26:05 +03:00
}
2022-01-07 18:29:20 +03:00
/// Update the profile
#[tauri::command]
pub async fn update_profile(
2022-07-12 19:54:47 +03:00
index: String,
option: Option<PrfOption>,
core: State<'_, Core>,
2022-04-18 20:41:20 +03:00
) -> CmdResult {
2022-07-12 19:54:47 +03:00
wrap_err!(Core::update_profile_item(core.inner().clone(), index, option).await)
2022-01-07 18:29:20 +03:00
}
/// change the current profile
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn select_profile(index: String, core: State<'_, Core>) -> CmdResult {
2022-07-12 19:54:47 +03:00
let mut profiles = core.profiles.lock();
wrap_err!(profiles.put_current(index))?;
2022-04-19 09:38:59 +03:00
2022-07-12 19:54:47 +03:00
drop(profiles);
2022-04-18 20:41:20 +03:00
2022-07-12 19:54:47 +03:00
wrap_err!(core.activate_enhanced(false))
2022-01-07 18:29:20 +03:00
}
2022-03-06 09:59:25 +03:00
/// change the profile chain
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn change_profile_chain(chain: Option<Vec<String>>, core: State<'_, Core>) -> CmdResult {
2022-07-12 19:54:47 +03:00
let mut profiles = core.profiles.lock();
2022-08-08 18:17:22 +03:00
wrap_err!(profiles.put_chain(chain))?;
2022-03-06 09:59:25 +03:00
2022-07-12 19:54:47 +03:00
drop(profiles);
2022-03-06 09:59:25 +03:00
2022-07-12 19:54:47 +03:00
wrap_err!(core.activate_enhanced(false))
2022-03-06 09:59:25 +03:00
}
/// change the profile valid fields
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn change_profile_valid(valid: Option<Vec<String>>, core: State<Core>) -> CmdResult {
2022-07-12 19:54:47 +03:00
let mut profiles = core.profiles.lock();
2022-08-08 18:17:22 +03:00
wrap_err!(profiles.put_valid(valid))?;
2022-04-18 20:41:20 +03:00
2022-07-12 19:54:47 +03:00
drop(profiles);
2022-07-12 19:54:47 +03:00
wrap_err!(core.activate_enhanced(false))
2022-03-06 09:59:25 +03:00
}
2022-01-07 18:29:20 +03:00
/// delete profile item
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn delete_profile(index: String, core: State<'_, Core>) -> CmdResult {
2022-07-12 19:54:47 +03:00
let mut profiles = core.profiles.lock();
2022-02-27 20:34:25 +03:00
2022-07-12 19:54:47 +03:00
if wrap_err!(profiles.delete_item(index))? {
drop(profiles);
2022-04-19 20:44:47 +03:00
2022-07-12 19:54:47 +03:00
log_if_err!(core.activate_enhanced(false));
}
2022-02-27 20:34:25 +03:00
2022-07-12 19:54:47 +03:00
Ok(())
2022-01-07 18:29:20 +03:00
}
/// patch the profile config
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn patch_profile(index: String, profile: PrfItem, core: State<'_, Core>) -> CmdResult {
2022-07-12 19:54:47 +03:00
let mut profiles = core.profiles.lock();
wrap_err!(profiles.patch_item(index, profile))?;
drop(profiles);
2022-04-18 20:41:20 +03:00
2022-07-12 19:54:47 +03:00
// update cron task
let mut timer = core.timer.lock();
wrap_err!(timer.refresh())
2022-01-07 18:29:20 +03:00
}
2022-01-16 21:16:17 +03:00
/// run vscode command to edit the profile
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn view_profile(index: String, core: State<'_, Core>) -> CmdResult {
2022-07-12 19:54:47 +03:00
let profiles = core.profiles.lock();
let item = wrap_err!(profiles.get_item(&index))?;
2022-01-16 21:16:17 +03:00
2022-07-12 19:54:47 +03:00
let file = item.file.clone();
if file.is_none() {
ret_err!("the file is null");
}
2022-01-16 21:16:17 +03:00
2022-07-12 19:54:47 +03:00
let path = dirs::app_profiles_dir().join(file.unwrap());
if !path.exists() {
ret_err!("the file not found");
}
2022-01-16 21:16:17 +03:00
2022-07-12 19:54:47 +03:00
wrap_err!(help::open_file(path))
2022-01-16 21:16:17 +03:00
}
2022-03-26 19:58:17 +03:00
/// read the profile item file data
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn read_profile_file(index: String, core: State<'_, Core>) -> CmdResult<String> {
2022-07-12 19:54:47 +03:00
let profiles = core.profiles.lock();
2022-04-18 20:41:20 +03:00
2022-07-12 19:54:47 +03:00
let item = wrap_err!(profiles.get_item(&index))?;
let data = wrap_err!(item.read_file())?;
2022-03-26 19:58:17 +03:00
2022-07-12 19:54:47 +03:00
Ok(data)
2022-03-26 19:58:17 +03:00
}
/// save the profile item file data
#[tauri::command]
pub fn save_profile_file(
2022-07-12 19:54:47 +03:00
index: String,
file_data: Option<String>,
core: State<'_, Core>,
2022-04-18 20:41:20 +03:00
) -> CmdResult {
2022-07-12 19:54:47 +03:00
if file_data.is_none() {
return Ok(());
}
2022-03-26 19:58:17 +03:00
2022-07-12 19:54:47 +03:00
let profiles = core.profiles.lock();
let item = wrap_err!(profiles.get_item(&index))?;
wrap_err!(item.save_file(file_data.unwrap()))
2022-03-26 19:58:17 +03:00
}
2022-01-07 18:29:20 +03:00
/// get the clash core info from the state
/// the caller can also get the infomation by clash's api
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn get_clash_info(core: State<'_, Core>) -> CmdResult<ClashInfo> {
2022-07-12 19:54:47 +03:00
let clash = core.clash.lock();
Ok(clash.info.clone())
2022-01-07 18:29:20 +03:00
}
2022-07-24 20:20:13 +03:00
/// get the running clash config string
#[tauri::command]
pub fn get_running_config(core: State<'_, Core>) -> CmdResult<Option<String>> {
let clash = core.clash.lock();
Ok(clash.running_config.clone())
}
2022-01-07 18:29:20 +03:00
/// update the clash core config
/// after putting the change to the clash core
/// then we should save the latest config
#[tauri::command]
pub fn patch_clash_config(
2022-07-12 19:54:47 +03:00
payload: Mapping,
app_handle: tauri::AppHandle,
core: State<'_, Core>,
) -> CmdResult {
2022-07-12 19:54:47 +03:00
wrap_err!(core.patch_clash(payload, &app_handle))
2022-01-07 18:29:20 +03:00
}
/// get the verge config
#[tauri::command]
2022-04-20 06:17:54 +03:00
pub fn get_verge_config(core: State<'_, Core>) -> CmdResult<Verge> {
2022-07-12 19:54:47 +03:00
let verge = core.verge.lock();
Ok(verge.clone())
2022-01-07 18:29:20 +03:00
}
/// patch the verge config
/// this command only save the config and not responsible for other things
#[tauri::command]
2022-02-20 13:53:21 +03:00
pub fn patch_verge_config(
2022-07-12 19:54:47 +03:00
payload: Verge,
app_handle: tauri::AppHandle,
core: State<'_, Core>,
2022-05-16 20:59:49 +03:00
) -> CmdResult {
2022-07-12 19:54:47 +03:00
wrap_err!(core.patch_verge(payload, &app_handle))
2022-04-19 20:44:47 +03:00
}
2022-04-12 20:09:51 +03:00
2022-05-16 20:59:49 +03:00
/// change clash core
#[tauri::command]
pub fn change_clash_core(core: State<'_, Core>, clash_core: Option<String>) -> CmdResult {
2022-07-12 19:54:47 +03:00
wrap_err!(core.change_core(clash_core))
2022-05-16 20:59:49 +03:00
}
2022-04-19 20:44:47 +03:00
/// restart the sidecar
#[tauri::command]
pub fn restart_sidecar(core: State<'_, Core>) -> CmdResult {
2022-07-12 19:54:47 +03:00
wrap_err!(core.restart_clash())
2022-01-07 18:29:20 +03:00
}
2022-02-15 22:21:34 +03:00
2022-02-16 21:10:25 +03:00
/// kill all sidecars when update app
#[tauri::command]
2022-04-19 20:44:47 +03:00
pub fn kill_sidecar() {
2022-07-12 19:54:47 +03:00
api::process::kill_children();
2022-02-16 21:10:25 +03:00
}
2022-04-19 20:44:47 +03:00
/// get the system proxy
#[tauri::command]
pub fn get_sys_proxy() -> Result<SysProxyConfig, String> {
2022-07-12 19:54:47 +03:00
wrap_err!(SysProxyConfig::get_sys())
2022-04-19 20:44:47 +03:00
}
/// get the current proxy config
/// which may not the same as system proxy
#[tauri::command]
pub fn get_cur_proxy(core: State<'_, Core>) -> CmdResult<Option<SysProxyConfig>> {
2022-07-12 19:54:47 +03:00
let sysopt = core.sysopt.lock();
wrap_err!(sysopt.get_sysproxy())
2022-04-19 20:44:47 +03:00
}
2022-02-15 22:21:34 +03:00
/// open app config dir
#[tauri::command]
pub fn open_app_dir() -> Result<(), String> {
2022-07-12 19:54:47 +03:00
let app_dir = dirs::app_home_dir();
wrap_err!(open::that(app_dir))
2022-02-15 22:21:34 +03:00
}
/// open logs dir
#[tauri::command]
pub fn open_logs_dir() -> Result<(), String> {
2022-07-12 19:54:47 +03:00
let log_dir = dirs::app_logs_dir();
wrap_err!(open::that(log_dir))
2022-02-15 22:21:34 +03:00
}
2022-04-24 16:00:17 +03:00
2022-08-06 16:56:54 +03:00
/// open url
#[tauri::command]
pub fn open_web_url(url: String) -> Result<(), String> {
wrap_err!(open::that(url))
}
2022-04-24 16:00:17 +03:00
/// service mode
#[cfg(windows)]
pub mod service {
2022-07-12 19:54:47 +03:00
use super::*;
use crate::core::win_service::JsonResponse;
#[tauri::command]
pub async fn start_service() -> Result<(), String> {
wrap_err!(crate::core::Service::start_service().await)
}
#[tauri::command]
pub async fn stop_service() -> Result<(), String> {
wrap_err!(crate::core::Service::stop_service().await)
}
#[tauri::command]
pub async fn check_service() -> Result<JsonResponse, String> {
// no log
match crate::core::Service::check_service().await {
Ok(res) => Ok(res),
Err(err) => Err(err.to_string()),
}
2022-07-12 19:54:47 +03:00
}
2022-07-12 19:54:47 +03:00
#[tauri::command]
pub async fn install_service() -> Result<(), String> {
wrap_err!(crate::core::Service::install_service().await)
}
2022-07-12 19:54:47 +03:00
#[tauri::command]
pub async fn uninstall_service() -> Result<(), String> {
wrap_err!(crate::core::Service::uninstall_service().await)
}
2022-04-24 16:00:17 +03:00
}
#[cfg(not(windows))]
pub mod service {
2022-07-12 19:54:47 +03:00
use super::*;
2022-04-24 16:00:17 +03:00
2022-07-12 19:54:47 +03:00
#[tauri::command]
pub async fn start_service() -> Result<(), String> {
Ok(())
}
2022-04-24 16:00:17 +03:00
2022-07-12 19:54:47 +03:00
#[tauri::command]
pub async fn stop_service() -> Result<(), String> {
Ok(())
}
2022-04-25 11:12:04 +03:00
2022-07-12 19:54:47 +03:00
#[tauri::command]
pub async fn check_service() -> Result<(), String> {
Ok(())
}
2022-04-24 16:00:17 +03:00
2022-07-12 19:54:47 +03:00
#[tauri::command]
pub async fn install_service() -> Result<(), String> {
Ok(())
}
#[tauri::command]
pub async fn uninstall_service() -> Result<(), String> {
Ok(())
}
2022-04-24 16:00:17 +03:00
}