2022-01-07 18:29:20 +03:00
|
|
|
use crate::{
|
2022-04-20 06:17:54 +03:00
|
|
|
core::{ClashInfo, Core, PrfItem, PrfOption, Profiles, Verge},
|
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,
|
2022-03-09 21:03:55 +03:00
|
|
|
option: Option<PrfOption>,
|
2022-04-18 20:41:20 +03:00
|
|
|
core: State<'_, Core>,
|
|
|
|
) -> CmdResult {
|
2022-03-09 21:03:55 +03:00
|
|
|
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
|
2022-03-19 14:21:55 +03:00
|
|
|
file_data: Option<String>,
|
2022-04-18 20:41:20 +03:00
|
|
|
core: State<'_, Core>,
|
|
|
|
) -> CmdResult {
|
2022-03-19 14:21:55 +03:00
|
|
|
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,
|
2022-03-09 21:03:55 +03:00
|
|
|
option: Option<PrfOption>,
|
2022-04-18 20:41:20 +03:00
|
|
|
core: State<'_, Core>,
|
|
|
|
) -> CmdResult {
|
2022-04-20 15:37:16 +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-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
|
|
|
}
|
|
|
|
|
2022-04-06 20:20:44 +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();
|
2022-04-06 20:20:44 +03:00
|
|
|
profiles.put_valid(valid);
|
2022-04-18 20:41:20 +03:00
|
|
|
|
2022-04-19 20:44:47 +03:00
|
|
|
drop(profiles);
|
2022-04-06 20:20:44 +03:00
|
|
|
|
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-21 09:26:41 +03:00
|
|
|
wrap_err!(profiles.patch_item(index, profile))?;
|
|
|
|
drop(profiles);
|
2022-04-18 20:41:20 +03:00
|
|
|
|
2022-04-21 09:26:41 +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-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-20 06:17:54 +03:00
|
|
|
pub fn get_verge_config(core: State<'_, Core>) -> CmdResult<Verge> {
|
2022-04-19 09:38:59 +03:00
|
|
|
let verge = core.verge.lock();
|
2022-04-20 15:37:16 +03:00
|
|
|
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-04-20 06:17:54 +03:00
|
|
|
payload: Verge,
|
2022-03-19 20:36:43 +03:00
|
|
|
app_handle: tauri::AppHandle,
|
2022-04-18 20:41:20 +03:00
|
|
|
core: State<'_, Core>,
|
2022-05-16 20:59:49 +03:00
|
|
|
) -> CmdResult {
|
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-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 {
|
|
|
|
wrap_err!(core.change_core(clash_core))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2022-04-24 16:00:17 +03:00
|
|
|
|
|
|
|
/// service mode
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub mod service {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:12:04 +03:00
|
|
|
#[tauri::command]
|
|
|
|
pub async fn stop_service() -> Result<(), String> {
|
|
|
|
wrap_err!(crate::core::Service::stop_service().await)
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:00:17 +03:00
|
|
|
#[tauri::command]
|
|
|
|
pub async fn check_service() -> Result<JsonResponse, String> {
|
2022-04-27 10:46:44 +03:00
|
|
|
// no log
|
|
|
|
match crate::core::Service::check_service().await {
|
|
|
|
Ok(res) => Ok(res),
|
|
|
|
Err(err) => Err(err.to_string()),
|
|
|
|
}
|
2022-04-24 16:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
pub async fn install_service() -> Result<(), String> {
|
2022-04-27 10:46:44 +03:00
|
|
|
wrap_err!(crate::core::Service::install_service().await)
|
2022-04-24 16:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
pub async fn uninstall_service() -> Result<(), String> {
|
|
|
|
wrap_err!(crate::core::Service::uninstall_service().await)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
pub mod service {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
pub async fn start_service() -> Result<(), String> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:12:04 +03:00
|
|
|
#[tauri::command]
|
|
|
|
pub async fn stop_service() -> Result<(), String> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:00:17 +03:00
|
|
|
#[tauri::command]
|
|
|
|
pub async fn check_service() -> Result<(), String> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
pub async fn install_service() -> Result<(), String> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
#[tauri::command]
|
|
|
|
pub async fn uninstall_service() -> Result<(), String> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|