fix: isolate profile daemon pipe

pull/93/head
Richard Liu 2026-06-01 15:56:04 +08:00
parent 4028473ec4
commit f78d96c898
3 changed files with 17 additions and 6 deletions

View File

@ -257,7 +257,8 @@ fn ping_unix() -> Result<bool> {
fn ping_windows() -> Result<bool> { fn ping_windows() -> Result<bool> {
use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream}; use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream};
let name = "wx-cli-daemon".to_ns_name::<GenericNamespaced>()?; let pipe_name = config::local_socket_name();
let name = pipe_name.as_str().to_ns_name::<GenericNamespaced>()?;
let stream = Stream::connect(name)?; let stream = Stream::connect(name)?;
let mut reader = BufReader::new(stream); let mut reader = BufReader::new(stream);
@ -468,7 +469,9 @@ fn send_unix(req: Request) -> Result<Response> {
fn send_windows(req: Request) -> Result<Response> { fn send_windows(req: Request) -> Result<Response> {
use interprocess::local_socket::{prelude::*, GenericNamespaced, Stream}; 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::<GenericNamespaced>() .to_ns_name::<GenericNamespaced>()
.context("构造 pipe name 失败")?; .context("构造 pipe name 失败")?;
let stream = Stream::connect(name).context("连接 daemon named pipe 失败")?; let stream = Stream::connect(name).context("连接 daemon named pipe 失败")?;

View File

@ -249,6 +249,14 @@ pub fn log_path() -> PathBuf {
cli_dir().join("daemon.log") 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 { pub fn cache_dir() -> PathBuf {
cli_dir().join("cache") cli_dir().join("cache")
} }

View File

@ -84,13 +84,13 @@ async fn serve_windows(
) -> Result<()> { ) -> Result<()> {
use interprocess::local_socket::{tokio::prelude::*, GenericNamespaced, ListenerOptions}; use interprocess::local_socket::{tokio::prelude::*, GenericNamespaced, ListenerOptions};
// interprocess 的 GenericNamespaced 在 Windows 上会自动拼接 `\\.\pipe\` 前缀 // interprocess 的 GenericNamespaced 在 Windows 上会自动拼接 `\\.\pipe\` 前缀
// 这里必须传相对名client 端用 `\\.\pipe\wx-cli-daemon` 直接打开可以对上 let pipe_name = crate::config::local_socket_name();
let name = "wx-cli-daemon".to_ns_name::<GenericNamespaced>()?; let name = pipe_name.as_str().to_ns_name::<GenericNamespaced>()?;
let opts = ListenerOptions::new().name(name); let opts = ListenerOptions::new().name(name);
let listener = opts.create_tokio()?; let listener = opts.create_tokio()?;
eprintln!("[server] 监听 \\\\.\\pipe\\wx-cli-daemon"); eprintln!("[server] 监听 \\\\.\\pipe\\{}", pipe_name);
loop { loop {
let conn = listener.accept().await?; let conn = listener.accept().await?;