mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-06 23:00:15 +08:00
feat: add launch guard and secure multi-user service ownership (#7569)
* feat(service): isolate macOS launch and owner runtime * ci(windows): run owner security tests * chore(deps): use kode-bridge v0.4.1 tag * fix(macos): classify installed service state * fix(macos): classify marked unavailable service * fix(macos): gate core startup on service migration * fix(macos): scope service init to supported platforms * feat(macos): prompt before migrating installed service * fix(macos): shorten sidecar IPC path * build(service): pin helper protocol and artifacts * fix(core): require confirmed service and sidecar readiness * fix(macos): harden launch roots and singleton state * fix(ui): refresh migration state before restart * fix(macos): linearize service migration lifecycle * fix(ui): keep service migration state fresh * feat(service): track authenticated owner sessions * fix(service): preserve newer owner sessions * feat(macos): route service proxy changes through helper * fix(macos): serialize service proxy guard * fix(macos): close service proxy owner lifecycle * fix(macos): close remaining proxy stop races * fix(app): do not cancel controlled service stop * fix(lifecycle): keep controlled updates cancellation-safe * fix(macos): stage dev core for service * test(macos): exercise staged core cleanup * fix(dev): await controlled macOS exit * chore: update changelog * fix(macos): recover sidecar after service install failure Allow the existing sidecar to continue when service installation leaves the manager in InstallRequired, then settle the session state as SidecarAllowed. Show neutral status text while the migration dialog waits for its final state. * fix(macos): stage dev service tools before elevation * fix(macos): localize launch guard prompts * fix(service): address cross-platform integration issues * feat: support seamless service migration * fix: pass Windows service installer path safely * fix(prebuild): derive service release from Cargo dependency * fix(dev): preserve service installation state * fix: resolve clippy warnings in service lifecycle
This commit is contained in:
+189
-31
@@ -3,8 +3,6 @@ use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use clash_verge_logging::{Type, logging};
|
||||
use once_cell::sync::OnceCell;
|
||||
#[cfg(unix)]
|
||||
use std::iter;
|
||||
use std::{fs, path::PathBuf};
|
||||
use tauri::Manager as _;
|
||||
|
||||
@@ -66,6 +64,28 @@ pub fn app_home_dir() -> Result<PathBuf> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn preinit_app_data_dir() -> Result<PathBuf> {
|
||||
if PORTABLE_FLAG.get().copied().unwrap_or(false) {
|
||||
let executable = std::env::current_exe()?;
|
||||
let parent = executable
|
||||
.parent()
|
||||
.ok_or_else(|| anyhow::anyhow!("portable executable has no parent directory"))?;
|
||||
return Ok(parent.join(".config").join(APP_ID));
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
let root = PathBuf::from(std::env::var_os("HOME").ok_or_else(|| anyhow::anyhow!("HOME is unavailable"))?)
|
||||
.join("Library/Application Support");
|
||||
#[cfg(target_os = "linux")]
|
||||
let root = std::env::var_os("XDG_DATA_HOME")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| PathBuf::from(std::env::var_os("HOME").unwrap_or_default()).join(".local/share"));
|
||||
#[cfg(windows)]
|
||||
let root = PathBuf::from(std::env::var_os("APPDATA").ok_or_else(|| anyhow::anyhow!("APPDATA is unavailable"))?);
|
||||
|
||||
Ok(root.join(APP_ID))
|
||||
}
|
||||
|
||||
/// get the resources dir
|
||||
pub fn app_resources_dir() -> Result<PathBuf> {
|
||||
// 避免在Handle未初始化时崩溃
|
||||
@@ -213,40 +233,155 @@ pub fn get_encryption_key() -> Result<Vec<u8>> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub fn ensure_mihomo_safe_dir() -> Option<PathBuf> {
|
||||
iter::once("/tmp")
|
||||
.map(PathBuf::from)
|
||||
.find(|path| path.exists())
|
||||
.or_else(|| {
|
||||
std::env::var_os("HOME").and_then(|home| {
|
||||
let home_config = PathBuf::from(home).join(".config");
|
||||
if home_config.exists() || fs::create_dir_all(&home_config).is_ok() {
|
||||
Some(home_config)
|
||||
} else {
|
||||
logging!(error, Type::File, "Failed to create safe directory: {home_config:?}");
|
||||
None
|
||||
}
|
||||
})
|
||||
})
|
||||
pub fn ipc_path() -> Result<PathBuf> {
|
||||
Ok(PathBuf::from(clash_verge_service_ipc::mihomo_ipc_path(
|
||||
&crate::core::owner_identity::current_owner_identity()?,
|
||||
)))
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub fn ipc_path() -> Result<PathBuf> {
|
||||
ensure_mihomo_safe_dir()
|
||||
.map(|base_dir| base_dir.join("verge").join("verge-mihomo.sock"))
|
||||
.or_else(|| {
|
||||
app_home_dir()
|
||||
.ok()
|
||||
.map(|dir| dir.join("verge").join("verge-mihomo.sock"))
|
||||
})
|
||||
.ok_or_else(|| anyhow::anyhow!("Failed to determine ipc path"))
|
||||
#[cfg(target_os = "macos")]
|
||||
pub fn sidecar_ipc_path() -> Result<PathBuf> {
|
||||
sidecar_ipc_path_for(
|
||||
std::path::Path::new(""),
|
||||
&crate::core::owner_identity::current_owner_identity()?,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn ipc_path() -> Result<PathBuf> {
|
||||
Ok(PathBuf::from(r"\\.\pipe\verge-mihomo"))
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
pub fn sidecar_ipc_path() -> Result<PathBuf> {
|
||||
Ok(sidecar_ipc_path_for(
|
||||
&preinit_app_data_dir()?,
|
||||
&crate::core::owner_identity::current_owner_identity()?,
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn sidecar_ipc_path_for(app_root: &std::path::Path, _identity: &clash_verge_service_ipc::OwnerIdentity) -> PathBuf {
|
||||
app_root.join("verge-mihomo.sock")
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn sidecar_ipc_path_for(
|
||||
_app_root: &std::path::Path,
|
||||
_identity: &clash_verge_service_ipc::OwnerIdentity,
|
||||
) -> Result<PathBuf> {
|
||||
use std::{ffi::OsStr, os::unix::ffi::OsStrExt as _};
|
||||
|
||||
// SAFETY: A null buffer with size zero asks confstr for the required buffer length.
|
||||
let required_len = unsafe { libc::confstr(libc::_CS_DARWIN_USER_TEMP_DIR, std::ptr::null_mut(), 0) };
|
||||
if required_len == 0 {
|
||||
return Err(anyhow::anyhow!("macOS per-user temporary directory is unavailable"));
|
||||
}
|
||||
|
||||
let mut buffer = vec![0_u8; required_len];
|
||||
// SAFETY: buffer is writable for buffer.len() bytes, as required by confstr.
|
||||
let written = unsafe { libc::confstr(libc::_CS_DARWIN_USER_TEMP_DIR, buffer.as_mut_ptr().cast(), buffer.len()) };
|
||||
if written == 0 || written > buffer.len() {
|
||||
return Err(anyhow::anyhow!("failed to read macOS per-user temporary directory"));
|
||||
}
|
||||
|
||||
let root = std::ffi::CStr::from_bytes_until_nul(&buffer)
|
||||
.map_err(|_| anyhow::anyhow!("macOS per-user temporary directory is not NUL-terminated"))?;
|
||||
#[cfg(feature = "verge-dev")]
|
||||
let filename = "verge-mihomo-dev.sock";
|
||||
#[cfg(not(feature = "verge-dev"))]
|
||||
let filename = "verge-mihomo.sock";
|
||||
let path = PathBuf::from(OsStr::from_bytes(root.to_bytes())).join(filename);
|
||||
|
||||
let path_len = path.as_os_str().as_bytes().len();
|
||||
if path_len >= 104 {
|
||||
return Err(anyhow::anyhow!(
|
||||
"macOS Sidecar IPC path is {path_len} bytes, but sockaddr_un.sun_path requires fewer than 104 bytes: {:?}",
|
||||
path,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn sidecar_ipc_path_for(_app_root: &std::path::Path, identity: &clash_verge_service_ipc::OwnerIdentity) -> PathBuf {
|
||||
PathBuf::from(sidecar_pipe_name(identity, cfg!(feature = "verge-dev")))
|
||||
}
|
||||
|
||||
#[cfg(any(windows, test))]
|
||||
fn sidecar_pipe_name(identity: &clash_verge_service_ipc::OwnerIdentity, is_dev: bool) -> String {
|
||||
let flavor = if is_dev { "dev" } else { "release" };
|
||||
format!(
|
||||
r"\\.\pipe\verge-mihomo-sidecar-{flavor}-{}",
|
||||
clash_verge_service_ipc::owner_key(identity)
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(all(test, target_os = "linux"))]
|
||||
mod ipc_tests {
|
||||
use super::sidecar_ipc_path_for;
|
||||
use clash_verge_service_ipc::OwnerIdentity;
|
||||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
fn sidecar_ipc_stays_in_the_app_root() {
|
||||
let identity = OwnerIdentity::Unix { uid: 501, gid: 20 };
|
||||
let app_root = Path::new("/home/test/.local/share/io.github.clash-verge-rev.clash-verge-rev");
|
||||
let path = sidecar_ipc_path_for(app_root, &identity);
|
||||
|
||||
assert_eq!(path, app_root.join("verge-mihomo.sock"));
|
||||
assert_ne!(
|
||||
path.to_string_lossy(),
|
||||
clash_verge_service_ipc::mihomo_ipc_path(&identity)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, target_os = "macos"))]
|
||||
mod ipc_tests {
|
||||
use super::sidecar_ipc_path_for;
|
||||
use clash_verge_service_ipc::OwnerIdentity;
|
||||
use std::{ffi::OsStr, os::unix::ffi::OsStrExt as _, path::Path};
|
||||
|
||||
#[test]
|
||||
fn sidecar_ipc_ignores_long_app_root_and_fits_sockaddr_un() -> anyhow::Result<()> {
|
||||
let identity = OwnerIdentity::Unix { uid: 501, gid: 20 };
|
||||
let app_root =
|
||||
Path::new("/Users/support/Library/Application Support/io.github.clash-verge-rev.clash-verge-rev.dev");
|
||||
let path = sidecar_ipc_path_for(app_root, &identity)?;
|
||||
|
||||
assert!(!path.starts_with(app_root));
|
||||
assert!(path.as_os_str().as_bytes().len() < 104);
|
||||
#[cfg(feature = "verge-dev")]
|
||||
assert_eq!(path.file_name(), Some(OsStr::new("verge-mihomo-dev.sock")));
|
||||
#[cfg(not(feature = "verge-dev"))]
|
||||
assert_eq!(path.file_name(), Some(OsStr::new("verge-mihomo.sock")));
|
||||
assert_eq!(path, sidecar_ipc_path_for(Path::new("/different/root"), &identity)?);
|
||||
assert!(path.parent().is_some_and(Path::is_dir));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, windows))]
|
||||
mod ipc_tests {
|
||||
use super::sidecar_ipc_path_for;
|
||||
use clash_verge_service_ipc::OwnerIdentity;
|
||||
use std::path::Path;
|
||||
|
||||
#[test]
|
||||
fn sidecar_ipc_uses_the_current_owners_named_pipe() {
|
||||
let identity = OwnerIdentity::Windows {
|
||||
sid: "S-1-5-21-1000".to_owned(),
|
||||
};
|
||||
let path = sidecar_ipc_path_for(Path::new(r"C:\ignored"), &identity);
|
||||
|
||||
assert_eq!(
|
||||
path,
|
||||
Path::new(&format!(
|
||||
r"\\.\pipe\verge-mihomo-sidecar-{}-{}",
|
||||
if cfg!(feature = "verge-dev") { "dev" } else { "release" },
|
||||
clash_verge_service_ipc::owner_key(&identity)
|
||||
))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait PathBufExec {
|
||||
async fn remove_if_exists(&self) -> Result<()>;
|
||||
@@ -262,3 +397,26 @@ impl PathBufExec for PathBuf {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod windows_pipe_name_tests {
|
||||
use super::sidecar_pipe_name;
|
||||
use clash_verge_service_ipc::OwnerIdentity;
|
||||
|
||||
#[test]
|
||||
fn windows_sidecar_pipe_separates_dev_and_release_for_the_same_owner() {
|
||||
let identity = OwnerIdentity::Windows {
|
||||
sid: "S-1-5-21-1000".to_owned(),
|
||||
};
|
||||
let owner_key = clash_verge_service_ipc::owner_key(&identity);
|
||||
|
||||
assert_eq!(
|
||||
sidecar_pipe_name(&identity, false),
|
||||
format!(r"\\.\pipe\verge-mihomo-sidecar-release-{owner_key}")
|
||||
);
|
||||
assert_eq!(
|
||||
sidecar_pipe_name(&identity, true),
|
||||
format!(r"\\.\pipe\verge-mihomo-sidecar-dev-{owner_key}")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user