2022-01-07 18:29:20 +03:00
|
|
|
use crate::{
|
2022-09-11 15:58:55 +03:00
|
|
|
core::Core,
|
|
|
|
data::{ClashInfo, Data, PrfItem, PrfOption, Profiles, Verge},
|
|
|
|
utils::{dirs, help},
|
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-09-05 15:30:39 +03:00
|
|
|
use std::collections::{HashMap, VecDeque};
|
2022-09-11 15:58:55 +03:00
|
|
|
use sysproxy::Sysproxy;
|
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-09-11 15:58:55 +03:00
|
|
|
pub fn get_profiles() -> CmdResult<Profiles> {
|
|
|
|
let global = Data::global();
|
|
|
|
let profiles = global.profiles.lock();
|
2022-07-12 19:54:47 +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-09-11 15:58:55 +03:00
|
|
|
pub fn enhance_profiles() -> CmdResult {
|
|
|
|
let core = Core::global();
|
2022-08-10 21:55:10 +03:00
|
|
|
wrap_err!(core.activate())
|
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-09-11 15:58:55 +03:00
|
|
|
pub async fn import_profile(url: String, option: Option<PrfOption>) -> 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-09-11 15:58:55 +03:00
|
|
|
let global = Data::global();
|
|
|
|
let mut profiles = global.profiles.lock();
|
2022-07-12 19:54: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-09-11 15:58:55 +03:00
|
|
|
pub async fn create_profile(item: PrfItem, file_data: Option<String>) -> 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-09-11 15:58:55 +03:00
|
|
|
let global = Data::global();
|
|
|
|
let mut profiles = global.profiles.lock();
|
2022-07-12 19:54: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]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResult {
|
|
|
|
let core = Core::global();
|
2022-08-11 22:20:55 +03:00
|
|
|
wrap_err!(core.update_profile_item(index, option).await)
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// change the current profile
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn select_profile(index: String) -> CmdResult {
|
|
|
|
let global = Data::global();
|
|
|
|
let mut profiles = global.profiles.lock();
|
2022-07-12 19:54:47 +03:00
|
|
|
wrap_err!(profiles.put_current(index))?;
|
|
|
|
drop(profiles);
|
2022-09-11 15:58:55 +03:00
|
|
|
|
|
|
|
let core = Core::global();
|
2022-08-10 21:55:10 +03:00
|
|
|
wrap_err!(core.activate())
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 09:59:25 +03:00
|
|
|
/// change the profile chain
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn change_profile_chain(chain: Option<Vec<String>>) -> CmdResult {
|
|
|
|
let global = Data::global();
|
|
|
|
let mut profiles = global.profiles.lock();
|
2022-08-08 18:17:22 +03:00
|
|
|
wrap_err!(profiles.put_chain(chain))?;
|
2022-07-12 19:54:47 +03:00
|
|
|
drop(profiles);
|
2022-09-11 15:58:55 +03:00
|
|
|
|
|
|
|
let core = Core::global();
|
2022-08-10 21:55:10 +03:00
|
|
|
wrap_err!(core.activate())
|
2022-03-06 09:59:25 +03:00
|
|
|
}
|
|
|
|
|
2022-04-06 20:20:44 +03:00
|
|
|
/// change the profile valid fields
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn change_profile_valid(valid: Option<Vec<String>>) -> CmdResult {
|
|
|
|
let global = Data::global();
|
|
|
|
let mut profiles = global.profiles.lock();
|
2022-08-08 18:17:22 +03:00
|
|
|
wrap_err!(profiles.put_valid(valid))?;
|
2022-07-12 19:54:47 +03:00
|
|
|
drop(profiles);
|
2022-09-11 15:58:55 +03:00
|
|
|
|
|
|
|
let core = Core::global();
|
2022-08-10 21:55:10 +03:00
|
|
|
wrap_err!(core.activate())
|
2022-03-06 09:59:25 +03:00
|
|
|
}
|
|
|
|
|
2022-01-07 18:29:20 +03:00
|
|
|
/// delete profile item
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn delete_profile(index: String) -> CmdResult {
|
|
|
|
let global = Data::global();
|
|
|
|
let mut profiles = global.profiles.lock();
|
2022-07-12 19:54:47 +03:00
|
|
|
if wrap_err!(profiles.delete_item(index))? {
|
|
|
|
drop(profiles);
|
2022-09-11 15:58:55 +03:00
|
|
|
|
|
|
|
let core = Core::global();
|
2022-08-10 21:55:10 +03:00
|
|
|
log_if_err!(core.activate());
|
2022-07-12 19:54:47 +03:00
|
|
|
}
|
|
|
|
Ok(())
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// patch the profile config
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
|
|
|
|
let global = Data::global();
|
|
|
|
let mut profiles = global.profiles.lock();
|
2022-07-12 19:54:47 +03:00
|
|
|
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
|
2022-09-11 15:58:55 +03:00
|
|
|
let core = Core::global();
|
2022-07-12 19:54:47 +03:00
|
|
|
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-09-11 15:58:55 +03:00
|
|
|
pub fn view_profile(index: String) -> CmdResult {
|
|
|
|
let global = Data::global();
|
|
|
|
let profiles = global.profiles.lock();
|
2022-07-12 19:54:47 +03:00
|
|
|
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() {
|
2022-09-11 15:58:55 +03:00
|
|
|
ret_err!("file is null");
|
2022-07-12 19:54:47 +03:00
|
|
|
}
|
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() {
|
2022-09-11 15:58:55 +03:00
|
|
|
ret_err!("file not found");
|
2022-07-12 19:54:47 +03:00
|
|
|
}
|
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-09-11 15:58:55 +03:00
|
|
|
pub fn read_profile_file(index: String) -> CmdResult<String> {
|
|
|
|
let global = Data::global();
|
|
|
|
let profiles = global.profiles.lock();
|
2022-07-12 19:54:47 +03:00
|
|
|
let item = wrap_err!(profiles.get_item(&index))?;
|
|
|
|
let data = wrap_err!(item.read_file())?;
|
|
|
|
Ok(data)
|
2022-03-26 19:58:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// save the profile item file data
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn save_profile_file(index: String, file_data: Option<String>) -> 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-09-11 15:58:55 +03:00
|
|
|
let global = Data::global();
|
|
|
|
let profiles = global.profiles.lock();
|
2022-07-12 19:54:47 +03:00
|
|
|
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-09-11 15:58:55 +03:00
|
|
|
pub fn get_clash_info() -> CmdResult<ClashInfo> {
|
|
|
|
let global = Data::global();
|
|
|
|
let clash = global.clash.lock();
|
2022-07-12 19:54:47 +03:00
|
|
|
Ok(clash.info.clone())
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
2022-08-11 22:20:55 +03:00
|
|
|
/// get the runtime clash config mapping
|
2022-07-24 20:20:13 +03:00
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn get_runtime_config() -> CmdResult<Option<Mapping>> {
|
|
|
|
let core = Core::global();
|
2022-08-11 22:20:55 +03:00
|
|
|
let rt = core.runtime.lock();
|
|
|
|
Ok(rt.config.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get the runtime clash config yaml string
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn get_runtime_yaml() -> CmdResult<Option<String>> {
|
|
|
|
let core = Core::global();
|
2022-08-11 22:20:55 +03:00
|
|
|
let rt = core.runtime.lock();
|
|
|
|
Ok(rt.config_yaml.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get the runtime config exists keys
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn get_runtime_exists() -> CmdResult<Vec<String>> {
|
|
|
|
let core = Core::global();
|
2022-08-11 22:20:55 +03:00
|
|
|
let rt = core.runtime.lock();
|
|
|
|
Ok(rt.exists_keys.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get the runtime enhanced chain log
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn get_runtime_logs() -> CmdResult<HashMap<String, Vec<(String, String)>>> {
|
|
|
|
let core = Core::global();
|
2022-08-11 22:20:55 +03:00
|
|
|
let rt = core.runtime.lock();
|
|
|
|
Ok(rt.chain_logs.clone())
|
2022-07-24 20:20:13 +03:00
|
|
|
}
|
|
|
|
|
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-09-11 15:58:55 +03:00
|
|
|
pub fn patch_clash_config(payload: Mapping) -> CmdResult {
|
|
|
|
let core = Core::global();
|
|
|
|
wrap_err!(core.patch_clash(payload))
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// get the verge config
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn get_verge_config() -> CmdResult<Verge> {
|
|
|
|
let global = Data::global();
|
|
|
|
let verge = global.verge.lock();
|
2022-07-12 19:54:47 +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-09-11 15:58:55 +03:00
|
|
|
pub fn patch_verge_config(payload: Verge) -> CmdResult {
|
|
|
|
let core = Core::global();
|
|
|
|
wrap_err!(core.patch_verge(payload))
|
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]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn change_clash_core(clash_core: Option<String>) -> CmdResult {
|
|
|
|
let core = Core::global();
|
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]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn restart_sidecar() -> CmdResult {
|
|
|
|
let core = Core::global();
|
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-09-11 15:58:55 +03:00
|
|
|
tauri::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]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn get_sys_proxy() -> CmdResult<Mapping> {
|
|
|
|
let current = wrap_err!(Sysproxy::get_system_proxy())?;
|
2022-04-19 20:44:47 +03:00
|
|
|
|
2022-09-11 15:58:55 +03: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());
|
|
|
|
|
|
|
|
Ok(map)
|
2022-04-19 20:44:47 +03:00
|
|
|
}
|
|
|
|
|
2022-09-05 15:30:39 +03:00
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn get_clash_logs() -> CmdResult<VecDeque<String>> {
|
|
|
|
let core = Core::global();
|
2022-09-05 15:30:39 +03:00
|
|
|
let service = core.service.lock();
|
|
|
|
Ok(service.get_logs())
|
|
|
|
}
|
|
|
|
|
2022-02-15 22:21:34 +03:00
|
|
|
/// open app config dir
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn open_app_dir() -> CmdResult<()> {
|
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]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn open_logs_dir() -> CmdResult<()> {
|
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]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub fn open_web_url(url: String) -> CmdResult<()> {
|
2022-08-06 16:56:54 +03:00
|
|
|
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]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn start_service() -> CmdResult<()> {
|
2022-07-12 19:54:47 +03:00
|
|
|
wrap_err!(crate::core::Service::start_service().await)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn stop_service() -> CmdResult<()> {
|
2022-07-12 19:54:47 +03:00
|
|
|
wrap_err!(crate::core::Service::stop_service().await)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn check_service() -> CmdResult<JsonResponse> {
|
2022-07-12 19:54:47 +03:00
|
|
|
// no log
|
|
|
|
match crate::core::Service::check_service().await {
|
|
|
|
Ok(res) => Ok(res),
|
|
|
|
Err(err) => Err(err.to_string()),
|
2022-07-12 19:43:27 +03:00
|
|
|
}
|
2022-07-12 19:54:47 +03:00
|
|
|
}
|
2022-07-12 19:43:27 +03:00
|
|
|
|
2022-07-12 19:54:47 +03:00
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn install_service() -> CmdResult<()> {
|
2022-07-12 19:54:47 +03:00
|
|
|
wrap_err!(crate::core::Service::install_service().await)
|
|
|
|
}
|
2022-07-12 19:43:27 +03:00
|
|
|
|
2022-07-12 19:54:47 +03:00
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn uninstall_service() -> CmdResult<()> {
|
2022-07-12 19:54:47 +03:00
|
|
|
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]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn start_service() -> CmdResult<()> {
|
2022-07-12 19:54:47 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn stop_service() -> CmdResult<()> {
|
2022-07-12 19:54:47 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn check_service() -> CmdResult<()> {
|
2022-07-12 19:54:47 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn install_service() -> CmdResult<()> {
|
2022-07-12 19:54:47 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
#[tauri::command]
|
2022-09-11 15:58:55 +03:00
|
|
|
pub async fn uninstall_service() -> CmdResult<()> {
|
2022-07-12 19:54:47 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-24 16:00:17 +03:00
|
|
|
}
|