From f78d96c898c4709c545d3f89aacad4f504966622 Mon Sep 17 00:00:00 2001 From: Richard Liu <1625351+richardzone@users.noreply.github.com> Date: Mon, 1 Jun 2026 15:56:04 +0800 Subject: [PATCH] fix: isolate profile daemon pipe --- src/cli/transport.rs | 7 +++++-- src/config.rs | 8 ++++++++ src/daemon/server.rs | 8 ++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/cli/transport.rs b/src/cli/transport.rs index 23c3e18..593da6a 100644 --- a/src/cli/transport.rs +++ b/src/cli/transport.rs @@ -257,7 +257,8 @@ fn ping_unix() -> Result { fn ping_windows() -> Result { use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream}; - let name = "wx-cli-daemon".to_ns_name::()?; + let pipe_name = config::local_socket_name(); + let name = pipe_name.as_str().to_ns_name::()?; let stream = Stream::connect(name)?; let mut reader = BufReader::new(stream); @@ -468,7 +469,9 @@ fn send_unix(req: Request) -> Result { fn send_windows(req: Request) -> Result { use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream}; - let name = "wx-cli-daemon" + let pipe_name = config::local_socket_name(); + let name = pipe_name + .as_str() .to_ns_name::() .context("构造 pipe name 失败")?; let stream = Stream::connect(name).context("连接 daemon named pipe 失败")?; diff --git a/src/config.rs b/src/config.rs index cca9b78..5e38bab 100644 --- a/src/config.rs +++ b/src/config.rs @@ -249,6 +249,14 @@ pub fn log_path() -> PathBuf { cli_dir().join("daemon.log") } +#[allow(dead_code)] +pub fn local_socket_name() -> String { + match active_profile() { + Some(profile) => format!("wx-cli-daemon-{}", profile), + None => "wx-cli-daemon".to_string(), + } +} + pub fn cache_dir() -> PathBuf { cli_dir().join("cache") } diff --git a/src/daemon/server.rs b/src/daemon/server.rs index 242edc1..1a4d440 100644 --- a/src/daemon/server.rs +++ b/src/daemon/server.rs @@ -84,13 +84,13 @@ async fn serve_windows( ) -> Result<()> { use interprocess::local_socket::{tokio::prelude::*, GenericNamespaced, ListenerOptions}; - // interprocess 的 GenericNamespaced 在 Windows 上会自动拼接 `\\.\pipe\` 前缀, - // 这里必须传相对名;client 端用 `\\.\pipe\wx-cli-daemon` 直接打开可以对上 - let name = "wx-cli-daemon".to_ns_name::()?; + // interprocess 的 GenericNamespaced 在 Windows 上会自动拼接 `\\.\pipe\` 前缀。 + let pipe_name = crate::config::local_socket_name(); + let name = pipe_name.as_str().to_ns_name::()?; let opts = ListenerOptions::new().name(name); let listener = opts.create_tokio()?; - eprintln!("[server] 监听 \\\\.\\pipe\\wx-cli-daemon"); + eprintln!("[server] 监听 \\\\.\\pipe\\{}", pipe_name); loop { let conn = listener.accept().await?;