entrust_agent/
client.rs

1use crate::server::{GetAgeIdentityResponse, Request};
2use crate::{SOCKET_NAME, read_deserialized, send_serialized};
3use interprocess::local_socket::prelude::*;
4use interprocess::local_socket::{GenericFilePath, GenericNamespaced, Stream};
5use std::io;
6use std::io::BufReader;
7
8pub fn set_age_identity(identity: String, pin: Option<String>) -> io::Result<()> {
9    let request = Request::SetAgeIdentity { identity, pin };
10    let mut con = connect()?;
11    send_serialized(&request, con.get_mut())
12}
13
14pub fn get_age_identity(pin: Option<String>) -> io::Result<GetAgeIdentityResponse> {
15    let request = Request::GetAgeIdentity { pin };
16    let mut con = connect()?;
17    send_serialized(&request, con.get_mut())?;
18    let response: GetAgeIdentityResponse = read_deserialized(&mut con)?;
19    Ok(response)
20}
21
22pub fn shutdown_server() -> io::Result<()> {
23    let mut con = connect()?;
24    send_serialized(&Request::Shutdown, con.get_mut())
25}
26
27pub fn is_server_running() -> bool {
28    get_age_identity(None).is_ok()
29}
30
31fn connect() -> io::Result<BufReader<Stream>> {
32    let socket_name = if GenericNamespaced::is_supported() {
33        SOCKET_NAME.as_ref().to_ns_name::<GenericNamespaced>()?
34    } else {
35        format!("/tmp/{}", SOCKET_NAME.as_ref()).to_fs_name::<GenericFilePath>()?
36    };
37    Stream::connect(socket_name).map(BufReader::new)
38}