2022-01-07 18:29:20 +03:00
|
|
|
use crate::{
|
2022-03-01 03:58:47 +03:00
|
|
|
core::{ClashInfo, PrfItem, Profiles, VergeConfig},
|
|
|
|
ret_err,
|
2022-01-07 18:29:20 +03:00
|
|
|
states::{ClashState, ProfilesState, VergeState},
|
2022-03-01 03:58:47 +03:00
|
|
|
utils::{dirs, sysopt::SysProxyConfig},
|
|
|
|
wrap_err,
|
2022-01-07 18:29:20 +03:00
|
|
|
};
|
2022-02-27 20:34:25 +03:00
|
|
|
use anyhow::Result;
|
2022-01-07 18:29:20 +03:00
|
|
|
use serde_yaml::Mapping;
|
2022-02-24 17:46:12 +03:00
|
|
|
use std::{path::PathBuf, process::Command};
|
2022-03-06 09:59:25 +03:00
|
|
|
use tauri::{api, Manager, State};
|
2022-01-07 18:29:20 +03:00
|
|
|
|
|
|
|
/// get all profiles from `profiles.yaml`
|
|
|
|
#[tauri::command]
|
2022-03-01 03:58:47 +03:00
|
|
|
pub fn get_profiles<'a>(profiles_state: State<'_, ProfilesState>) -> Result<Profiles, String> {
|
2022-02-27 20:34:25 +03:00
|
|
|
let profiles = profiles_state.0.lock().unwrap();
|
|
|
|
Ok(profiles.clone())
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// synchronize data irregularly
|
|
|
|
#[tauri::command]
|
2022-01-20 21:57:15 +03:00
|
|
|
pub fn sync_profiles(profiles_state: State<'_, ProfilesState>) -> Result<(), String> {
|
2022-02-27 20:34:25 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
|
|
|
wrap_err!(profiles.sync_file())
|
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,
|
|
|
|
with_proxy: bool,
|
2022-01-20 21:57:15 +03:00
|
|
|
profiles_state: State<'_, ProfilesState>,
|
2022-01-16 17:57:42 +03:00
|
|
|
) -> Result<(), String> {
|
2022-03-05 14:04:20 +03:00
|
|
|
let item = wrap_err!(PrfItem::from_url(&url, None, None, with_proxy).await)?;
|
2022-03-01 03:58:47 +03:00
|
|
|
|
2022-02-18 19:09:36 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
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-02-07 12:26:05 +03:00
|
|
|
profiles_state: State<'_, ProfilesState>,
|
|
|
|
) -> Result<(), String> {
|
2022-03-05 14:04:20 +03:00
|
|
|
let item = wrap_err!(PrfItem::from(item).await)?;
|
2022-02-07 12:26:05 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
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-01-16 17:57:42 +03:00
|
|
|
with_proxy: bool,
|
2022-01-20 21:57:15 +03:00
|
|
|
clash_state: State<'_, ClashState>,
|
|
|
|
profiles_state: State<'_, ProfilesState>,
|
2022-01-07 18:29:20 +03:00
|
|
|
) -> Result<(), String> {
|
2022-03-01 03:58:47 +03:00
|
|
|
let url = {
|
|
|
|
// must release the lock here
|
|
|
|
let profiles = profiles_state.0.lock().unwrap();
|
|
|
|
let item = wrap_err!(profiles.get_item(&index))?;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
item.url.clone().unwrap()
|
2022-01-07 18:29:20 +03:00
|
|
|
};
|
|
|
|
|
2022-03-05 14:04:20 +03:00
|
|
|
let item = wrap_err!(PrfItem::from_url(&url, None, None, with_proxy).await)?;
|
2022-01-07 18:29:20 +03:00
|
|
|
|
2022-03-01 03:58:47 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
|
|
|
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() {
|
|
|
|
let clash = clash_state.0.lock().unwrap();
|
|
|
|
wrap_err!(clash.activate(&profiles))?;
|
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]
|
|
|
|
pub fn select_profile(
|
2022-03-01 03:58:47 +03:00
|
|
|
index: String,
|
2022-01-20 21:57:15 +03:00
|
|
|
clash_state: State<'_, ClashState>,
|
|
|
|
profiles_state: State<'_, ProfilesState>,
|
2022-01-07 18:29:20 +03:00
|
|
|
) -> Result<(), String> {
|
2022-01-20 21:57:15 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
2022-02-27 20:34:25 +03:00
|
|
|
wrap_err!(profiles.put_current(index))?;
|
2022-01-07 18:29:20 +03:00
|
|
|
|
2022-02-27 20:34:25 +03:00
|
|
|
let clash = clash_state.0.lock().unwrap();
|
2022-03-01 03:58:47 +03:00
|
|
|
wrap_err!(clash.activate(&profiles))
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
2022-03-06 09:59:25 +03:00
|
|
|
/// change the profile chain
|
|
|
|
#[tauri::command]
|
|
|
|
pub fn change_profile_chain(
|
|
|
|
chain: Option<Vec<String>>,
|
|
|
|
app_handle: tauri::AppHandle,
|
|
|
|
clash_state: State<'_, ClashState>,
|
|
|
|
profiles_state: State<'_, ProfilesState>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
let clash = clash_state.0.lock().unwrap();
|
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
|
|
|
|
|
|
|
profiles.put_chain(chain);
|
|
|
|
|
|
|
|
app_handle
|
|
|
|
.get_window("main")
|
|
|
|
.map(|win| wrap_err!(clash.activate_enhanced(&profiles, win, false)));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// manually exec enhanced profile
|
|
|
|
#[tauri::command]
|
|
|
|
pub fn enhance_profiles(
|
|
|
|
app_handle: tauri::AppHandle,
|
|
|
|
clash_state: State<'_, ClashState>,
|
|
|
|
profiles_state: State<'_, ProfilesState>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
let clash = clash_state.0.lock().unwrap();
|
|
|
|
let profiles = profiles_state.0.lock().unwrap();
|
|
|
|
|
|
|
|
app_handle
|
|
|
|
.get_window("main")
|
|
|
|
.map(|win| wrap_err!(clash.activate_enhanced(&profiles, win, false)));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-07 18:29:20 +03:00
|
|
|
/// delete profile item
|
|
|
|
#[tauri::command]
|
2022-01-08 09:21:12 +03:00
|
|
|
pub fn delete_profile(
|
2022-03-01 03:58:47 +03:00
|
|
|
index: String,
|
2022-01-08 09:21:12 +03:00
|
|
|
clash_state: State<'_, ClashState>,
|
|
|
|
profiles_state: State<'_, ProfilesState>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
2022-02-27 20:34:25 +03:00
|
|
|
|
|
|
|
if wrap_err!(profiles.delete_item(index))? {
|
|
|
|
let clash = clash_state.0.lock().unwrap();
|
2022-03-01 03:58:47 +03:00
|
|
|
wrap_err!(clash.activate(&profiles))?;
|
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]
|
|
|
|
pub fn patch_profile(
|
2022-03-01 03:58:47 +03:00
|
|
|
index: String,
|
|
|
|
profile: PrfItem,
|
2022-01-20 21:57:15 +03:00
|
|
|
profiles_state: State<'_, ProfilesState>,
|
2022-01-07 18:29:20 +03:00
|
|
|
) -> Result<(), String> {
|
2022-02-27 20:34:25 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
|
|
|
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-03-01 03:58:47 +03:00
|
|
|
pub fn view_profile(index: String, profiles_state: State<'_, ProfilesState>) -> Result<(), String> {
|
|
|
|
let profiles = profiles_state.0.lock().unwrap();
|
|
|
|
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-02-07 11:45:20 +03:00
|
|
|
// use vscode first
|
|
|
|
if let Ok(code) = which::which("code") {
|
2022-02-27 20:34:25 +03:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
{
|
|
|
|
use std::os::windows::process::CommandExt;
|
|
|
|
|
|
|
|
return match Command::new(code)
|
|
|
|
.creation_flags(0x08000000)
|
|
|
|
.arg(path)
|
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err("failed to open file by VScode".into()),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
2022-02-15 21:43:52 +03:00
|
|
|
return match Command::new(code).arg(path).spawn() {
|
2022-01-16 21:16:17 +03:00
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err("failed to open file by VScode".into()),
|
2022-02-07 11:45:20 +03:00
|
|
|
};
|
2022-01-16 21:16:17 +03:00
|
|
|
}
|
2022-02-07 11:45:20 +03:00
|
|
|
|
2022-02-24 17:46:12 +03:00
|
|
|
open_path_cmd(path, "failed to open file by `open`")
|
2022-01-16 21:16:17 +03:00
|
|
|
}
|
|
|
|
|
2022-01-07 18:29:20 +03:00
|
|
|
/// restart the sidecar
|
|
|
|
#[tauri::command]
|
2022-01-10 17:08:18 +03:00
|
|
|
pub fn restart_sidecar(
|
|
|
|
clash_state: State<'_, ClashState>,
|
|
|
|
profiles_state: State<'_, ProfilesState>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
let mut clash = clash_state.0.lock().unwrap();
|
2022-01-20 21:31:44 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
2022-01-10 17:08:18 +03:00
|
|
|
|
2022-02-27 20:34:25 +03:00
|
|
|
wrap_err!(clash.restart_sidecar(&mut profiles))
|
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]
|
|
|
|
pub fn get_clash_info(clash_state: State<'_, ClashState>) -> Result<ClashInfo, String> {
|
2022-02-27 20:34:25 +03:00
|
|
|
let clash = clash_state.0.lock().unwrap();
|
|
|
|
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-01-20 21:31:44 +03:00
|
|
|
pub fn patch_clash_config(
|
|
|
|
payload: Mapping,
|
|
|
|
clash_state: State<'_, ClashState>,
|
2022-01-20 21:50:13 +03:00
|
|
|
verge_state: State<'_, VergeState>,
|
2022-01-20 21:31:44 +03:00
|
|
|
profiles_state: State<'_, ProfilesState>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
let mut clash = clash_state.0.lock().unwrap();
|
2022-01-20 21:50:13 +03:00
|
|
|
let mut verge = verge_state.0.lock().unwrap();
|
2022-01-20 21:31:44 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
2022-02-27 20:34:25 +03:00
|
|
|
wrap_err!(clash.patch_config(payload, &mut verge, &mut profiles))
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// get the system proxy
|
|
|
|
#[tauri::command]
|
|
|
|
pub fn get_sys_proxy() -> Result<SysProxyConfig, String> {
|
2022-02-27 20:34:25 +03:00
|
|
|
wrap_err!(SysProxyConfig::get_sys())
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 21:24:43 +03:00
|
|
|
/// get the current proxy config
|
|
|
|
/// which may not the same as system proxy
|
2022-01-07 18:29:20 +03:00
|
|
|
#[tauri::command]
|
|
|
|
pub fn get_cur_proxy(verge_state: State<'_, VergeState>) -> Result<Option<SysProxyConfig>, String> {
|
2022-02-27 20:34:25 +03:00
|
|
|
let verge = verge_state.0.lock().unwrap();
|
|
|
|
Ok(verge.cur_sysproxy.clone())
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// get the verge config
|
|
|
|
#[tauri::command]
|
|
|
|
pub fn get_verge_config(verge_state: State<'_, VergeState>) -> Result<VergeConfig, String> {
|
2022-02-13 20:26:24 +03:00
|
|
|
let verge = verge_state.0.lock().unwrap();
|
|
|
|
let mut config = verge.config.clone();
|
|
|
|
|
|
|
|
if config.system_proxy_bypass.is_none() && verge.cur_sysproxy.is_some() {
|
|
|
|
config.system_proxy_bypass = Some(verge.cur_sysproxy.clone().unwrap().bypass)
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
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,
|
2022-02-24 21:09:39 +03:00
|
|
|
clash_state: State<'_, ClashState>,
|
2022-01-07 18:29:20 +03:00
|
|
|
verge_state: State<'_, VergeState>,
|
2022-02-24 21:09:39 +03:00
|
|
|
profiles_state: State<'_, ProfilesState>,
|
2022-01-07 18:29:20 +03:00
|
|
|
) -> Result<(), String> {
|
2022-02-24 21:09:39 +03:00
|
|
|
let tun_mode = payload.enable_tun_mode.clone();
|
|
|
|
|
2022-01-07 18:29:20 +03:00
|
|
|
let mut verge = verge_state.0.lock().unwrap();
|
2022-02-27 20:34:25 +03:00
|
|
|
wrap_err!(verge.patch_config(payload))?;
|
2022-02-24 21:09:39 +03:00
|
|
|
|
|
|
|
// change tun mode
|
|
|
|
if tun_mode.is_some() {
|
|
|
|
let mut clash = clash_state.0.lock().unwrap();
|
|
|
|
let profiles = profiles_state.0.lock().unwrap();
|
|
|
|
|
2022-02-27 20:34:25 +03:00
|
|
|
wrap_err!(clash.tun_mode(tun_mode.unwrap()))?;
|
2022-02-24 21:09:39 +03:00
|
|
|
clash.update_config();
|
2022-03-01 03:58:47 +03:00
|
|
|
wrap_err!(clash.activate(&profiles))?;
|
2022-02-24 21:09:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
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]
|
|
|
|
pub fn kill_sidecars() {
|
|
|
|
api::process::kill_children();
|
|
|
|
}
|
|
|
|
|
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-02-24 17:46:12 +03:00
|
|
|
open_path_cmd(app_dir, "failed to open 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-02-24 17:46:12 +03:00
|
|
|
open_path_cmd(log_dir, "failed to open logs dir")
|
2022-02-15 22:21:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// get open/explorer command
|
2022-02-24 17:46:12 +03:00
|
|
|
fn open_path_cmd(dir: PathBuf, err_str: &str) -> Result<(), String> {
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
{
|
|
|
|
use std::os::windows::process::CommandExt;
|
|
|
|
|
|
|
|
match Command::new("explorer")
|
|
|
|
.creation_flags(0x08000000)
|
|
|
|
.arg(dir)
|
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(err_str.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
match Command::new("open").arg(dir).spawn() {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err(err_str.into()),
|
|
|
|
}
|
2022-02-15 22:21:34 +03:00
|
|
|
}
|