2022-01-07 18:29:20 +03:00
|
|
|
use crate::{
|
2022-01-20 21:57:15 +03:00
|
|
|
core::{ClashInfo, ProfileItem, Profiles, VergeConfig},
|
2022-01-07 18:29:20 +03:00
|
|
|
states::{ClashState, ProfilesState, VergeState},
|
2022-01-20 21:31:44 +03:00
|
|
|
utils::{dirs::app_home_dir, fetch::fetch_profile, sysopt::SysProxyConfig},
|
2022-01-07 18:29:20 +03:00
|
|
|
};
|
|
|
|
use serde_yaml::Mapping;
|
2022-01-16 21:16:17 +03:00
|
|
|
use std::process::Command;
|
2022-01-15 22:09:36 +03:00
|
|
|
use tauri::State;
|
2022-01-07 18:29:20 +03:00
|
|
|
|
|
|
|
/// get all profiles from `profiles.yaml`
|
|
|
|
/// do not acquire the lock of ProfileLock
|
|
|
|
#[tauri::command]
|
2022-01-20 21:57:15 +03:00
|
|
|
pub fn get_profiles(profiles_state: State<'_, ProfilesState>) -> Result<Profiles, String> {
|
|
|
|
match profiles_state.0.lock() {
|
2022-01-07 18:29:20 +03:00
|
|
|
Ok(profiles) => Ok(profiles.clone()),
|
|
|
|
Err(_) => Err("failed to get profiles lock".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// synchronize data irregularly
|
|
|
|
#[tauri::command]
|
2022-01-20 21:57:15 +03:00
|
|
|
pub fn sync_profiles(profiles_state: State<'_, ProfilesState>) -> Result<(), String> {
|
|
|
|
match profiles_state.0.lock() {
|
2022-01-07 18:29:20 +03:00
|
|
|
Ok(mut profiles) => profiles.sync_file(),
|
|
|
|
Err(_) => Err("failed to get profiles lock".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
|
|
|
match fetch_profile(&url, with_proxy).await {
|
2022-01-07 18:29:20 +03:00
|
|
|
Some(result) => {
|
2022-01-20 21:57:15 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
2022-01-07 18:29:20 +03:00
|
|
|
profiles.import_from_url(url, result)
|
|
|
|
}
|
|
|
|
None => Err(format!("failed to fetch profile from `{}`", url)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the profile
|
|
|
|
#[tauri::command]
|
|
|
|
pub async fn update_profile(
|
|
|
|
index: usize,
|
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> {
|
|
|
|
// maybe we can get the url from the web app directly
|
2022-01-20 21:57:15 +03:00
|
|
|
let url = match profiles_state.0.lock() {
|
2022-01-07 18:29:20 +03:00
|
|
|
Ok(mut profile) => {
|
|
|
|
let items = profile.items.take().unwrap_or(vec![]);
|
|
|
|
if index >= items.len() {
|
|
|
|
return Err("the index out of bound".into());
|
|
|
|
}
|
|
|
|
let url = match &items[index].url {
|
|
|
|
Some(u) => u.clone(),
|
|
|
|
None => return Err("failed to update profile for `invalid url`".into()),
|
|
|
|
};
|
|
|
|
profile.items = Some(items);
|
|
|
|
url
|
|
|
|
}
|
|
|
|
Err(_) => return Err("failed to get profiles lock".into()),
|
|
|
|
};
|
|
|
|
|
2022-01-16 17:57:42 +03:00
|
|
|
match fetch_profile(&url, with_proxy).await {
|
2022-01-20 21:57:15 +03:00
|
|
|
Some(result) => match profiles_state.0.lock() {
|
2022-01-07 18:29:20 +03:00
|
|
|
Ok(mut profiles) => {
|
|
|
|
profiles.update_item(index, result)?;
|
|
|
|
|
|
|
|
// reactivate the profile
|
|
|
|
let current = profiles.current.clone().unwrap_or(0);
|
|
|
|
if current == index {
|
2022-01-20 21:57:15 +03:00
|
|
|
let clash = clash_state.0.lock().unwrap();
|
2022-01-20 21:31:44 +03:00
|
|
|
profiles.activate(&clash)
|
2022-01-07 18:29:20 +03:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => Err("failed to get profiles lock".into()),
|
|
|
|
},
|
|
|
|
None => Err(format!("failed to fetch profile from `{}`", url)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// change the current profile
|
|
|
|
#[tauri::command]
|
|
|
|
pub fn select_profile(
|
|
|
|
index: usize,
|
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-01-07 18:29:20 +03:00
|
|
|
|
|
|
|
match profiles.put_current(index) {
|
|
|
|
Ok(()) => {
|
2022-01-20 21:57:15 +03:00
|
|
|
let clash = clash_state.0.lock().unwrap();
|
2022-01-20 21:31:44 +03:00
|
|
|
profiles.activate(&clash)
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// delete profile item
|
|
|
|
#[tauri::command]
|
2022-01-08 09:21:12 +03:00
|
|
|
pub fn delete_profile(
|
|
|
|
index: usize,
|
|
|
|
clash_state: State<'_, ClashState>,
|
|
|
|
profiles_state: State<'_, ProfilesState>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
|
|
|
match profiles.delete_item(index) {
|
|
|
|
Ok(change) => match change {
|
|
|
|
true => {
|
|
|
|
let clash = clash_state.0.lock().unwrap();
|
2022-01-20 21:31:44 +03:00
|
|
|
profiles.activate(&clash)
|
2022-01-08 09:21:12 +03:00
|
|
|
}
|
|
|
|
false => Ok(()),
|
|
|
|
},
|
|
|
|
Err(err) => Err(err),
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// patch the profile config
|
|
|
|
#[tauri::command]
|
|
|
|
pub fn patch_profile(
|
|
|
|
index: usize,
|
|
|
|
profile: ProfileItem,
|
2022-01-20 21:57:15 +03:00
|
|
|
profiles_state: State<'_, ProfilesState>,
|
2022-01-07 18:29:20 +03:00
|
|
|
) -> Result<(), String> {
|
2022-01-20 21:57:15 +03:00
|
|
|
match profiles_state.0.lock() {
|
2022-01-07 18:29:20 +03:00
|
|
|
Ok(mut profiles) => profiles.patch_item(index, profile),
|
|
|
|
Err(_) => Err("can not get profiles lock".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 21:16:17 +03:00
|
|
|
/// run vscode command to edit the profile
|
|
|
|
#[tauri::command]
|
2022-01-19 18:58:34 +03:00
|
|
|
pub fn view_profile(index: usize, profiles_state: State<'_, ProfilesState>) -> Result<(), String> {
|
2022-01-16 21:16:17 +03:00
|
|
|
let mut profiles = profiles_state.0.lock().unwrap();
|
|
|
|
let items = profiles.items.take().unwrap_or(vec![]);
|
|
|
|
|
|
|
|
if index >= items.len() {
|
|
|
|
profiles.items = Some(items);
|
|
|
|
return Err("the index out of bound".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let file = items[index].file.clone().unwrap_or("".into());
|
|
|
|
profiles.items = Some(items);
|
|
|
|
|
|
|
|
let path = app_home_dir().join("profiles").join(file);
|
|
|
|
if !path.exists() {
|
|
|
|
return Err("failed to open the file".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
match which::which("code") {
|
|
|
|
Ok(code) => match Command::new(code).arg(path).status() {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(_) => Err("failed to open file by VScode".into()),
|
|
|
|
},
|
|
|
|
Err(_) => Err("please install VScode for edit".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-20 21:31:44 +03:00
|
|
|
match clash.restart_sidecar(&mut profiles) {
|
|
|
|
Ok(_) => Ok(()),
|
2022-01-10 17:08:18 +03:00
|
|
|
Err(err) => {
|
|
|
|
log::error!("{}", err);
|
|
|
|
Err(err)
|
|
|
|
}
|
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> {
|
|
|
|
match clash_state.0.lock() {
|
2022-01-20 21:31:44 +03:00
|
|
|
Ok(clash) => Ok(clash.info.clone()),
|
2022-01-07 18:29:20 +03:00
|
|
|
Err(_) => Err("failed to get clash lock".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-01-20 21:50:13 +03:00
|
|
|
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> {
|
|
|
|
match SysProxyConfig::get_sys() {
|
|
|
|
Ok(value) => Ok(value),
|
|
|
|
Err(err) => Err(err.to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
|
|
|
match verge_state.0.lock() {
|
|
|
|
Ok(verge) => Ok(verge.cur_sysproxy.clone()),
|
|
|
|
Err(_) => Err("failed to get verge lock".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get the verge config
|
|
|
|
#[tauri::command]
|
|
|
|
pub fn get_verge_config(verge_state: State<'_, VergeState>) -> Result<VergeConfig, String> {
|
|
|
|
match verge_state.0.lock() {
|
|
|
|
Ok(arc) => Ok(arc.config.clone()),
|
|
|
|
Err(_) => Err("failed to get verge lock".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// patch the verge config
|
|
|
|
/// this command only save the config and not responsible for other things
|
|
|
|
#[tauri::command]
|
|
|
|
pub async fn patch_verge_config(
|
|
|
|
payload: VergeConfig,
|
|
|
|
verge_state: State<'_, VergeState>,
|
|
|
|
) -> Result<(), String> {
|
|
|
|
let mut verge = verge_state.0.lock().unwrap();
|
2022-01-10 21:24:43 +03:00
|
|
|
verge.patch_config(payload)
|
2022-01-07 18:29:20 +03:00
|
|
|
}
|