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

271 lines
6.8 KiB
Rust
Raw Normal View History

2022-01-07 18:29:20 +03:00
use crate::{
2022-04-18 20:41:20 +03:00
core::{ClashInfo, Core, PrfItem, PrfOption, Profiles, VergeConfig},
2022-04-19 20:44:47 +03:00
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-04-19 09:38:59 +03:00
let profiles = core.profiles.lock();
2022-02-27 20:34:25 +03:00
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 {
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(
url: String,
option: Option<PrfOption>,
2022-04-18 20:41:20 +03:00
core: State<'_, Core>,
) -> CmdResult {
let item = wrap_err!(PrfItem::from_url(&url, None, None, option).await)?;
2022-03-01 03:58:47 +03:00
2022-04-19 09:38:59 +03:00
let mut profiles = core.profiles.lock();
2022-03-01 03:58:47 +03:00
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(
item: PrfItem, // partial
file_data: Option<String>,
2022-04-18 20:41:20 +03:00
core: State<'_, Core>,
) -> CmdResult {
let item = wrap_err!(PrfItem::from(item, file_data).await)?;
2022-03-01 03:58:47 +03:00
2022-04-19 09:38:59 +03:00
let mut profiles = core.profiles.lock();
2022-03-01 03:58:47 +03:00
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-03-01 03:58:47 +03:00
index: String,
option: Option<PrfOption>,
2022-04-18 20:41:20 +03:00
core: State<'_, Core>,
) -> CmdResult {
2022-03-11 14:50:51 +03:00
let (url, opt) = {
2022-03-01 03:58:47 +03:00
// must release the lock here
2022-04-19 09:38:59 +03:00
let profiles = core.profiles.lock();
2022-03-01 03:58:47 +03:00
let item = wrap_err!(profiles.get_item(&index))?;
2022-03-11 14:50:51 +03:00
// check the profile type
if let Some(typ) = item.itype.as_ref() {
if *typ != "remote" {
ret_err!(format!("could not update the `{typ}` profile"));
}
}
2022-03-01 03:58:47 +03:00
if item.url.is_none() {
ret_err!("failed to get the item url");
2022-01-07 18:29:20 +03:00
}
2022-03-01 03:58:47 +03:00
2022-03-11 14:50:51 +03:00
(item.url.clone().unwrap(), item.option.clone())
2022-01-07 18:29:20 +03:00
};
2022-03-11 14:50:51 +03:00
let fetch_opt = PrfOption::merge(opt, option);
let item = wrap_err!(PrfItem::from_url(&url, None, None, fetch_opt).await)?;
2022-01-07 18:29:20 +03:00
2022-04-19 09:38:59 +03:00
let mut profiles = core.profiles.lock();
2022-03-01 03:58:47 +03:00
wrap_err!(profiles.update_item(index.clone(), item))?;
2022-02-18 19:09:36 +03:00
2022-03-01 03:58:47 +03:00
// reactivate the profile
if Some(index) == profiles.get_current() {
2022-04-19 09:38:59 +03:00
drop(profiles);
log_if_err!(core.activate_enhanced(false));
2022-01-07 18:29:20 +03:00
}
2022-03-01 03:58:47 +03:00
Ok(())
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-04-19 09:38:59 +03:00
let mut profiles = core.profiles.lock();
wrap_err!(profiles.put_current(index))?;
drop(profiles);
2022-04-18 20:41:20 +03:00
2022-04-19 20:44: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-04-19 09:38:59 +03:00
let mut profiles = core.profiles.lock();
profiles.put_chain(chain);
2022-03-06 09:59:25 +03:00
2022-04-19 20:44:47 +03:00
drop(profiles);
2022-03-06 09:59:25 +03:00
2022-04-19 20:44: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-04-19 09:38:59 +03:00
let mut profiles = core.profiles.lock();
profiles.put_valid(valid);
2022-04-18 20:41:20 +03:00
2022-04-19 20:44:47 +03:00
drop(profiles);
2022-04-19 20:44: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-04-19 09:38:59 +03:00
let mut profiles = core.profiles.lock();
2022-02-27 20:34:25 +03:00
if wrap_err!(profiles.delete_item(index))? {
2022-04-19 09:38:59 +03:00
drop(profiles);
2022-04-19 20:44:47 +03:00
2022-04-19 09:38:59 +03:00
log_if_err!(core.activate_enhanced(false));
2022-01-07 18:29:20 +03:00
}
2022-02-27 20:34:25 +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-04-19 09:38:59 +03:00
let mut profiles = core.profiles.lock();
2022-04-18 20:41:20 +03:00
2022-02-27 20:34:25 +03:00
wrap_err!(profiles.patch_item(index, profile))
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-04-19 09:38:59 +03:00
let profiles = core.profiles.lock();
2022-03-01 03:58:47 +03:00
let item = wrap_err!(profiles.get_item(&index))?;
2022-01-16 21:16:17 +03:00
2022-03-01 03:58: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-03-01 03:58:47 +03:00
let path = dirs::app_profiles_dir().join(file.unwrap());
2022-01-16 21:16:17 +03:00
if !path.exists() {
2022-02-27 20:34:25 +03:00
ret_err!("the file not found");
2022-01-16 21:16:17 +03:00
}
2022-04-19 20:44: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-04-19 09:38:59 +03:00
let profiles = core.profiles.lock();
2022-04-18 20:41:20 +03:00
2022-03-26 19:58:17 +03:00
let item = wrap_err!(profiles.get_item(&index))?;
let data = wrap_err!(item.read_file())?;
Ok(data)
}
/// save the profile item file data
#[tauri::command]
pub fn save_profile_file(
index: String,
file_data: Option<String>,
2022-04-18 20:41:20 +03:00
core: State<'_, Core>,
) -> CmdResult {
2022-03-26 19:58:17 +03:00
if file_data.is_none() {
return Ok(());
}
2022-04-19 09:38:59 +03:00
let profiles = core.profiles.lock();
2022-03-26 19:58:17 +03:00
let item = wrap_err!(profiles.get_item(&index))?;
wrap_err!(item.save_file(file_data.unwrap()))
}
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-04-19 09:38:59 +03:00
let clash = core.clash.lock();
2022-02-27 20:34:25 +03:00
Ok(clash.info.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]
2022-04-18 20:41:20 +03:00
pub fn patch_clash_config(payload: Mapping, core: State<'_, Core>) -> CmdResult {
wrap_err!(core.patch_clash(payload))
2022-01-07 18:29:20 +03:00
}
/// get the verge config
#[tauri::command]
2022-04-18 20:41:20 +03:00
pub fn get_verge_config(core: State<'_, Core>) -> CmdResult<VergeConfig> {
2022-04-19 09:38:59 +03:00
let verge = core.verge.lock();
2022-04-19 20:44:47 +03:00
let config = verge.config.clone();
2022-02-13 20:26:24 +03:00
Ok(config)
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-01-07 18:29:20 +03:00
payload: VergeConfig,
app_handle: tauri::AppHandle,
2022-04-18 20:41:20 +03:00
core: State<'_, Core>,
2022-01-07 18:29:20 +03:00
) -> Result<(), String> {
2022-04-19 20:44:47 +03:00
wrap_err!(core.patch_verge(payload, &app_handle))
}
2022-04-12 20:09:51 +03:00
2022-04-19 20:44:47 +03:00
/// restart the sidecar
#[tauri::command]
pub fn restart_sidecar(core: State<'_, Core>) -> CmdResult {
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-02-16 21:10:25 +03:00
api::process::kill_children();
}
2022-04-19 20:44:47 +03:00
/// get the system proxy
#[tauri::command]
pub fn get_sys_proxy() -> Result<SysProxyConfig, String> {
wrap_err!(SysProxyConfig::get_sys())
}
/// 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>> {
let sysopt = core.sysopt.lock();
wrap_err!(sysopt.get_sysproxy())
}
2022-02-15 22:21:34 +03:00
/// open app config dir
#[tauri::command]
pub fn open_app_dir() -> Result<(), String> {
2022-02-27 20:34:25 +03:00
let app_dir = dirs::app_home_dir();
2022-03-23 18:00:14 +03:00
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-02-27 20:34:25 +03:00
let log_dir = dirs::app_logs_dir();
2022-03-23 18:00:14 +03:00
wrap_err!(open::that(log_dir))
2022-02-15 22:21:34 +03:00
}