feat: add serval commands
This commit is contained in:
parent
0d8114c9f4
commit
13ceb1e445
@ -1,10 +1,16 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
events::{emit::ClashInfoPayload, state::ClashInfoState},
|
config::VergeConfig,
|
||||||
|
events::{
|
||||||
|
emit::ClashInfoPayload,
|
||||||
|
state::{ClashInfoState, VergeConfLock},
|
||||||
|
},
|
||||||
utils::{
|
utils::{
|
||||||
clash::run_clash_bin,
|
clash::run_clash_bin,
|
||||||
sysopt::{set_proxy_config, SysProxyConfig},
|
config::{read_clash, save_clash, save_verge},
|
||||||
|
sysopt::{get_proxy_config, set_proxy_config, SysProxyConfig},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use serde_yaml::Mapping;
|
||||||
use tauri::{api::process::kill_children, AppHandle, State};
|
use tauri::{api::process::kill_children, AppHandle, State};
|
||||||
|
|
||||||
/// restart the sidecar
|
/// restart the sidecar
|
||||||
@ -19,6 +25,7 @@ pub fn restart_sidecar(app_handle: AppHandle, clash_info: State<'_, ClashInfoSta
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// get the clash core info from the state
|
/// get the clash core info from the state
|
||||||
|
/// the caller can also get the infomation by clash's api
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn get_clash_info(clash_info: State<'_, ClashInfoState>) -> Result<ClashInfoPayload, String> {
|
pub fn get_clash_info(clash_info: State<'_, ClashInfoState>) -> Result<ClashInfoPayload, String> {
|
||||||
match clash_info.0.lock() {
|
match clash_info.0.lock() {
|
||||||
@ -27,6 +34,22 @@ pub fn get_clash_info(clash_info: State<'_, ClashInfoState>) -> Result<ClashInfo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// update the clash core config
|
||||||
|
/// after putting the change to the clash core
|
||||||
|
/// then we should save the latest config
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn patch_clash_config(payload: Mapping) -> Result<(), String> {
|
||||||
|
let mut config = read_clash();
|
||||||
|
for (key, value) in payload.iter() {
|
||||||
|
if config.contains_key(key) {
|
||||||
|
config[key] = value.clone();
|
||||||
|
} else {
|
||||||
|
config.insert(key.clone(), value.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
save_clash(&config)
|
||||||
|
}
|
||||||
|
|
||||||
/// set the system proxy
|
/// set the system proxy
|
||||||
/// Tips: only support windows now
|
/// Tips: only support windows now
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
@ -68,3 +91,50 @@ pub fn set_sys_proxy(enable: bool, clash_info: State<'_, ClashInfoState>) -> Res
|
|||||||
Err(_) => Err(format!("can not set proxy")),
|
Err(_) => Err(format!("can not set proxy")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// get the system proxy
|
||||||
|
/// Tips: only support windows now
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn get_sys_proxy() -> Result<SysProxyConfig, String> {
|
||||||
|
match get_proxy_config() {
|
||||||
|
Ok(value) => Ok(value),
|
||||||
|
Err(err) => Err(err.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// get the verge config
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn get_verge_config(verge_lock: State<'_, VergeConfLock>) -> Result<VergeConfig, String> {
|
||||||
|
match verge_lock.0.lock() {
|
||||||
|
Ok(arc) => Ok(arc.clone()),
|
||||||
|
Err(_) => Err(format!("can not get the lock")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// patch the verge config
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn patch_verge_config(
|
||||||
|
payload: VergeConfig,
|
||||||
|
verge_lock: State<'_, VergeConfLock>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let mut verge = match verge_lock.0.lock() {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(_) => return Err(format!("can not get the lock")),
|
||||||
|
};
|
||||||
|
|
||||||
|
if payload.theme_mode.is_some() {
|
||||||
|
verge.theme_mode = payload.theme_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo
|
||||||
|
if payload.enable_self_startup.is_some() {
|
||||||
|
verge.enable_self_startup = payload.enable_self_startup;
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo
|
||||||
|
if payload.enable_system_proxy.is_some() {
|
||||||
|
verge.enable_system_proxy = payload.enable_system_proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
save_verge(&verge)
|
||||||
|
}
|
||||||
|
@ -3,5 +3,12 @@ use serde::{Deserialize, Serialize};
|
|||||||
/// ### `verge.yaml` schema
|
/// ### `verge.yaml` schema
|
||||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct VergeConfig {
|
pub struct VergeConfig {
|
||||||
pub something: Option<String>,
|
/// `light` or `dark`
|
||||||
|
pub theme_mode: Option<String>,
|
||||||
|
|
||||||
|
/// can the app auto startup
|
||||||
|
pub enable_self_startup: Option<bool>,
|
||||||
|
|
||||||
|
/// set system proxy
|
||||||
|
pub enable_system_proxy: Option<bool>,
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use super::emit::ClashInfoPayload;
|
use super::emit::ClashInfoPayload;
|
||||||
|
use crate::config::VergeConfig;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ClashInfoState(pub Arc<Mutex<ClashInfoPayload>>);
|
pub struct ClashInfoState(pub Arc<Mutex<ClashInfoPayload>>);
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ProfileLock(pub Mutex<bool>);
|
pub struct ProfileLock(pub Mutex<bool>);
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct VergeConfLock(pub Arc<Mutex<VergeConfig>>);
|
||||||
|
@ -10,7 +10,10 @@ mod config;
|
|||||||
mod events;
|
mod events;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use crate::{events::state, utils::clash::put_clash_profile};
|
use crate::{
|
||||||
|
events::state,
|
||||||
|
utils::{clash::put_clash_profile, config::read_verge},
|
||||||
|
};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use tauri::{
|
use tauri::{
|
||||||
api, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
|
api, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
|
||||||
@ -58,8 +61,12 @@ fn main() -> std::io::Result<()> {
|
|||||||
})
|
})
|
||||||
.invoke_handler(tauri::generate_handler![
|
.invoke_handler(tauri::generate_handler![
|
||||||
cmds::some::restart_sidecar,
|
cmds::some::restart_sidecar,
|
||||||
cmds::some::get_clash_info,
|
|
||||||
cmds::some::set_sys_proxy,
|
cmds::some::set_sys_proxy,
|
||||||
|
cmds::some::get_sys_proxy,
|
||||||
|
cmds::some::get_clash_info,
|
||||||
|
cmds::some::patch_clash_config,
|
||||||
|
cmds::some::get_verge_config,
|
||||||
|
cmds::some::patch_verge_config,
|
||||||
cmds::profile::import_profile,
|
cmds::profile::import_profile,
|
||||||
cmds::profile::update_profile,
|
cmds::profile::update_profile,
|
||||||
cmds::profile::get_profiles,
|
cmds::profile::get_profiles,
|
||||||
@ -82,6 +89,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.manage(state::VergeConfLock(Arc::new(Mutex::new(read_verge()))));
|
||||||
app.manage(state::ClashInfoState(Arc::new(Mutex::new(info))));
|
app.manage(state::ClashInfoState(Arc::new(Mutex::new(info))));
|
||||||
app.manage(state::ProfileLock::default());
|
app.manage(state::ProfileLock::default());
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
config::{ClashController, ProfilesConfig},
|
config::{ClashController, ProfilesConfig, VergeConfig},
|
||||||
utils::app_home_dir,
|
utils::app_home_dir,
|
||||||
};
|
};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
@ -7,9 +7,9 @@ use serde_yaml::{Mapping, Value};
|
|||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
/// read data from yaml as struct T
|
/// read data from yaml as struct T
|
||||||
pub fn read_yaml<T: DeserializeOwned>(path: PathBuf) -> T {
|
pub fn read_yaml<T: DeserializeOwned + Default>(path: PathBuf) -> T {
|
||||||
let yaml_str = fs::read_to_string(path).unwrap();
|
let yaml_str = fs::read_to_string(path).unwrap_or("".into());
|
||||||
serde_yaml::from_str::<T>(&yaml_str).unwrap()
|
serde_yaml::from_str::<T>(&yaml_str).unwrap_or(T::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// - save the data to the file
|
/// - save the data to the file
|
||||||
@ -36,11 +36,20 @@ pub fn save_yaml<T: Serialize>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get Clash Core Config
|
/// Get Clash Core Config `config.yaml`
|
||||||
pub fn read_clash() -> Mapping {
|
pub fn read_clash() -> Mapping {
|
||||||
read_yaml::<Mapping>(app_home_dir().join("config.yaml"))
|
read_yaml::<Mapping>(app_home_dir().join("config.yaml"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Save the clash core Config `config.yaml`
|
||||||
|
pub fn save_clash(config: &Mapping) -> Result<(), String> {
|
||||||
|
save_yaml(
|
||||||
|
app_home_dir().join("config.yaml"),
|
||||||
|
config,
|
||||||
|
Some("# Default Config For Clash Core\n\n"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get infomation of the clash's `external-controller` and `secret`
|
/// Get infomation of the clash's `external-controller` and `secret`
|
||||||
pub fn read_clash_controller() -> ClashController {
|
pub fn read_clash_controller() -> ClashController {
|
||||||
let config = read_clash();
|
let config = read_clash();
|
||||||
@ -107,3 +116,17 @@ pub fn save_profiles(profiles: &ProfilesConfig) -> Result<(), String> {
|
|||||||
Some("# Profiles Config for Clash Verge\n\n"),
|
Some("# Profiles Config for Clash Verge\n\n"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the `verge.yaml`
|
||||||
|
pub fn read_verge() -> VergeConfig {
|
||||||
|
read_yaml::<VergeConfig>(app_home_dir().join("verge.yaml"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Save Verge App Config
|
||||||
|
pub fn save_verge(verge: &VergeConfig) -> Result<(), String> {
|
||||||
|
save_yaml(
|
||||||
|
app_home_dir().join("verge.yaml"),
|
||||||
|
verge,
|
||||||
|
Some("# The Config for Clash Verge App\n\n"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user