Files
clash-proxy/src-tauri/src/core/sysopt.rs

464 lines
17 KiB
Rust
Raw Normal View History

2025-04-21 00:06:37 +08:00
#[cfg(target_os = "windows")]
use crate::utils::autostart as startup_shortcut;
2024-05-26 17:59:39 +08:00
use crate::{
config::{Config, IVerge},
core::handle::Handle,
logging, logging_error,
process::AsyncHandler,
2025-04-21 00:06:37 +08:00
utils::logging::Type,
2024-05-26 17:59:39 +08:00
};
2024-11-20 03:52:19 +08:00
use anyhow::Result;
2022-11-14 01:26:33 +08:00
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
2022-04-20 01:44:47 +08:00
use std::sync::Arc;
2024-05-26 17:59:39 +08:00
use sysproxy::{Autoproxy, Sysproxy};
2024-04-13 15:45:50 +08:00
use tauri::async_runtime::Mutex as TokioMutex;
2024-11-20 03:52:19 +08:00
use tauri_plugin_autostart::ManagerExt;
use tokio::time::{sleep, Duration};
2022-04-20 01:44:47 +08:00
pub struct Sysopt {
update_sysproxy: Arc<TokioMutex<bool>>,
reset_sysproxy: Arc<TokioMutex<bool>>,
2022-11-12 11:37:23 +08:00
/// record whether the guard async is running or not
2024-10-10 18:52:20 +08:00
guard_state: Arc<Mutex<bool>>,
2022-04-20 01:44:47 +08:00
}
2022-09-11 20:58:55 +08:00
#[cfg(target_os = "windows")]
static DEFAULT_BYPASS: &str = "localhost;127.*;192.168.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;<local>";
2022-09-11 20:58:55 +08:00
#[cfg(target_os = "linux")]
static DEFAULT_BYPASS: &str =
"localhost,127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,172.29.0.0/16,::1";
2022-09-11 20:58:55 +08:00
#[cfg(target_os = "macos")]
2023-12-01 12:56:18 +08:00
static DEFAULT_BYPASS: &str =
"127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,172.29.0.0/16,localhost,*.local,*.crashlytics.com,<local>";
2022-09-11 20:58:55 +08:00
2024-06-09 12:45:57 +08:00
fn get_bypass() -> String {
let use_default = Config::verge().latest().use_default_bypass.unwrap_or(true);
2024-06-12 10:00:22 +08:00
let res = {
2024-06-09 12:45:57 +08:00
let verge = Config::verge();
let verge = verge.latest();
verge.system_proxy_bypass.clone()
2024-06-12 10:00:22 +08:00
};
let custom_bypass = match res {
2024-06-09 12:45:57 +08:00
Some(bypass) => bypass,
None => "".to_string(),
};
2024-10-05 02:58:41 +08:00
2024-10-10 18:40:39 +08:00
if custom_bypass.is_empty() {
DEFAULT_BYPASS.to_string()
2024-09-24 20:06:25 +08:00
} else if use_default {
format!("{},{}", DEFAULT_BYPASS, custom_bypass)
2024-06-09 12:45:57 +08:00
} else {
2024-09-24 20:06:25 +08:00
custom_bypass
2024-10-10 18:40:39 +08:00
}
2024-06-09 12:45:57 +08:00
}
2022-04-20 01:44:47 +08:00
impl Sysopt {
2022-11-14 01:26:33 +08:00
pub fn global() -> &'static Sysopt {
static SYSOPT: OnceCell<Sysopt> = OnceCell::new();
SYSOPT.get_or_init(|| Sysopt {
update_sysproxy: Arc::new(TokioMutex::new(false)),
reset_sysproxy: Arc::new(TokioMutex::new(false)),
2024-10-10 18:52:20 +08:00
guard_state: Arc::new(false.into()),
2022-11-14 01:26:33 +08:00
})
2022-04-20 01:44:47 +08:00
}
pub fn init_guard_sysproxy(&self) -> Result<()> {
self.guard_proxy();
Ok(())
}
2022-11-12 11:37:23 +08:00
/// init the sysproxy
2024-10-04 05:27:59 +08:00
pub async fn update_sysproxy(&self) -> Result<()> {
2024-10-10 18:52:20 +08:00
let _lock = self.update_sysproxy.lock().await;
2023-12-01 12:56:18 +08:00
let port = Config::verge()
.latest()
.verge_mixed_port
.unwrap_or(Config::clash().data().get_mixed_port());
2024-05-26 17:59:39 +08:00
let pac_port = IVerge::get_singleton_port();
2022-09-11 20:58:55 +08:00
let (sys_enable, pac_enable, proxy_host) = {
2022-11-16 01:26:41 +08:00
let verge = Config::verge();
let verge = verge.latest();
(
2024-01-10 17:36:35 +08:00
verge.enable_system_proxy.unwrap_or(false),
2024-05-26 17:59:39 +08:00
verge.proxy_auto_config.unwrap_or(false),
2025-04-17 15:20:12 +08:00
verge
.proxy_host
.clone()
.unwrap_or_else(|| String::from("127.0.0.1")),
2022-11-16 01:26:41 +08:00
)
};
#[cfg(not(target_os = "windows"))]
{
let mut sys = Sysproxy {
enable: false,
host: proxy_host.clone(),
port,
bypass: get_bypass(),
};
let mut auto = Autoproxy {
enable: false,
url: format!("http://{}:{}/commands/pac", proxy_host, pac_port),
};
2024-10-16 02:55:23 +08:00
if !sys_enable {
sys.set_system_proxy()?;
auto.set_auto_proxy()?;
return Ok(());
}
if pac_enable {
sys.enable = false;
auto.enable = true;
sys.set_system_proxy()?;
auto.set_auto_proxy()?;
return Ok(());
}
if sys_enable {
auto.enable = false;
sys.enable = true;
auto.set_auto_proxy()?;
sys.set_system_proxy()?;
return Ok(());
}
}
#[cfg(target_os = "windows")]
{
2024-10-16 02:55:23 +08:00
if !sys_enable {
2024-10-04 05:27:59 +08:00
return self.reset_sysproxy().await;
}
use crate::{core::handle::Handle, utils::dirs};
use anyhow::bail;
use tauri_plugin_shell::ShellExt;
let app_handle = Handle::global().app_handle().unwrap();
2024-10-03 14:31:40 +08:00
let binary_path = dirs::service_path()?;
let sysproxy_exe = binary_path.with_file_name("sysproxy.exe");
if !sysproxy_exe.exists() {
bail!("sysproxy.exe not found");
}
let shell = app_handle.shell();
2024-10-16 02:55:23 +08:00
let output = if pac_enable {
let address = format!("http://{}:{}/commands/pac", proxy_host, pac_port);
2024-10-04 05:27:59 +08:00
let output = shell
.command(sysproxy_exe.as_path().to_str().unwrap())
.args(["pac", address.as_str()])
2024-10-04 05:27:59 +08:00
.output()
.await
.unwrap();
output
} else {
let address = format!("{}:{}", proxy_host, port);
let bypass = get_bypass();
2024-10-04 05:27:59 +08:00
let output = shell
.command(sysproxy_exe.as_path().to_str().unwrap())
.args(["global", address.as_str(), bypass.as_ref()])
.output()
.await
.unwrap();
output
};
if !output.status.success() {
bail!("sysproxy exe run failed");
}
2022-11-12 11:37:23 +08:00
}
2022-04-20 01:44:47 +08:00
2022-11-12 11:37:23 +08:00
Ok(())
}
2022-11-12 11:37:23 +08:00
/// reset the sysproxy
2024-10-04 05:27:59 +08:00
pub async fn reset_sysproxy(&self) -> Result<()> {
2024-10-10 18:52:20 +08:00
let _lock = self.reset_sysproxy.lock().await;
//直接关闭所有代理
#[cfg(not(target_os = "windows"))]
{
let mut sysproxy: Sysproxy = Sysproxy::get_system_proxy()?;
let mut autoproxy = Autoproxy::get_auto_proxy()?;
sysproxy.enable = false;
autoproxy.enable = false;
autoproxy.set_auto_proxy()?;
sysproxy.set_system_proxy()?;
}
2022-11-12 11:37:23 +08:00
#[cfg(target_os = "windows")]
{
use crate::{core::handle::Handle, utils::dirs};
use anyhow::bail;
use tauri_plugin_shell::ShellExt;
let app_handle = Handle::global().app_handle().unwrap();
2024-10-03 14:31:40 +08:00
let binary_path = dirs::service_path()?;
let sysproxy_exe = binary_path.with_file_name("sysproxy.exe");
if !sysproxy_exe.exists() {
bail!("sysproxy.exe not found");
}
let shell = app_handle.shell();
2024-10-04 05:27:59 +08:00
let output = shell
.command(sysproxy_exe.as_path().to_str().unwrap())
.args(["set", "1"])
.output()
.await
.unwrap();
if !output.status.success() {
bail!("sysproxy exe run failed");
}
}
2024-05-26 19:07:14 +08:00
2022-11-12 11:37:23 +08:00
Ok(())
2022-04-20 01:44:47 +08:00
}
2022-11-12 11:37:23 +08:00
/// update the startup
2022-11-14 01:26:33 +08:00
pub fn update_launch(&self) -> Result<()> {
let enable_auto_launch = { Config::verge().latest().enable_auto_launch };
let is_enable = enable_auto_launch.unwrap_or(false);
logging!(info, true, "Setting auto-launch state to: {:?}", is_enable);
// 首先尝试使用快捷方式方法
#[cfg(target_os = "windows")]
{
if is_enable {
if let Err(e) = startup_shortcut::create_shortcut() {
log::error!(target: "app", "创建启动快捷方式失败: {}", e);
// 如果快捷方式创建失败,回退到原来的方法
self.try_original_autostart_method(is_enable);
} else {
return Ok(());
}
} else {
if let Err(e) = startup_shortcut::remove_shortcut() {
log::error!(target: "app", "删除启动快捷方式失败: {}", e);
self.try_original_autostart_method(is_enable);
} else {
return Ok(());
}
}
}
#[cfg(not(target_os = "windows"))]
{
// 非Windows平台使用原来的方法
self.try_original_autostart_method(is_enable);
}
Ok(())
}
/// 尝试使用原来的自启动方法
fn try_original_autostart_method(&self, is_enable: bool) {
2024-11-20 03:52:19 +08:00
let app_handle = Handle::global().app_handle().unwrap();
let autostart_manager = app_handle.autolaunch();
if is_enable {
logging_error!(Type::System, true, "{:?}", autostart_manager.enable());
} else {
logging_error!(Type::System, true, "{:?}", autostart_manager.disable());
}
2022-11-12 11:37:23 +08:00
}
2025-03-17 09:48:44 +08:00
/// 获取当前自启动的实际状态
pub fn get_launch_status(&self) -> Result<bool> {
// 首先尝试检查快捷方式是否存在
#[cfg(target_os = "windows")]
{
match startup_shortcut::is_shortcut_enabled() {
Ok(enabled) => {
log::info!(target: "app", "快捷方式自启动状态: {}", enabled);
return Ok(enabled);
}
Err(e) => {
log::error!(target: "app", "检查快捷方式失败,尝试原来的方法: {}", e);
}
}
}
// 回退到原来的方法
2025-03-17 09:48:44 +08:00
let app_handle = Handle::global().app_handle().unwrap();
let autostart_manager = app_handle.autolaunch();
2025-03-17 13:51:52 +08:00
match autostart_manager.is_enabled() {
Ok(status) => {
log::info!(target: "app", "Auto launch status: {}", status);
Ok(status)
}
2025-03-17 13:51:52 +08:00
Err(e) => {
log::error!(target: "app", "Failed to get auto launch status: {}", e);
Err(anyhow::anyhow!("Failed to get auto launch status: {}", e))
}
}
2025-03-17 09:48:44 +08:00
}
fn guard_proxy(&self) {
2024-10-10 18:52:20 +08:00
let _lock = self.guard_state.lock();
AsyncHandler::spawn(move || async move {
2022-11-12 11:37:23 +08:00
// default duration is 10s
let mut wait_secs = 10u64;
loop {
sleep(Duration::from_secs(wait_secs)).await;
let (enable, guard, guard_duration, pac, proxy_host) = {
2022-11-16 01:26:41 +08:00
let verge = Config::verge();
let verge = verge.latest();
(
2024-01-10 17:36:35 +08:00
verge.enable_system_proxy.unwrap_or(false),
verge.enable_proxy_guard.unwrap_or(false),
verge.proxy_guard_duration.unwrap_or(10),
2024-05-26 19:07:14 +08:00
verge.proxy_auto_config.unwrap_or(false),
2025-04-17 15:20:12 +08:00
verge
.proxy_host
.clone()
.unwrap_or_else(|| String::from("127.0.0.1")),
2022-11-16 01:26:41 +08:00
)
};
2022-11-12 11:37:23 +08:00
// stop loop
if !enable || !guard {
continue;
2022-11-12 11:37:23 +08:00
}
// update duration
wait_secs = guard_duration;
log::debug!(target: "app", "try to guard the system proxy");
// 获取期望的代理端口
let port = Config::verge()
.latest()
.verge_mixed_port
.unwrap_or(Config::clash().data().get_mixed_port());
2024-05-26 19:07:14 +08:00
let pac_port = IVerge::get_singleton_port();
let bypass = get_bypass();
2024-10-04 05:37:53 +08:00
// 检查系统代理配置
if pac {
// 检查 PAC 代理设置
let expected_url = format!("http://{}:{}/commands/pac", proxy_host, pac_port);
let autoproxy = match Autoproxy::get_auto_proxy() {
Ok(ap) => ap,
Err(e) => {
log::error!(target: "app", "failed to get the auto proxy: {}", e);
continue;
}
};
2024-10-04 05:37:53 +08:00
// 检查自动代理是否启用且URL是否正确
if !autoproxy.enable || autoproxy.url != expected_url {
log::info!(target: "app", "auto proxy settings changed, restoring...");
#[cfg(not(target_os = "windows"))]
{
let new_autoproxy = Autoproxy {
enable: true,
url: expected_url,
};
logging_error!(Type::System, true, new_autoproxy.set_auto_proxy());
}
#[cfg(target_os = "windows")]
{
use crate::{core::handle::Handle, utils::dirs};
use tauri_plugin_shell::ShellExt;
let app_handle = Handle::global().app_handle().unwrap();
let binary_path = match dirs::service_path() {
Ok(path) => path,
Err(e) => {
log::error!(target: "app", "failed to get service path: {}", e);
continue;
}
};
let sysproxy_exe = binary_path.with_file_name("sysproxy.exe");
if !sysproxy_exe.exists() {
log::error!(target: "app", "sysproxy.exe not found");
continue;
}
let shell = app_handle.shell();
let output = shell
.command(sysproxy_exe.as_path().to_str().unwrap())
.args(["pac", expected_url.as_str()])
.output()
.await
.unwrap();
if !output.status.success() {
log::error!(target: "app", "failed to set auto proxy");
}
}
2024-10-04 05:37:53 +08:00
}
} else {
// 检查常规系统代理设置
let sysproxy = match Sysproxy::get_system_proxy() {
Ok(sp) => sp,
Err(e) => {
log::error!(target: "app", "failed to get the system proxy: {}", e);
continue;
}
2024-10-04 05:37:53 +08:00
};
// 检查系统代理是否启用且配置是否匹配
if !sysproxy.enable || sysproxy.host != proxy_host || sysproxy.port != port {
log::info!(target: "app", "system proxy settings changed, restoring...");
#[cfg(not(target_os = "windows"))]
{
let new_sysproxy = Sysproxy {
enable: true,
host: proxy_host.clone(),
port,
bypass: bypass.clone(),
};
logging_error!(Type::System, true, new_sysproxy.set_system_proxy());
}
#[cfg(target_os = "windows")]
{
use crate::{core::handle::Handle, utils::dirs};
use tauri_plugin_shell::ShellExt;
let app_handle = Handle::global().app_handle().unwrap();
let binary_path = match dirs::service_path() {
Ok(path) => path,
Err(e) => {
log::error!(target: "app", "failed to get service path: {}", e);
continue;
}
};
let sysproxy_exe = binary_path.with_file_name("sysproxy.exe");
if !sysproxy_exe.exists() {
log::error!(target: "app", "sysproxy.exe not found");
continue;
}
let address = format!("{}:{}", proxy_host, port);
let shell = app_handle.shell();
let output = shell
.command(sysproxy_exe.as_path().to_str().unwrap())
.args(["global", address.as_str(), bypass.as_ref()])
.output()
.await
.unwrap();
if !output.status.success() {
log::error!(target: "app", "failed to set system proxy");
}
}
2024-10-04 05:37:53 +08:00
}
}
2022-11-12 11:37:23 +08:00
}
});
}
2022-04-20 01:44:47 +08:00
}