mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
feat(system tray): support switch rule/global/direct/script mode in system tray
This commit is contained in:
@@ -5,285 +5,304 @@ use serde_yaml::{Mapping, Value};
|
||||
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct ClashInfo {
|
||||
/// clash sidecar status
|
||||
pub status: String,
|
||||
/// clash sidecar status
|
||||
pub status: String,
|
||||
|
||||
/// clash core port
|
||||
pub port: Option<String>,
|
||||
/// clash core port
|
||||
pub port: Option<String>,
|
||||
|
||||
/// same as `external-controller`
|
||||
pub server: Option<String>,
|
||||
/// same as `external-controller`
|
||||
pub server: Option<String>,
|
||||
|
||||
/// clash secret
|
||||
pub secret: Option<String>,
|
||||
/// clash secret
|
||||
pub secret: Option<String>,
|
||||
|
||||
/// mode
|
||||
pub mode: Option<String>,
|
||||
}
|
||||
|
||||
impl ClashInfo {
|
||||
/// parse the clash's config.yaml
|
||||
/// get some information
|
||||
pub fn from(config: &Mapping) -> ClashInfo {
|
||||
let key_port_1 = Value::from("port");
|
||||
let key_port_2 = Value::from("mixed-port");
|
||||
let key_server = Value::from("external-controller");
|
||||
let key_secret = Value::from("secret");
|
||||
/// parse the clash's config.yaml
|
||||
/// get some information
|
||||
pub fn from(config: &Mapping) -> ClashInfo {
|
||||
let key_port_1 = Value::from("port");
|
||||
let key_port_2 = Value::from("mixed-port");
|
||||
let key_server = Value::from("external-controller");
|
||||
let key_secret = Value::from("secret");
|
||||
let key_mode = Value::from("mode");
|
||||
|
||||
let port = match config.get(&key_port_1) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
let port = match port {
|
||||
Some(_) => port,
|
||||
None => match config.get(&key_port_2) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
},
|
||||
};
|
||||
let port = match config.get(&key_port_1) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
let port = match port {
|
||||
Some(_) => port,
|
||||
None => match config.get(&key_port_2) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
},
|
||||
};
|
||||
|
||||
// `external-controller` could be
|
||||
// "127.0.0.1:9090" or ":9090"
|
||||
let server = match config.get(&key_server) {
|
||||
Some(value) => {
|
||||
let val_str = value.as_str().unwrap_or("");
|
||||
// `external-controller` could be
|
||||
// "127.0.0.1:9090" or ":9090"
|
||||
let server = match config.get(&key_server) {
|
||||
Some(value) => {
|
||||
let val_str = value.as_str().unwrap_or("");
|
||||
|
||||
if val_str.starts_with(":") {
|
||||
Some(format!("127.0.0.1{val_str}"))
|
||||
} else {
|
||||
Some(val_str.into())
|
||||
if val_str.starts_with(":") {
|
||||
Some(format!("127.0.0.1{val_str}"))
|
||||
} else {
|
||||
Some(val_str.into())
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let secret = match config.get(&key_secret) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Bool(val_bool) => Some(val_bool.to_string()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let mode = match config.get(&key_mode) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
ClashInfo {
|
||||
status: "init".into(),
|
||||
port,
|
||||
server,
|
||||
secret,
|
||||
mode,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let secret = match config.get(&key_secret) {
|
||||
Some(value) => match value {
|
||||
Value::String(val_str) => Some(val_str.clone()),
|
||||
Value::Bool(val_bool) => Some(val_bool.to_string()),
|
||||
Value::Number(val_num) => Some(val_num.to_string()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
ClashInfo {
|
||||
status: "init".into(),
|
||||
port,
|
||||
server,
|
||||
secret,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Clash {
|
||||
/// maintain the clash config
|
||||
pub config: Mapping,
|
||||
/// maintain the clash config
|
||||
pub config: Mapping,
|
||||
|
||||
/// some info
|
||||
pub info: ClashInfo,
|
||||
/// some info
|
||||
pub info: ClashInfo,
|
||||
}
|
||||
|
||||
impl Clash {
|
||||
pub fn new() -> Clash {
|
||||
let config = Clash::read_config();
|
||||
let info = ClashInfo::from(&config);
|
||||
pub fn new() -> Clash {
|
||||
let config = Clash::read_config();
|
||||
let info = ClashInfo::from(&config);
|
||||
|
||||
Clash { config, info }
|
||||
}
|
||||
|
||||
/// get clash config
|
||||
pub fn read_config() -> Mapping {
|
||||
config::read_yaml::<Mapping>(dirs::clash_path())
|
||||
}
|
||||
|
||||
/// save the clash config
|
||||
pub fn save_config(&self) -> Result<()> {
|
||||
config::save_yaml(
|
||||
dirs::clash_path(),
|
||||
&self.config,
|
||||
Some("# Default Config For Clash Core\n\n"),
|
||||
)
|
||||
}
|
||||
|
||||
/// patch update the clash config
|
||||
/// if the port is changed then return true
|
||||
pub fn patch_config(&mut self, patch: Mapping) -> Result<bool> {
|
||||
let port_key = Value::from("mixed-port");
|
||||
let server_key = Value::from("external-controller");
|
||||
let secret_key = Value::from("secret");
|
||||
|
||||
let mut change_port = false;
|
||||
let mut change_info = false;
|
||||
|
||||
for (key, value) in patch.into_iter() {
|
||||
if key == port_key {
|
||||
change_port = true;
|
||||
}
|
||||
|
||||
if key == port_key || key == server_key || key == secret_key {
|
||||
change_info = true;
|
||||
}
|
||||
|
||||
self.config.insert(key, value);
|
||||
Clash { config, info }
|
||||
}
|
||||
|
||||
if change_info {
|
||||
self.info = ClashInfo::from(&self.config);
|
||||
/// get clash config
|
||||
pub fn read_config() -> Mapping {
|
||||
config::read_yaml::<Mapping>(dirs::clash_path())
|
||||
}
|
||||
|
||||
self.save_config()?;
|
||||
|
||||
Ok(change_port)
|
||||
}
|
||||
|
||||
/// revise the `tun` and `dns` config
|
||||
pub fn _tun_mode(mut config: Mapping, enable: bool) -> Mapping {
|
||||
macro_rules! revise {
|
||||
($map: expr, $key: expr, $val: expr) => {
|
||||
let ret_key = Value::String($key.into());
|
||||
$map.insert(ret_key, Value::from($val));
|
||||
};
|
||||
/// save the clash config
|
||||
pub fn save_config(&self) -> Result<()> {
|
||||
config::save_yaml(
|
||||
dirs::clash_path(),
|
||||
&self.config,
|
||||
Some("# Default Config For Clash Core\n\n"),
|
||||
)
|
||||
}
|
||||
|
||||
// if key not exists then append value
|
||||
macro_rules! append {
|
||||
($map: expr, $key: expr, $val: expr) => {
|
||||
let ret_key = Value::String($key.into());
|
||||
if !$map.contains_key(&ret_key) {
|
||||
$map.insert(ret_key, Value::from($val));
|
||||
/// patch update the clash config
|
||||
/// if the port is changed then return true
|
||||
pub fn patch_config(&mut self, patch: Mapping) -> Result<(bool, bool)> {
|
||||
let port_key = Value::from("mixed-port");
|
||||
let server_key = Value::from("external-controller");
|
||||
let secret_key = Value::from("secret");
|
||||
let mode_key = Value::from("mode");
|
||||
|
||||
let mut change_port = false;
|
||||
let mut change_info = false;
|
||||
let mut change_mode = false;
|
||||
|
||||
for (key, value) in patch.into_iter() {
|
||||
if key == port_key {
|
||||
change_port = true;
|
||||
}
|
||||
|
||||
if key == mode_key {
|
||||
change_mode = true;
|
||||
}
|
||||
|
||||
if key == port_key || key == server_key || key == secret_key || key == mode_key {
|
||||
change_info = true;
|
||||
}
|
||||
|
||||
self.config.insert(key, value);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// tun config
|
||||
let tun_val = config.get(&Value::from("tun"));
|
||||
let mut new_tun = Mapping::new();
|
||||
|
||||
if tun_val.is_some() && tun_val.as_ref().unwrap().is_mapping() {
|
||||
new_tun = tun_val.as_ref().unwrap().as_mapping().unwrap().clone();
|
||||
}
|
||||
|
||||
revise!(new_tun, "enable", enable);
|
||||
|
||||
if enable {
|
||||
append!(new_tun, "stack", "gvisor");
|
||||
append!(new_tun, "dns-hijack", vec!["198.18.0.2:53"]);
|
||||
append!(new_tun, "auto-route", true);
|
||||
append!(new_tun, "auto-detect-interface", true);
|
||||
}
|
||||
|
||||
revise!(config, "tun", new_tun);
|
||||
|
||||
if enable {
|
||||
// dns config
|
||||
let dns_val = config.get(&Value::from("dns"));
|
||||
let mut new_dns = Mapping::new();
|
||||
|
||||
if dns_val.is_some() && dns_val.as_ref().unwrap().is_mapping() {
|
||||
new_dns = dns_val.as_ref().unwrap().as_mapping().unwrap().clone();
|
||||
}
|
||||
revise!(new_dns, "enable", enable);
|
||||
|
||||
// 借鉴cfw的默认配置
|
||||
append!(new_dns, "enhanced-mode", "fake-ip");
|
||||
append!(
|
||||
new_dns,
|
||||
"nameserver",
|
||||
vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"]
|
||||
);
|
||||
append!(new_dns, "fallback", vec![] as Vec<&str>);
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
append!(
|
||||
new_dns,
|
||||
"fake-ip-filter",
|
||||
vec![
|
||||
"dns.msftncsi.com",
|
||||
"www.msftncsi.com",
|
||||
"www.msftconnecttest.com"
|
||||
]
|
||||
);
|
||||
|
||||
revise!(config, "dns", new_dns);
|
||||
}
|
||||
|
||||
config
|
||||
}
|
||||
|
||||
/// only 5 default fields available (clash config fields)
|
||||
/// convert to lowercase
|
||||
pub fn strict_filter(config: Mapping) -> Mapping {
|
||||
// Only the following fields are allowed:
|
||||
// proxies/proxy-providers/proxy-groups/rule-providers/rules
|
||||
let valid_keys = vec![
|
||||
"proxies",
|
||||
"proxy-providers",
|
||||
"proxy-groups",
|
||||
"rules",
|
||||
"rule-providers",
|
||||
];
|
||||
|
||||
let mut new_config = Mapping::new();
|
||||
|
||||
for (key, value) in config.into_iter() {
|
||||
key.as_str().map(|key_str| {
|
||||
// change to lowercase
|
||||
let mut key_str = String::from(key_str);
|
||||
key_str.make_ascii_lowercase();
|
||||
|
||||
// filter
|
||||
if valid_keys.contains(&&*key_str) {
|
||||
new_config.insert(Value::String(key_str), value);
|
||||
if change_info {
|
||||
self.info = ClashInfo::from(&self.config);
|
||||
}
|
||||
});
|
||||
|
||||
self.save_config()?;
|
||||
|
||||
Ok((change_port, change_mode))
|
||||
}
|
||||
|
||||
new_config
|
||||
}
|
||||
|
||||
/// more clash config fields available
|
||||
/// convert to lowercase
|
||||
pub fn loose_filter(config: Mapping) -> Mapping {
|
||||
// all of these can not be revised by script or merge
|
||||
// http/https/socks port should be under control
|
||||
let not_allow = vec![
|
||||
"port",
|
||||
"socks-port",
|
||||
"mixed-port",
|
||||
"allow-lan",
|
||||
"mode",
|
||||
"external-controller",
|
||||
"secret",
|
||||
"log-level",
|
||||
];
|
||||
|
||||
let mut new_config = Mapping::new();
|
||||
|
||||
for (key, value) in config.into_iter() {
|
||||
key.as_str().map(|key_str| {
|
||||
// change to lowercase
|
||||
let mut key_str = String::from(key_str);
|
||||
key_str.make_ascii_lowercase();
|
||||
|
||||
// filter
|
||||
if !not_allow.contains(&&*key_str) {
|
||||
new_config.insert(Value::String(key_str), value);
|
||||
/// revise the `tun` and `dns` config
|
||||
pub fn _tun_mode(mut config: Mapping, enable: bool) -> Mapping {
|
||||
macro_rules! revise {
|
||||
($map: expr, $key: expr, $val: expr) => {
|
||||
let ret_key = Value::String($key.into());
|
||||
$map.insert(ret_key, Value::from($val));
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// if key not exists then append value
|
||||
macro_rules! append {
|
||||
($map: expr, $key: expr, $val: expr) => {
|
||||
let ret_key = Value::String($key.into());
|
||||
if !$map.contains_key(&ret_key) {
|
||||
$map.insert(ret_key, Value::from($val));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// tun config
|
||||
let tun_val = config.get(&Value::from("tun"));
|
||||
let mut new_tun = Mapping::new();
|
||||
|
||||
if tun_val.is_some() && tun_val.as_ref().unwrap().is_mapping() {
|
||||
new_tun = tun_val.as_ref().unwrap().as_mapping().unwrap().clone();
|
||||
}
|
||||
|
||||
revise!(new_tun, "enable", enable);
|
||||
|
||||
if enable {
|
||||
append!(new_tun, "stack", "gvisor");
|
||||
append!(new_tun, "dns-hijack", vec!["198.18.0.2:53"]);
|
||||
append!(new_tun, "auto-route", true);
|
||||
append!(new_tun, "auto-detect-interface", true);
|
||||
}
|
||||
|
||||
revise!(config, "tun", new_tun);
|
||||
|
||||
if enable {
|
||||
// dns config
|
||||
let dns_val = config.get(&Value::from("dns"));
|
||||
let mut new_dns = Mapping::new();
|
||||
|
||||
if dns_val.is_some() && dns_val.as_ref().unwrap().is_mapping() {
|
||||
new_dns = dns_val.as_ref().unwrap().as_mapping().unwrap().clone();
|
||||
}
|
||||
revise!(new_dns, "enable", enable);
|
||||
|
||||
// 借鉴cfw的默认配置
|
||||
append!(new_dns, "enhanced-mode", "fake-ip");
|
||||
append!(
|
||||
new_dns,
|
||||
"nameserver",
|
||||
vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"]
|
||||
);
|
||||
append!(new_dns, "fallback", vec![] as Vec<&str>);
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
append!(
|
||||
new_dns,
|
||||
"fake-ip-filter",
|
||||
vec![
|
||||
"dns.msftncsi.com",
|
||||
"www.msftncsi.com",
|
||||
"www.msftconnecttest.com"
|
||||
]
|
||||
);
|
||||
|
||||
revise!(config, "dns", new_dns);
|
||||
}
|
||||
|
||||
config
|
||||
}
|
||||
|
||||
new_config
|
||||
}
|
||||
/// only 5 default fields available (clash config fields)
|
||||
/// convert to lowercase
|
||||
pub fn strict_filter(config: Mapping) -> Mapping {
|
||||
// Only the following fields are allowed:
|
||||
// proxies/proxy-providers/proxy-groups/rule-providers/rules
|
||||
let valid_keys = vec![
|
||||
"proxies",
|
||||
"proxy-providers",
|
||||
"proxy-groups",
|
||||
"rules",
|
||||
"rule-providers",
|
||||
];
|
||||
|
||||
let mut new_config = Mapping::new();
|
||||
|
||||
for (key, value) in config.into_iter() {
|
||||
key.as_str().map(|key_str| {
|
||||
// change to lowercase
|
||||
let mut key_str = String::from(key_str);
|
||||
key_str.make_ascii_lowercase();
|
||||
|
||||
// filter
|
||||
if valid_keys.contains(&&*key_str) {
|
||||
new_config.insert(Value::String(key_str), value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
new_config
|
||||
}
|
||||
|
||||
/// more clash config fields available
|
||||
/// convert to lowercase
|
||||
pub fn loose_filter(config: Mapping) -> Mapping {
|
||||
// all of these can not be revised by script or merge
|
||||
// http/https/socks port should be under control
|
||||
let not_allow = vec![
|
||||
"port",
|
||||
"socks-port",
|
||||
"mixed-port",
|
||||
"allow-lan",
|
||||
"mode",
|
||||
"external-controller",
|
||||
"secret",
|
||||
"log-level",
|
||||
];
|
||||
|
||||
let mut new_config = Mapping::new();
|
||||
|
||||
for (key, value) in config.into_iter() {
|
||||
key.as_str().map(|key_str| {
|
||||
// change to lowercase
|
||||
let mut key_str = String::from(key_str);
|
||||
key_str.make_ascii_lowercase();
|
||||
|
||||
// filter
|
||||
if !not_allow.contains(&&*key_str) {
|
||||
new_config.insert(Value::String(key_str), value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
new_config
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Clash {
|
||||
fn default() -> Self {
|
||||
Clash::new()
|
||||
}
|
||||
fn default() -> Self {
|
||||
Clash::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::utils::{dirs, help};
|
||||
use anyhow::{bail, Result};
|
||||
use parking_lot::Mutex;
|
||||
use serde_yaml::Mapping;
|
||||
use serde_yaml::Value;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tauri::{AppHandle, Manager, Window};
|
||||
@@ -34,421 +35,461 @@ static mut WINDOW_CLOSABLE: bool = true;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Core {
|
||||
pub clash: Arc<Mutex<Clash>>,
|
||||
pub clash: Arc<Mutex<Clash>>,
|
||||
|
||||
pub verge: Arc<Mutex<Verge>>,
|
||||
pub verge: Arc<Mutex<Verge>>,
|
||||
|
||||
pub profiles: Arc<Mutex<Profiles>>,
|
||||
pub profiles: Arc<Mutex<Profiles>>,
|
||||
|
||||
pub service: Arc<Mutex<Service>>,
|
||||
pub service: Arc<Mutex<Service>>,
|
||||
|
||||
pub sysopt: Arc<Mutex<Sysopt>>,
|
||||
pub sysopt: Arc<Mutex<Sysopt>>,
|
||||
|
||||
pub timer: Arc<Mutex<Timer>>,
|
||||
pub timer: Arc<Mutex<Timer>>,
|
||||
|
||||
pub window: Arc<Mutex<Option<Window>>>,
|
||||
pub window: Arc<Mutex<Option<Window>>>,
|
||||
}
|
||||
|
||||
impl Core {
|
||||
pub fn new() -> Core {
|
||||
let clash = Clash::new();
|
||||
let verge = Verge::new();
|
||||
let profiles = Profiles::new();
|
||||
let service = Service::new();
|
||||
pub fn new() -> Core {
|
||||
let clash = Clash::new();
|
||||
let verge = Verge::new();
|
||||
let profiles = Profiles::new();
|
||||
let service = Service::new();
|
||||
|
||||
Core {
|
||||
clash: Arc::new(Mutex::new(clash)),
|
||||
verge: Arc::new(Mutex::new(verge)),
|
||||
profiles: Arc::new(Mutex::new(profiles)),
|
||||
service: Arc::new(Mutex::new(service)),
|
||||
sysopt: Arc::new(Mutex::new(Sysopt::new())),
|
||||
timer: Arc::new(Mutex::new(Timer::new())),
|
||||
window: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
/// initialize the core state
|
||||
pub fn init(&self, app_handle: tauri::AppHandle) {
|
||||
let verge = self.verge.lock();
|
||||
let clash_core = verge.clash_core.clone();
|
||||
|
||||
let mut service = self.service.lock();
|
||||
service.set_core(clash_core);
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let enable = verge.enable_service_mode.clone();
|
||||
service.set_mode(enable.unwrap_or(false));
|
||||
Core {
|
||||
clash: Arc::new(Mutex::new(clash)),
|
||||
verge: Arc::new(Mutex::new(verge)),
|
||||
profiles: Arc::new(Mutex::new(profiles)),
|
||||
service: Arc::new(Mutex::new(service)),
|
||||
sysopt: Arc::new(Mutex::new(Sysopt::new())),
|
||||
timer: Arc::new(Mutex::new(Timer::new())),
|
||||
window: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
log_if_err!(service.start());
|
||||
drop(verge);
|
||||
drop(service);
|
||||
/// initialize the core state
|
||||
pub fn init(&self, app_handle: tauri::AppHandle) {
|
||||
let verge = self.verge.lock();
|
||||
let clash_core = verge.clash_core.clone();
|
||||
|
||||
log_if_err!(self.activate());
|
||||
let mut service = self.service.lock();
|
||||
service.set_core(clash_core);
|
||||
|
||||
let clash = self.clash.lock();
|
||||
let verge = self.verge.lock();
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let enable = verge.enable_service_mode.clone();
|
||||
service.set_mode(enable.unwrap_or(false));
|
||||
}
|
||||
|
||||
let silent_start = verge.enable_silent_start.clone();
|
||||
let auto_launch = verge.enable_auto_launch.clone();
|
||||
log_if_err!(service.start());
|
||||
drop(verge);
|
||||
drop(service);
|
||||
|
||||
// silent start
|
||||
if silent_start.unwrap_or(false) {
|
||||
let window = self.window.lock();
|
||||
window.as_ref().map(|win| {
|
||||
win.hide().unwrap();
|
||||
});
|
||||
log_if_err!(self.activate());
|
||||
|
||||
let clash = self.clash.lock();
|
||||
let verge = self.verge.lock();
|
||||
|
||||
let silent_start = verge.enable_silent_start.clone();
|
||||
let auto_launch = verge.enable_auto_launch.clone();
|
||||
|
||||
// silent start
|
||||
if silent_start.unwrap_or(false) {
|
||||
let window = self.window.lock();
|
||||
window.as_ref().map(|win| {
|
||||
win.hide().unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
|
||||
sysopt.init_sysproxy(clash.info.port.clone(), &verge);
|
||||
|
||||
drop(clash);
|
||||
drop(verge);
|
||||
|
||||
log_if_err!(sysopt.init_launch(auto_launch));
|
||||
|
||||
log_if_err!(self.update_systray(&app_handle));
|
||||
|
||||
// wait the window setup during resolve app
|
||||
let core = self.clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
sleep(Duration::from_secs(2)).await;
|
||||
log_if_err!(core.activate_enhanced(true));
|
||||
});
|
||||
|
||||
// timer initialize
|
||||
let mut timer = self.timer.lock();
|
||||
timer.set_core(self.clone());
|
||||
log_if_err!(timer.refresh());
|
||||
}
|
||||
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
|
||||
sysopt.init_sysproxy(clash.info.port.clone(), &verge);
|
||||
|
||||
drop(clash);
|
||||
drop(verge);
|
||||
|
||||
log_if_err!(sysopt.init_launch(auto_launch));
|
||||
|
||||
log_if_err!(self.update_systray(&app_handle));
|
||||
|
||||
// wait the window setup during resolve app
|
||||
let core = self.clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
sleep(Duration::from_secs(2)).await;
|
||||
log_if_err!(core.activate_enhanced(true));
|
||||
});
|
||||
|
||||
// timer initialize
|
||||
let mut timer = self.timer.lock();
|
||||
timer.set_core(self.clone());
|
||||
log_if_err!(timer.refresh());
|
||||
}
|
||||
|
||||
/// save the window instance
|
||||
pub fn set_win(&self, win: Option<Window>) {
|
||||
let mut window = self.window.lock();
|
||||
*window = win;
|
||||
}
|
||||
|
||||
/// restart the clash sidecar
|
||||
pub fn restart_clash(&self) -> Result<()> {
|
||||
let mut service = self.service.lock();
|
||||
service.restart()?;
|
||||
drop(service);
|
||||
|
||||
self.activate()?;
|
||||
self.activate_enhanced(true)
|
||||
}
|
||||
|
||||
/// change the clash core
|
||||
pub fn change_core(&self, clash_core: Option<String>) -> Result<()> {
|
||||
let clash_core = clash_core.unwrap_or("clash".into());
|
||||
|
||||
if &clash_core != "clash" && &clash_core != "clash-meta" {
|
||||
bail!("invalid clash core name \"{clash_core}\"");
|
||||
/// save the window instance
|
||||
pub fn set_win(&self, win: Option<Window>) {
|
||||
let mut window = self.window.lock();
|
||||
*window = win;
|
||||
}
|
||||
|
||||
let mut verge = self.verge.lock();
|
||||
verge.patch_config(Verge {
|
||||
clash_core: Some(clash_core.clone()),
|
||||
..Verge::default()
|
||||
})?;
|
||||
drop(verge);
|
||||
/// restart the clash sidecar
|
||||
pub fn restart_clash(&self) -> Result<()> {
|
||||
let mut service = self.service.lock();
|
||||
service.restart()?;
|
||||
drop(service);
|
||||
|
||||
let mut service = self.service.lock();
|
||||
service.stop()?;
|
||||
service.set_core(Some(clash_core));
|
||||
service.start()?;
|
||||
drop(service);
|
||||
|
||||
self.activate()?;
|
||||
self.activate_enhanced(true)
|
||||
}
|
||||
|
||||
/// Patch Clash
|
||||
/// handle the clash config changed
|
||||
pub fn patch_clash(&self, patch: Mapping) -> Result<()> {
|
||||
let (changed, port) = {
|
||||
let mut clash = self.clash.lock();
|
||||
(clash.patch_config(patch)?, clash.info.port.clone())
|
||||
};
|
||||
|
||||
// todo: port check
|
||||
|
||||
if changed {
|
||||
let mut service = self.service.lock();
|
||||
service.restart()?;
|
||||
drop(service);
|
||||
|
||||
self.activate()?;
|
||||
self.activate_enhanced(true)?;
|
||||
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
let verge = self.verge.lock();
|
||||
sysopt.init_sysproxy(port, &verge);
|
||||
self.activate()?;
|
||||
self.activate_enhanced(true)
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// change the clash core
|
||||
pub fn change_core(&self, clash_core: Option<String>) -> Result<()> {
|
||||
let clash_core = clash_core.unwrap_or("clash".into());
|
||||
|
||||
/// Patch Verge
|
||||
pub fn patch_verge(&self, patch: Verge, app_handle: &AppHandle) -> Result<()> {
|
||||
let tun_mode = patch.enable_tun_mode.clone();
|
||||
let auto_launch = patch.enable_auto_launch.clone();
|
||||
let system_proxy = patch.enable_system_proxy.clone();
|
||||
let proxy_bypass = patch.system_proxy_bypass.clone();
|
||||
let proxy_guard = patch.enable_proxy_guard.clone();
|
||||
if &clash_core != "clash" && &clash_core != "clash-meta" {
|
||||
bail!("invalid clash core name \"{clash_core}\"");
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let service_mode = patch.enable_service_mode.clone();
|
||||
|
||||
if service_mode.is_some() {
|
||||
let service_mode = service_mode.unwrap();
|
||||
let mut verge = self.verge.lock();
|
||||
verge.patch_config(Verge {
|
||||
clash_core: Some(clash_core.clone()),
|
||||
..Verge::default()
|
||||
})?;
|
||||
drop(verge);
|
||||
|
||||
let mut service = self.service.lock();
|
||||
service.stop()?;
|
||||
service.set_mode(service_mode);
|
||||
service.set_core(Some(clash_core));
|
||||
service.start()?;
|
||||
drop(service);
|
||||
|
||||
self.activate_enhanced(false)?;
|
||||
}
|
||||
self.activate()?;
|
||||
self.activate_enhanced(true)
|
||||
}
|
||||
|
||||
if auto_launch.is_some() {
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
sysopt.update_launch(auto_launch)?;
|
||||
}
|
||||
/// Patch Clash
|
||||
/// handle the clash config changed
|
||||
pub fn patch_clash(&self, patch: Mapping, app_handle: &AppHandle) -> Result<()> {
|
||||
let ((changed_port, changed_mode), port) = {
|
||||
let mut clash = self.clash.lock();
|
||||
(clash.patch_config(patch)?, clash.info.port.clone())
|
||||
};
|
||||
|
||||
if system_proxy.is_some() || proxy_bypass.is_some() {
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
sysopt.update_sysproxy(system_proxy.clone(), proxy_bypass)?;
|
||||
sysopt.guard_proxy();
|
||||
}
|
||||
// todo: port check
|
||||
|
||||
if proxy_guard.unwrap_or(false) {
|
||||
let sysopt = self.sysopt.lock();
|
||||
sysopt.guard_proxy();
|
||||
}
|
||||
if changed_port {
|
||||
let mut service = self.service.lock();
|
||||
service.restart()?;
|
||||
drop(service);
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
if tun_mode.is_some() && *tun_mode.as_ref().unwrap_or(&false) {
|
||||
let wintun_dll = dirs::app_home_dir().join("wintun.dll");
|
||||
if !wintun_dll.exists() {
|
||||
bail!("failed to enable TUN for missing `wintun.dll`");
|
||||
}
|
||||
}
|
||||
self.activate()?;
|
||||
self.activate_enhanced(true)?;
|
||||
|
||||
// save the patch
|
||||
let mut verge = self.verge.lock();
|
||||
verge.patch_config(patch)?;
|
||||
drop(verge);
|
||||
|
||||
if system_proxy.is_some() || tun_mode.is_some() {
|
||||
self.update_systray(app_handle)?;
|
||||
}
|
||||
|
||||
if tun_mode.is_some() {
|
||||
self.activate_enhanced(false)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// update the system tray state
|
||||
pub fn update_systray(&self, app_handle: &AppHandle) -> Result<()> {
|
||||
let verge = self.verge.lock();
|
||||
let tray = app_handle.tray_handle();
|
||||
|
||||
let system_proxy = verge.enable_system_proxy.as_ref();
|
||||
let tun_mode = verge.enable_tun_mode.as_ref();
|
||||
|
||||
tray
|
||||
.get_item("system_proxy")
|
||||
.set_selected(*system_proxy.unwrap_or(&false))?;
|
||||
tray
|
||||
.get_item("tun_mode")
|
||||
.set_selected(*tun_mode.unwrap_or(&false))?;
|
||||
|
||||
// update verge config
|
||||
let window = app_handle.get_window("main");
|
||||
let notice = Notice::from(window);
|
||||
notice.refresh_verge();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// activate the profile
|
||||
/// auto activate enhanced profile
|
||||
pub fn activate(&self) -> Result<()> {
|
||||
let data = {
|
||||
let profiles = self.profiles.lock();
|
||||
let data = profiles.gen_activate()?;
|
||||
Clash::strict_filter(data)
|
||||
};
|
||||
|
||||
let (mut config, info) = {
|
||||
let clash = self.clash.lock();
|
||||
let config = clash.config.clone();
|
||||
let info = clash.info.clone();
|
||||
(config, info)
|
||||
};
|
||||
|
||||
for (key, value) in data.into_iter() {
|
||||
config.insert(key, value);
|
||||
}
|
||||
|
||||
let config = {
|
||||
let verge = self.verge.lock();
|
||||
let tun_mode = verge.enable_tun_mode.unwrap_or(false);
|
||||
Clash::_tun_mode(config, tun_mode)
|
||||
};
|
||||
|
||||
let notice = {
|
||||
let window = self.window.lock();
|
||||
Notice::from(window.clone())
|
||||
};
|
||||
|
||||
let service = self.service.lock();
|
||||
service.set_config(info, config, notice)
|
||||
}
|
||||
|
||||
/// Enhanced
|
||||
/// enhanced profiles mode
|
||||
pub fn activate_enhanced(&self, skip: bool) -> Result<()> {
|
||||
let window = self.window.lock();
|
||||
if window.is_none() {
|
||||
bail!("failed to get the main window");
|
||||
}
|
||||
|
||||
let event_name = help::get_uid("e");
|
||||
let event_name = format!("enhanced-cb-{event_name}");
|
||||
|
||||
// generate the payload
|
||||
let payload = {
|
||||
let profiles = self.profiles.lock();
|
||||
profiles.gen_enhanced(event_name.clone())?
|
||||
};
|
||||
|
||||
// do not run enhanced
|
||||
if payload.chain.len() == 0 {
|
||||
if skip {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
drop(window);
|
||||
return self.activate();
|
||||
}
|
||||
|
||||
let tun_mode = {
|
||||
let verge = self.verge.lock();
|
||||
verge.enable_tun_mode.unwrap_or(false)
|
||||
};
|
||||
|
||||
let info = {
|
||||
let clash = self.clash.lock();
|
||||
clash.info.clone()
|
||||
};
|
||||
|
||||
let notice = Notice::from(window.clone());
|
||||
let service = self.service.clone();
|
||||
|
||||
let window = window.clone().unwrap();
|
||||
window.once(&event_name, move |event| {
|
||||
let result = event.payload();
|
||||
|
||||
if result.is_none() {
|
||||
log::warn!("event payload result is none");
|
||||
return;
|
||||
}
|
||||
|
||||
let result = result.unwrap();
|
||||
let result: PrfEnhancedResult = serde_json::from_str(result).unwrap();
|
||||
|
||||
if let Some(data) = result.data {
|
||||
let mut config = Clash::read_config();
|
||||
let filter_data = Clash::loose_filter(data); // loose filter
|
||||
|
||||
for (key, value) in filter_data.into_iter() {
|
||||
config.insert(key, value);
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
let verge = self.verge.lock();
|
||||
sysopt.init_sysproxy(port, &verge);
|
||||
}
|
||||
|
||||
let config = Clash::_tun_mode(config, tun_mode);
|
||||
if changed_mode {
|
||||
self.update_systray(app_handle)?;
|
||||
}
|
||||
|
||||
let service = service.lock();
|
||||
log_if_err!(service.set_config(info, config, notice));
|
||||
|
||||
log::info!("profile enhanced status {}", result.status);
|
||||
}
|
||||
|
||||
result.error.map(|err| log::error!("{err}"));
|
||||
});
|
||||
|
||||
let verge = self.verge.lock();
|
||||
let silent_start = verge.enable_silent_start.clone();
|
||||
|
||||
let closable = unsafe { WINDOW_CLOSABLE };
|
||||
|
||||
if silent_start.unwrap_or(false) && closable {
|
||||
unsafe {
|
||||
WINDOW_CLOSABLE = false;
|
||||
}
|
||||
|
||||
window.emit("script-handler-close", payload).unwrap();
|
||||
} else {
|
||||
window.emit("script-handler", payload).unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
/// Patch Verge
|
||||
pub fn patch_verge(&self, patch: Verge, app_handle: &AppHandle) -> Result<()> {
|
||||
let tun_mode = patch.enable_tun_mode.clone();
|
||||
let auto_launch = patch.enable_auto_launch.clone();
|
||||
let system_proxy = patch.enable_system_proxy.clone();
|
||||
let proxy_bypass = patch.system_proxy_bypass.clone();
|
||||
let proxy_guard = patch.enable_proxy_guard.clone();
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let service_mode = patch.enable_service_mode.clone();
|
||||
|
||||
if service_mode.is_some() {
|
||||
let service_mode = service_mode.unwrap();
|
||||
|
||||
let mut service = self.service.lock();
|
||||
service.stop()?;
|
||||
service.set_mode(service_mode);
|
||||
service.start()?;
|
||||
drop(service);
|
||||
|
||||
self.activate_enhanced(false)?;
|
||||
}
|
||||
}
|
||||
|
||||
if auto_launch.is_some() {
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
sysopt.update_launch(auto_launch)?;
|
||||
}
|
||||
|
||||
if system_proxy.is_some() || proxy_bypass.is_some() {
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
sysopt.update_sysproxy(system_proxy.clone(), proxy_bypass)?;
|
||||
sysopt.guard_proxy();
|
||||
}
|
||||
|
||||
if proxy_guard.unwrap_or(false) {
|
||||
let sysopt = self.sysopt.lock();
|
||||
sysopt.guard_proxy();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
if tun_mode.is_some() && *tun_mode.as_ref().unwrap_or(&false) {
|
||||
let wintun_dll = dirs::app_home_dir().join("wintun.dll");
|
||||
if !wintun_dll.exists() {
|
||||
bail!("failed to enable TUN for missing `wintun.dll`");
|
||||
}
|
||||
}
|
||||
|
||||
// save the patch
|
||||
let mut verge = self.verge.lock();
|
||||
verge.patch_config(patch)?;
|
||||
drop(verge);
|
||||
|
||||
if system_proxy.is_some() || tun_mode.is_some() {
|
||||
self.update_systray(app_handle)?;
|
||||
}
|
||||
|
||||
if tun_mode.is_some() {
|
||||
self.activate_enhanced(false)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// update the system tray state
|
||||
pub fn update_systray(&self, app_handle: &AppHandle) -> Result<()> {
|
||||
let clash = self.clash.lock();
|
||||
let info = clash.info.clone();
|
||||
let mode = info.mode.as_ref();
|
||||
|
||||
let verge = self.verge.lock();
|
||||
let tray = app_handle.tray_handle();
|
||||
|
||||
let system_proxy = verge.enable_system_proxy.as_ref();
|
||||
let tun_mode = verge.enable_tun_mode.as_ref();
|
||||
|
||||
tray.get_item("rule_mode")
|
||||
.set_selected((*mode.unwrap()).eq("rule"))?;
|
||||
tray.get_item("global_mode")
|
||||
.set_selected((*mode.unwrap()).eq("global"))?;
|
||||
tray.get_item("direct_mode")
|
||||
.set_selected((*mode.unwrap()).eq("direct"))?;
|
||||
tray.get_item("script_mode")
|
||||
.set_selected((*mode.unwrap()).eq("script"))?;
|
||||
|
||||
tray.get_item("system_proxy")
|
||||
.set_selected(*system_proxy.unwrap_or(&false))?;
|
||||
tray.get_item("tun_mode")
|
||||
.set_selected(*tun_mode.unwrap_or(&false))?;
|
||||
|
||||
// update verge config
|
||||
let window = app_handle.get_window("main");
|
||||
let notice = Notice::from(window);
|
||||
notice.refresh_verge();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// activate the profile
|
||||
/// auto activate enhanced profile
|
||||
pub fn activate(&self) -> Result<()> {
|
||||
let data = {
|
||||
let profiles = self.profiles.lock();
|
||||
let data = profiles.gen_activate()?;
|
||||
Clash::strict_filter(data)
|
||||
};
|
||||
|
||||
let (mut config, info) = {
|
||||
let clash = self.clash.lock();
|
||||
let config = clash.config.clone();
|
||||
let info = clash.info.clone();
|
||||
(config, info)
|
||||
};
|
||||
|
||||
for (key, value) in data.into_iter() {
|
||||
config.insert(key, value);
|
||||
}
|
||||
|
||||
let config = {
|
||||
let verge = self.verge.lock();
|
||||
let tun_mode = verge.enable_tun_mode.unwrap_or(false);
|
||||
Clash::_tun_mode(config, tun_mode)
|
||||
};
|
||||
|
||||
let notice = {
|
||||
let window = self.window.lock();
|
||||
Notice::from(window.clone())
|
||||
};
|
||||
|
||||
let service = self.service.lock();
|
||||
service.set_config(info, config, notice)
|
||||
}
|
||||
|
||||
/// Enhanced
|
||||
/// enhanced profiles mode
|
||||
pub fn activate_enhanced(&self, skip: bool) -> Result<()> {
|
||||
let window = self.window.lock();
|
||||
if window.is_none() {
|
||||
bail!("failed to get the main window");
|
||||
}
|
||||
|
||||
let event_name = help::get_uid("e");
|
||||
let event_name = format!("enhanced-cb-{event_name}");
|
||||
|
||||
// generate the payload
|
||||
let payload = {
|
||||
let profiles = self.profiles.lock();
|
||||
profiles.gen_enhanced(event_name.clone())?
|
||||
};
|
||||
|
||||
// do not run enhanced
|
||||
if payload.chain.len() == 0 {
|
||||
if skip {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
drop(window);
|
||||
return self.activate();
|
||||
}
|
||||
|
||||
let tun_mode = {
|
||||
let verge = self.verge.lock();
|
||||
verge.enable_tun_mode.unwrap_or(false)
|
||||
};
|
||||
|
||||
let info = {
|
||||
let clash = self.clash.lock();
|
||||
clash.info.clone()
|
||||
};
|
||||
|
||||
let notice = Notice::from(window.clone());
|
||||
let service = self.service.clone();
|
||||
|
||||
let window = window.clone().unwrap();
|
||||
window.once(&event_name, move |event| {
|
||||
let result = event.payload();
|
||||
|
||||
if result.is_none() {
|
||||
log::warn!("event payload result is none");
|
||||
return;
|
||||
}
|
||||
|
||||
let result = result.unwrap();
|
||||
let result: PrfEnhancedResult = serde_json::from_str(result).unwrap();
|
||||
|
||||
if let Some(data) = result.data {
|
||||
let mut config = Clash::read_config();
|
||||
let filter_data = Clash::loose_filter(data); // loose filter
|
||||
|
||||
for (key, value) in filter_data.into_iter() {
|
||||
config.insert(key, value);
|
||||
}
|
||||
|
||||
let config = Clash::_tun_mode(config, tun_mode);
|
||||
|
||||
let service = service.lock();
|
||||
log_if_err!(service.set_config(info, config, notice));
|
||||
|
||||
log::info!("profile enhanced status {}", result.status);
|
||||
}
|
||||
|
||||
result.error.map(|err| log::error!("{err}"));
|
||||
});
|
||||
|
||||
let verge = self.verge.lock();
|
||||
let silent_start = verge.enable_silent_start.clone();
|
||||
|
||||
let closable = unsafe { WINDOW_CLOSABLE };
|
||||
|
||||
if silent_start.unwrap_or(false) && closable {
|
||||
unsafe {
|
||||
WINDOW_CLOSABLE = false;
|
||||
}
|
||||
|
||||
window.emit("script-handler-close", payload).unwrap();
|
||||
} else {
|
||||
window.emit("script-handler", payload).unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// update rule/global/direct/script mode
|
||||
pub fn update_mode(&self, app_handle: &AppHandle, mode: &str) -> Result<()> {
|
||||
let mut mapping = Mapping::new();
|
||||
mapping.insert(Value::from("mode"), Value::from(mode));
|
||||
|
||||
self.patch_clash(mapping, app_handle);
|
||||
|
||||
let (config, info) = {
|
||||
let clash = self.clash.lock();
|
||||
let config = clash.config.clone();
|
||||
let info = clash.info.clone();
|
||||
(config, info)
|
||||
};
|
||||
|
||||
let notice = {
|
||||
let window = self.window.lock();
|
||||
Notice::from(window.clone())
|
||||
};
|
||||
|
||||
let service = self.service.lock();
|
||||
service.patch_config(info, config, notice);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Core {
|
||||
/// Static function
|
||||
/// update profile item
|
||||
pub async fn update_profile_item(
|
||||
core: Core,
|
||||
uid: String,
|
||||
option: Option<PrfOption>,
|
||||
) -> Result<()> {
|
||||
let (url, opt) = {
|
||||
let profiles = core.profiles.lock();
|
||||
let item = profiles.get_item(&uid)?;
|
||||
/// Static function
|
||||
/// update profile item
|
||||
pub async fn update_profile_item(
|
||||
core: Core,
|
||||
uid: String,
|
||||
option: Option<PrfOption>,
|
||||
) -> Result<()> {
|
||||
let (url, opt) = {
|
||||
let profiles = core.profiles.lock();
|
||||
let item = profiles.get_item(&uid)?;
|
||||
|
||||
if let Some(typ) = item.itype.as_ref() {
|
||||
// maybe only valid for `local` profile
|
||||
if *typ != "remote" {
|
||||
// reactivate the config
|
||||
if Some(uid) == profiles.get_current() {
|
||||
if let Some(typ) = item.itype.as_ref() {
|
||||
// maybe only valid for `local` profile
|
||||
if *typ != "remote" {
|
||||
// reactivate the config
|
||||
if Some(uid) == profiles.get_current() {
|
||||
drop(profiles);
|
||||
return core.activate_enhanced(false);
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
if item.url.is_none() {
|
||||
bail!("failed to get the profile item url");
|
||||
}
|
||||
|
||||
(item.url.clone().unwrap(), item.option.clone())
|
||||
};
|
||||
|
||||
let merged_opt = PrfOption::merge(opt, option);
|
||||
let item = PrfItem::from_url(&url, None, None, merged_opt).await?;
|
||||
|
||||
let mut profiles = core.profiles.lock();
|
||||
profiles.update_item(uid.clone(), item)?;
|
||||
|
||||
// reactivate the profile
|
||||
if Some(uid) == profiles.get_current() {
|
||||
drop(profiles);
|
||||
return core.activate_enhanced(false);
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
core.activate_enhanced(false)?;
|
||||
}
|
||||
}
|
||||
|
||||
if item.url.is_none() {
|
||||
bail!("failed to get the profile item url");
|
||||
}
|
||||
|
||||
(item.url.clone().unwrap(), item.option.clone())
|
||||
};
|
||||
|
||||
let merged_opt = PrfOption::merge(opt, option);
|
||||
let item = PrfItem::from_url(&url, None, None, merged_opt).await?;
|
||||
|
||||
let mut profiles = core.profiles.lock();
|
||||
profiles.update_item(uid.clone(), item)?;
|
||||
|
||||
// reactivate the profile
|
||||
if Some(uid) == profiles.get_current() {
|
||||
drop(profiles);
|
||||
core.activate_enhanced(false)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,33 +3,33 @@ use tauri::Window;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Notice {
|
||||
win: Option<Window>,
|
||||
win: Option<Window>,
|
||||
}
|
||||
|
||||
impl Notice {
|
||||
pub fn from(win: Option<Window>) -> Notice {
|
||||
Notice { win }
|
||||
}
|
||||
|
||||
pub fn set_win(&mut self, win: Option<Window>) {
|
||||
self.win = win;
|
||||
}
|
||||
|
||||
pub fn refresh_clash(&self) {
|
||||
if let Some(window) = self.win.as_ref() {
|
||||
log_if_err!(window.emit("verge://refresh-clash-config", "yes"));
|
||||
pub fn from(win: Option<Window>) -> Notice {
|
||||
Notice { win }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refresh_verge(&self) {
|
||||
if let Some(window) = self.win.as_ref() {
|
||||
log_if_err!(window.emit("verge://refresh-verge-config", "yes"));
|
||||
pub fn set_win(&mut self, win: Option<Window>) {
|
||||
self.win = win;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refresh_profiles(&self) {
|
||||
if let Some(window) = self.win.as_ref() {
|
||||
log_if_err!(window.emit("verge://refresh-profiles-config", "yes"));
|
||||
pub fn refresh_clash(&self) {
|
||||
if let Some(window) = self.win.as_ref() {
|
||||
log_if_err!(window.emit("verge://refresh-clash-config", "yes"));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refresh_verge(&self) {
|
||||
if let Some(window) = self.win.as_ref() {
|
||||
log_if_err!(window.emit("verge://refresh-verge-config", "yes"));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refresh_profiles(&self) {
|
||||
if let Some(window) = self.win.as_ref() {
|
||||
log_if_err!(window.emit("verge://refresh-profiles-config", "yes"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use super::Clash;
|
||||
use super::{notice::Notice, ClashInfo};
|
||||
use crate::log_if_err;
|
||||
use crate::utils::{config, dirs};
|
||||
@@ -13,401 +14,462 @@ static mut CLASH_CORE: &str = "clash";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Service {
|
||||
sidecar: Option<CommandChild>,
|
||||
sidecar: Option<CommandChild>,
|
||||
|
||||
#[allow(unused)]
|
||||
service_mode: bool,
|
||||
#[allow(unused)]
|
||||
service_mode: bool,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub fn new() -> Service {
|
||||
Service {
|
||||
sidecar: None,
|
||||
service_mode: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_core(&mut self, clash_core: Option<String>) {
|
||||
unsafe {
|
||||
CLASH_CORE = Box::leak(clash_core.unwrap_or("clash".into()).into_boxed_str());
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn set_mode(&mut self, enable: bool) {
|
||||
self.service_mode = enable;
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
pub fn start(&mut self) -> Result<()> {
|
||||
self.start_clash_by_sidecar()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn start(&mut self) -> Result<()> {
|
||||
if !self.service_mode {
|
||||
return self.start_clash_by_sidecar();
|
||||
}
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
match Self::check_service().await {
|
||||
Ok(status) => {
|
||||
// 未启动clash
|
||||
if status.code != 0 {
|
||||
if let Err(err) = Self::start_clash_by_service().await {
|
||||
log::error!("{err}");
|
||||
}
|
||||
}
|
||||
pub fn new() -> Service {
|
||||
Service {
|
||||
sidecar: None,
|
||||
service_mode: false,
|
||||
}
|
||||
Err(err) => log::error!("{err}"),
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
pub fn stop(&mut self) -> Result<()> {
|
||||
self.stop_clash_by_sidecar()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn stop(&mut self) -> Result<()> {
|
||||
if !self.service_mode {
|
||||
return self.stop_clash_by_sidecar();
|
||||
}
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
if let Err(err) = Self::stop_clash_by_service().await {
|
||||
log::error!("{err}");
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn restart(&mut self) -> Result<()> {
|
||||
self.stop()?;
|
||||
self.start()
|
||||
}
|
||||
|
||||
/// start the clash sidecar
|
||||
fn start_clash_by_sidecar(&mut self) -> Result<()> {
|
||||
if self.sidecar.is_some() {
|
||||
bail!("could not run clash sidecar twice");
|
||||
}
|
||||
|
||||
let app_dir = dirs::app_home_dir();
|
||||
let app_dir = app_dir.as_os_str().to_str().unwrap();
|
||||
|
||||
let clash_core = unsafe { CLASH_CORE };
|
||||
let cmd = Command::new_sidecar(clash_core)?;
|
||||
let (mut rx, cmd_child) = cmd.args(["-d", app_dir]).spawn()?;
|
||||
|
||||
self.sidecar = Some(cmd_child);
|
||||
|
||||
// clash log
|
||||
tauri::async_runtime::spawn(async move {
|
||||
while let Some(event) = rx.recv().await {
|
||||
match event {
|
||||
CommandEvent::Stdout(line) => log::info!("[clash]: {}", line),
|
||||
CommandEvent::Stderr(err) => log::error!("[clash]: {}", err),
|
||||
_ => {}
|
||||
pub fn set_core(&mut self, clash_core: Option<String>) {
|
||||
unsafe {
|
||||
CLASH_CORE = Box::leak(clash_core.unwrap_or("clash".into()).into_boxed_str());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// stop the clash sidecar
|
||||
fn stop_clash_by_sidecar(&mut self) -> Result<()> {
|
||||
if let Some(sidecar) = self.sidecar.take() {
|
||||
sidecar.kill()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// update clash config
|
||||
/// using PUT methods
|
||||
pub fn set_config(&self, info: ClashInfo, config: Mapping, notice: Notice) -> Result<()> {
|
||||
if !self.service_mode && self.sidecar.is_none() {
|
||||
bail!("did not start sidecar");
|
||||
}
|
||||
|
||||
let temp_path = dirs::profiles_temp_path();
|
||||
config::save_yaml(temp_path.clone(), &config, Some("# Clash Verge Temp File"))?;
|
||||
|
||||
if info.server.is_none() {
|
||||
if info.port.is_none() {
|
||||
bail!("failed to parse config.yaml file");
|
||||
} else {
|
||||
bail!("failed to parse the server");
|
||||
}
|
||||
#[allow(unused)]
|
||||
pub fn set_mode(&mut self, enable: bool) {
|
||||
self.service_mode = enable;
|
||||
}
|
||||
|
||||
let server = info.server.unwrap();
|
||||
let server = format!("http://{server}/configs");
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("Content-Type", "application/json".parse().unwrap());
|
||||
|
||||
if let Some(secret) = info.secret.as_ref() {
|
||||
let secret = format!("Bearer {}", secret.clone()).parse().unwrap();
|
||||
headers.insert("Authorization", secret);
|
||||
#[cfg(not(windows))]
|
||||
pub fn start(&mut self) -> Result<()> {
|
||||
self.start_clash_by_sidecar()
|
||||
}
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("path", temp_path.as_os_str().to_str().unwrap());
|
||||
#[cfg(windows)]
|
||||
pub fn start(&mut self) -> Result<()> {
|
||||
if !self.service_mode {
|
||||
return self.start_clash_by_sidecar();
|
||||
}
|
||||
|
||||
// retry 5 times
|
||||
for _ in 0..5 {
|
||||
match reqwest::ClientBuilder::new().no_proxy().build() {
|
||||
Ok(client) => {
|
||||
let builder = client.put(&server).headers(headers.clone()).json(&data);
|
||||
|
||||
match builder.send().await {
|
||||
Ok(resp) => {
|
||||
if resp.status() != 204 {
|
||||
log::error!("failed to activate clash with status \"{}\"", resp.status());
|
||||
tauri::async_runtime::spawn(async move {
|
||||
match Self::check_service().await {
|
||||
Ok(status) => {
|
||||
// 未启动clash
|
||||
if status.code != 0 {
|
||||
if let Err(err) = Self::start_clash_by_service().await {
|
||||
log::error!("{err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
notice.refresh_clash();
|
||||
|
||||
// do not retry
|
||||
break;
|
||||
}
|
||||
Err(err) => log::error!("failed to activate for `{err}`"),
|
||||
Err(err) => log::error!("{err}"),
|
||||
}
|
||||
}
|
||||
Err(err) => log::error!("failed to activate for `{err}`"),
|
||||
}
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
pub fn stop(&mut self) -> Result<()> {
|
||||
self.stop_clash_by_sidecar()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn stop(&mut self) -> Result<()> {
|
||||
if !self.service_mode {
|
||||
return self.stop_clash_by_sidecar();
|
||||
}
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
if let Err(err) = Self::stop_clash_by_service().await {
|
||||
log::error!("{err}");
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn restart(&mut self) -> Result<()> {
|
||||
self.stop()?;
|
||||
self.start()
|
||||
}
|
||||
|
||||
/// start the clash sidecar
|
||||
fn start_clash_by_sidecar(&mut self) -> Result<()> {
|
||||
if self.sidecar.is_some() {
|
||||
bail!("could not run clash sidecar twice");
|
||||
}
|
||||
|
||||
let app_dir = dirs::app_home_dir();
|
||||
let app_dir = app_dir.as_os_str().to_str().unwrap();
|
||||
|
||||
let clash_core = unsafe { CLASH_CORE };
|
||||
let cmd = Command::new_sidecar(clash_core)?;
|
||||
let (mut rx, cmd_child) = cmd.args(["-d", app_dir]).spawn()?;
|
||||
|
||||
self.sidecar = Some(cmd_child);
|
||||
|
||||
// clash log
|
||||
tauri::async_runtime::spawn(async move {
|
||||
while let Some(event) = rx.recv().await {
|
||||
match event {
|
||||
CommandEvent::Stdout(line) => log::info!("[clash]: {}", line),
|
||||
CommandEvent::Stderr(err) => log::error!("[clash]: {}", err),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// stop the clash sidecar
|
||||
fn stop_clash_by_sidecar(&mut self) -> Result<()> {
|
||||
if let Some(sidecar) = self.sidecar.take() {
|
||||
sidecar.kill()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// update clash config
|
||||
/// using PUT methods
|
||||
pub fn set_config(&self, info: ClashInfo, config: Mapping, notice: Notice) -> Result<()> {
|
||||
if !self.service_mode && self.sidecar.is_none() {
|
||||
bail!("did not start sidecar");
|
||||
}
|
||||
|
||||
let temp_path = dirs::profiles_temp_path();
|
||||
config::save_yaml(temp_path.clone(), &config, Some("# Clash Verge Temp File"))?;
|
||||
|
||||
if info.server.is_none() {
|
||||
if info.port.is_none() {
|
||||
bail!("failed to parse config.yaml file");
|
||||
} else {
|
||||
bail!("failed to parse the server");
|
||||
}
|
||||
}
|
||||
|
||||
let server = info.server.unwrap();
|
||||
let server = format!("http://{server}/configs");
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("Content-Type", "application/json".parse().unwrap());
|
||||
|
||||
if let Some(secret) = info.secret.as_ref() {
|
||||
let secret = format!("Bearer {}", secret.clone()).parse().unwrap();
|
||||
headers.insert("Authorization", secret);
|
||||
}
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("path", temp_path.as_os_str().to_str().unwrap());
|
||||
|
||||
// retry 5 times
|
||||
for _ in 0..5 {
|
||||
match reqwest::ClientBuilder::new().no_proxy().build() {
|
||||
Ok(client) => {
|
||||
let builder = client.put(&server).headers(headers.clone()).json(&data);
|
||||
|
||||
match builder.send().await {
|
||||
Ok(resp) => {
|
||||
if resp.status() != 204 {
|
||||
log::error!(
|
||||
"failed to activate clash with status \"{}\"",
|
||||
resp.status()
|
||||
);
|
||||
}
|
||||
|
||||
notice.refresh_clash();
|
||||
|
||||
// do not retry
|
||||
break;
|
||||
}
|
||||
Err(err) => log::error!("failed to activate for `{err}`"),
|
||||
}
|
||||
}
|
||||
Err(err) => log::error!("failed to activate for `{err}`"),
|
||||
}
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// patch clash config
|
||||
pub fn patch_config(&self, info: ClashInfo, config: Mapping, notice: Notice) -> Result<()> {
|
||||
if !self.service_mode && self.sidecar.is_none() {
|
||||
bail!("did not start sidecar");
|
||||
}
|
||||
|
||||
if info.server.is_none() {
|
||||
if info.port.is_none() {
|
||||
bail!("failed to parse config.yaml file");
|
||||
} else {
|
||||
bail!("failed to parse the server");
|
||||
}
|
||||
}
|
||||
|
||||
let server = info.server.unwrap();
|
||||
let server = format!("http://{server}/configs");
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("Content-Type", "application/json".parse().unwrap());
|
||||
|
||||
if let Some(secret) = info.secret.as_ref() {
|
||||
let secret = format!("Bearer {}", secret.clone()).parse().unwrap();
|
||||
headers.insert("Authorization", secret);
|
||||
}
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
// retry 5 times
|
||||
for _ in 0..5 {
|
||||
match reqwest::ClientBuilder::new().no_proxy().build() {
|
||||
Ok(client) => {
|
||||
let builder = client.patch(&server).headers(headers.clone()).json(&config);
|
||||
|
||||
match builder.send().await {
|
||||
Ok(resp) => {
|
||||
if resp.status() != 204 {
|
||||
log::error!(
|
||||
"failed to activate clash with status \"{}\"",
|
||||
resp.status()
|
||||
);
|
||||
}
|
||||
|
||||
notice.refresh_clash();
|
||||
|
||||
// do not retry
|
||||
break;
|
||||
}
|
||||
Err(err) => log::error!("failed to activate for `{err}`"),
|
||||
}
|
||||
}
|
||||
Err(err) => log::error!("failed to activate for `{err}`"),
|
||||
}
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Service {
|
||||
fn drop(&mut self) {
|
||||
log_if_err!(self.stop());
|
||||
}
|
||||
fn drop(&mut self) {
|
||||
log_if_err!(self.stop());
|
||||
}
|
||||
}
|
||||
|
||||
/// ### Service Mode
|
||||
///
|
||||
#[cfg(windows)]
|
||||
pub mod win_service {
|
||||
use super::*;
|
||||
use anyhow::Context;
|
||||
use deelevate::{PrivilegeLevel, Token};
|
||||
use runas::Command as RunasCommand;
|
||||
use std::os::windows::process::CommandExt;
|
||||
use std::{env::current_exe, process::Command as StdCommand};
|
||||
use super::*;
|
||||
use anyhow::Context;
|
||||
use deelevate::{PrivilegeLevel, Token};
|
||||
use runas::Command as RunasCommand;
|
||||
use std::os::windows::process::CommandExt;
|
||||
use std::{env::current_exe, process::Command as StdCommand};
|
||||
|
||||
const SERVICE_NAME: &str = "clash_verge_service";
|
||||
const SERVICE_NAME: &str = "clash_verge_service";
|
||||
|
||||
const SERVICE_URL: &str = "http://127.0.0.1:33211";
|
||||
const SERVICE_URL: &str = "http://127.0.0.1:33211";
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct ResponseBody {
|
||||
pub bin_path: String,
|
||||
pub config_dir: String,
|
||||
pub log_file: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct JsonResponse {
|
||||
pub code: u64,
|
||||
pub msg: String,
|
||||
pub data: Option<ResponseBody>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
/// Install the Clash Verge Service
|
||||
/// 该函数应该在协程或者线程中执行,避免UAC弹窗阻塞主线程
|
||||
pub async fn install_service() -> Result<()> {
|
||||
let binary_path = dirs::service_path();
|
||||
let install_path = binary_path.with_file_name("install-service.exe");
|
||||
|
||||
if !install_path.exists() {
|
||||
bail!("installer exe not found");
|
||||
}
|
||||
|
||||
let token = Token::with_current_process()?;
|
||||
let level = token.privilege_level()?;
|
||||
|
||||
let status = match level {
|
||||
PrivilegeLevel::NotPrivileged => RunasCommand::new(install_path).status()?,
|
||||
_ => StdCommand::new(install_path)
|
||||
.creation_flags(0x08000000)
|
||||
.status()?,
|
||||
};
|
||||
|
||||
if !status.success() {
|
||||
bail!(
|
||||
"failed to install service with status {}",
|
||||
status.code().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct ResponseBody {
|
||||
pub bin_path: String,
|
||||
pub config_dir: String,
|
||||
pub log_file: String,
|
||||
}
|
||||
|
||||
/// Uninstall the Clash Verge Service
|
||||
/// 该函数应该在协程或者线程中执行,避免UAC弹窗阻塞主线程
|
||||
pub async fn uninstall_service() -> Result<()> {
|
||||
let binary_path = dirs::service_path();
|
||||
let uninstall_path = binary_path.with_file_name("uninstall-service.exe");
|
||||
|
||||
if !uninstall_path.exists() {
|
||||
bail!("uninstaller exe not found");
|
||||
}
|
||||
|
||||
let token = Token::with_current_process()?;
|
||||
let level = token.privilege_level()?;
|
||||
|
||||
let status = match level {
|
||||
PrivilegeLevel::NotPrivileged => RunasCommand::new(uninstall_path).status()?,
|
||||
_ => StdCommand::new(uninstall_path)
|
||||
.creation_flags(0x08000000)
|
||||
.status()?,
|
||||
};
|
||||
|
||||
if !status.success() {
|
||||
bail!(
|
||||
"failed to uninstall service with status {}",
|
||||
status.code().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct JsonResponse {
|
||||
pub code: u64,
|
||||
pub msg: String,
|
||||
pub data: Option<ResponseBody>,
|
||||
}
|
||||
|
||||
/// [deprecated]
|
||||
/// start service
|
||||
/// 该函数应该在协程或者线程中执行,避免UAC弹窗阻塞主线程
|
||||
pub async fn start_service() -> Result<()> {
|
||||
let token = Token::with_current_process()?;
|
||||
let level = token.privilege_level()?;
|
||||
impl Service {
|
||||
/// Install the Clash Verge Service
|
||||
/// 该函数应该在协程或者线程中执行,避免UAC弹窗阻塞主线程
|
||||
pub async fn install_service() -> Result<()> {
|
||||
let binary_path = dirs::service_path();
|
||||
let install_path = binary_path.with_file_name("install-service.exe");
|
||||
|
||||
let args = ["start", SERVICE_NAME];
|
||||
if !install_path.exists() {
|
||||
bail!("installer exe not found");
|
||||
}
|
||||
|
||||
let status = match level {
|
||||
PrivilegeLevel::NotPrivileged => RunasCommand::new("sc").args(&args).status()?,
|
||||
_ => StdCommand::new("sc").args(&args).status()?,
|
||||
};
|
||||
let token = Token::with_current_process()?;
|
||||
let level = token.privilege_level()?;
|
||||
|
||||
match status.success() {
|
||||
true => Ok(()),
|
||||
false => bail!(
|
||||
"failed to start service with status {}",
|
||||
status.code().unwrap()
|
||||
),
|
||||
}
|
||||
let status = match level {
|
||||
PrivilegeLevel::NotPrivileged => RunasCommand::new(install_path).status()?,
|
||||
_ => StdCommand::new(install_path)
|
||||
.creation_flags(0x08000000)
|
||||
.status()?,
|
||||
};
|
||||
|
||||
if !status.success() {
|
||||
bail!(
|
||||
"failed to install service with status {}",
|
||||
status.code().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Uninstall the Clash Verge Service
|
||||
/// 该函数应该在协程或者线程中执行,避免UAC弹窗阻塞主线程
|
||||
pub async fn uninstall_service() -> Result<()> {
|
||||
let binary_path = dirs::service_path();
|
||||
let uninstall_path = binary_path.with_file_name("uninstall-service.exe");
|
||||
|
||||
if !uninstall_path.exists() {
|
||||
bail!("uninstaller exe not found");
|
||||
}
|
||||
|
||||
let token = Token::with_current_process()?;
|
||||
let level = token.privilege_level()?;
|
||||
|
||||
let status = match level {
|
||||
PrivilegeLevel::NotPrivileged => RunasCommand::new(uninstall_path).status()?,
|
||||
_ => StdCommand::new(uninstall_path)
|
||||
.creation_flags(0x08000000)
|
||||
.status()?,
|
||||
};
|
||||
|
||||
if !status.success() {
|
||||
bail!(
|
||||
"failed to uninstall service with status {}",
|
||||
status.code().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// [deprecated]
|
||||
/// start service
|
||||
/// 该函数应该在协程或者线程中执行,避免UAC弹窗阻塞主线程
|
||||
pub async fn start_service() -> Result<()> {
|
||||
let token = Token::with_current_process()?;
|
||||
let level = token.privilege_level()?;
|
||||
|
||||
let args = ["start", SERVICE_NAME];
|
||||
|
||||
let status = match level {
|
||||
PrivilegeLevel::NotPrivileged => RunasCommand::new("sc").args(&args).status()?,
|
||||
_ => StdCommand::new("sc").args(&args).status()?,
|
||||
};
|
||||
|
||||
match status.success() {
|
||||
true => Ok(()),
|
||||
false => bail!(
|
||||
"failed to start service with status {}",
|
||||
status.code().unwrap()
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// stop service
|
||||
pub async fn stop_service() -> Result<()> {
|
||||
let url = format!("{SERVICE_URL}/stop_service");
|
||||
let res = reqwest::ClientBuilder::new()
|
||||
.no_proxy()
|
||||
.build()?
|
||||
.post(url)
|
||||
.send()
|
||||
.await?
|
||||
.json::<JsonResponse>()
|
||||
.await
|
||||
.context("failed to connect to the Clash Verge Service")?;
|
||||
|
||||
if res.code != 0 {
|
||||
bail!(res.msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// check the windows service status
|
||||
pub async fn check_service() -> Result<JsonResponse> {
|
||||
let url = format!("{SERVICE_URL}/get_clash");
|
||||
let response = reqwest::ClientBuilder::new()
|
||||
.no_proxy()
|
||||
.build()?
|
||||
.get(url)
|
||||
.send()
|
||||
.await?
|
||||
.json::<JsonResponse>()
|
||||
.await
|
||||
.context("failed to connect to the Clash Verge Service")?;
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
/// start the clash by service
|
||||
pub(super) async fn start_clash_by_service() -> Result<()> {
|
||||
let status = Self::check_service().await?;
|
||||
|
||||
if status.code == 0 {
|
||||
Self::stop_clash_by_service().await?;
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
|
||||
let clash_core = unsafe { CLASH_CORE };
|
||||
let clash_bin = format!("{clash_core}.exe");
|
||||
let bin_path = current_exe().unwrap().with_file_name(clash_bin);
|
||||
let bin_path = bin_path.as_os_str().to_str().unwrap();
|
||||
|
||||
let config_dir = dirs::app_home_dir();
|
||||
let config_dir = config_dir.as_os_str().to_str().unwrap();
|
||||
|
||||
let log_path = dirs::service_log_file();
|
||||
let log_path = log_path.as_os_str().to_str().unwrap();
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert("bin_path", bin_path);
|
||||
map.insert("config_dir", config_dir);
|
||||
map.insert("log_file", log_path);
|
||||
|
||||
let url = format!("{SERVICE_URL}/start_clash");
|
||||
let res = reqwest::ClientBuilder::new()
|
||||
.no_proxy()
|
||||
.build()?
|
||||
.post(url)
|
||||
.json(&map)
|
||||
.send()
|
||||
.await?
|
||||
.json::<JsonResponse>()
|
||||
.await
|
||||
.context("failed to connect to the Clash Verge Service")?;
|
||||
|
||||
if res.code != 0 {
|
||||
bail!(res.msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// stop the clash by service
|
||||
pub(super) async fn stop_clash_by_service() -> Result<()> {
|
||||
let url = format!("{SERVICE_URL}/stop_clash");
|
||||
let res = reqwest::ClientBuilder::new()
|
||||
.no_proxy()
|
||||
.build()?
|
||||
.post(url)
|
||||
.send()
|
||||
.await?
|
||||
.json::<JsonResponse>()
|
||||
.await
|
||||
.context("failed to connect to the Clash Verge Service")?;
|
||||
|
||||
if res.code != 0 {
|
||||
bail!(res.msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// stop service
|
||||
pub async fn stop_service() -> Result<()> {
|
||||
let url = format!("{SERVICE_URL}/stop_service");
|
||||
let res = reqwest::ClientBuilder::new()
|
||||
.no_proxy()
|
||||
.build()?
|
||||
.post(url)
|
||||
.send()
|
||||
.await?
|
||||
.json::<JsonResponse>()
|
||||
.await
|
||||
.context("failed to connect to the Clash Verge Service")?;
|
||||
|
||||
if res.code != 0 {
|
||||
bail!(res.msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// check the windows service status
|
||||
pub async fn check_service() -> Result<JsonResponse> {
|
||||
let url = format!("{SERVICE_URL}/get_clash");
|
||||
let response = reqwest::ClientBuilder::new()
|
||||
.no_proxy()
|
||||
.build()?
|
||||
.get(url)
|
||||
.send()
|
||||
.await?
|
||||
.json::<JsonResponse>()
|
||||
.await
|
||||
.context("failed to connect to the Clash Verge Service")?;
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
/// start the clash by service
|
||||
pub(super) async fn start_clash_by_service() -> Result<()> {
|
||||
let status = Self::check_service().await?;
|
||||
|
||||
if status.code == 0 {
|
||||
Self::stop_clash_by_service().await?;
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
|
||||
let clash_core = unsafe { CLASH_CORE };
|
||||
let clash_bin = format!("{clash_core}.exe");
|
||||
let bin_path = current_exe().unwrap().with_file_name(clash_bin);
|
||||
let bin_path = bin_path.as_os_str().to_str().unwrap();
|
||||
|
||||
let config_dir = dirs::app_home_dir();
|
||||
let config_dir = config_dir.as_os_str().to_str().unwrap();
|
||||
|
||||
let log_path = dirs::service_log_file();
|
||||
let log_path = log_path.as_os_str().to_str().unwrap();
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert("bin_path", bin_path);
|
||||
map.insert("config_dir", config_dir);
|
||||
map.insert("log_file", log_path);
|
||||
|
||||
let url = format!("{SERVICE_URL}/start_clash");
|
||||
let res = reqwest::ClientBuilder::new()
|
||||
.no_proxy()
|
||||
.build()?
|
||||
.post(url)
|
||||
.json(&map)
|
||||
.send()
|
||||
.await?
|
||||
.json::<JsonResponse>()
|
||||
.await
|
||||
.context("failed to connect to the Clash Verge Service")?;
|
||||
|
||||
if res.code != 0 {
|
||||
bail!(res.msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// stop the clash by service
|
||||
pub(super) async fn stop_clash_by_service() -> Result<()> {
|
||||
let url = format!("{SERVICE_URL}/stop_clash");
|
||||
let res = reqwest::ClientBuilder::new()
|
||||
.no_proxy()
|
||||
.build()?
|
||||
.post(url)
|
||||
.send()
|
||||
.await?
|
||||
.json::<JsonResponse>()
|
||||
.await
|
||||
.context("failed to connect to the Clash Verge Service")?;
|
||||
|
||||
if res.code != 0 {
|
||||
bail!(res.msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user