use std::path::Path;
use anyhow::{Context, bail};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use super::protocol::{IpcRequest, IpcResponse};
use super::transport::{self, default_ipc_path};
pub struct IpcClient {
path: std::path::PathBuf,
}
impl IpcClient {
pub fn connect() -> Self {
Self {
path: default_ipc_path(),
}
}
pub fn agent() -> Self {
Self::connect()
}
pub fn with_path(path: impl Into<std::path::PathBuf>) -> Self {
Self { path: path.into() }
}
pub fn path(&self) -> &Path {
&self.path
}
pub async fn request(&self, req: IpcRequest) -> anyhow::Result<IpcResponse> {
let stream = transport::connect(&self.path).await.with_context(|| {
format!(
"cannot connect to agent IPC at {} - is the agent running?",
self.path.display()
)
})?;
let (read, mut write) = stream.split();
let mut buf = serde_json::to_vec(&req)?;
buf.push(b'\n');
write.write_all(&buf).await?;
write.flush().await?;
let mut reader = BufReader::new(read);
let mut line = String::new();
let n = reader.read_line(&mut line).await?;
if n == 0 {
bail!("agent closed IPC connection without a response");
}
let resp: IpcResponse = serde_json::from_str(line.trim())
.with_context(|| format!("bad IPC response: {}", line.trim()))?;
if let IpcResponse::Error { message } = &resp {
bail!("{message}");
}
Ok(resp)
}
pub async fn request_stream(
&self,
req: IpcRequest,
mut on_response: impl FnMut(IpcResponse) -> anyhow::Result<()>,
) -> anyhow::Result<()> {
let stream = transport::connect(&self.path).await.with_context(|| {
format!(
"cannot connect to agent IPC at {} - is the agent running?",
self.path.display()
)
})?;
let (read, mut write) = stream.split();
let mut buf = serde_json::to_vec(&req)?;
buf.push(b'\n');
write.write_all(&buf).await?;
write.flush().await?;
let mut reader = BufReader::new(read);
loop {
let mut line = String::new();
let n = reader.read_line(&mut line).await?;
if n == 0 {
break;
}
let resp: IpcResponse = serde_json::from_str(line.trim())
.with_context(|| format!("bad IPC response: {}", line.trim()))?;
let is_summary = matches!(&resp, IpcResponse::PingSummary(_));
on_response(resp)?;
if is_summary {
break;
}
}
Ok(())
}
}
pub fn discover_agent_state(
state_dir: Option<&str>,
) -> anyhow::Result<crate::state::PersistedState> {
let paths = crate::state::StatePaths::resolve(state_dir);
crate::state::PersistedState::try_load(&paths)?.with_context(|| {
format!(
"not connected to a network yet (no state in {}). \
Use `tunnet create` for Direct or `tunnet enroll` for Managed",
paths.dir.display()
)
})
}
pub fn discover_network_id(
state_dir: Option<&str>,
) -> anyhow::Result<(uuid::Uuid, crate::state::PersistedState)> {
let persisted = discover_agent_state(state_dir)?;
Ok((
persisted
.primary_network_id()
.context("persisted state has no network id")?,
persisted,
))
}