diff --git a/src/client/client.rs b/src/client/client.rs new file mode 100644 index 0000000..880d666 --- /dev/null +++ b/src/client/client.rs @@ -0,0 +1,14 @@ +use libcommand::internal::unix_client::UnixClient; + +#[cfg(unix)] +use tokio::net::UnixStream; +use tonic::transport::{Channel, Endpoint, Uri}; +use tower::service_fn; + +pub(super) async fn connect() -> Result, Box> { + let channel = Endpoint::try_from("http://[::]:50051")? + .connect_with_connector(service_fn(|_: Uri| UnixStream::connect(libcommand::SOCK_FILE))) + .await?; + + Ok(UnixClient::new(channel)) +} diff --git a/src/client/main.rs b/src/client/main.rs index d9707fa..7631fd8 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -1,26 +1,13 @@ #![cfg_attr(not(unix), allow(unused_imports))] -pub mod internal { - tonic::include_proto!("internal"); -} +pub mod client; -use internal::{unix_client::UnixClient, AuthorizeRequest}; -#[cfg(unix)] -use tokio::net::UnixStream; -use tonic::transport::{Endpoint, Uri}; -use tower::service_fn; +use libcommand::internal::{AuthorizeRequest}; #[cfg(unix)] #[tokio::main] async fn main() -> Result<(), Box> { - let channel = Endpoint::try_from("http://[::]:50051")? - .connect_with_connector(service_fn(|_: Uri| { - // Connect to a Uds socket - UnixStream::connect(libcommand::SOCK_FILE) - })) - .await?; - - let mut client = UnixClient::new(channel); + let mut client = client::connect().await?; let request = tonic::Request::new(AuthorizeRequest { identifier: "Tonic".into(), diff --git a/src/daemon/main.rs b/src/daemon/main.rs index 6a517b3..30a6f67 100644 --- a/src/daemon/main.rs +++ b/src/daemon/main.rs @@ -9,11 +9,7 @@ use tokio_stream::wrappers::UnixListenerStream; use tonic::transport::server::UdsConnectInfo; use tonic::{transport::Server, Request, Response, Status}; -pub mod internal { - tonic::include_proto!("internal"); -} - -use internal::{ +use libcommand::internal::{ unix_server::{Unix, UnixServer}, AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse }; @@ -33,8 +29,8 @@ impl Unix for DaemonServer { println!("Got a request {:?} with info {:?}", request, conn_info); } - let reply = internal::AuthorizeResponse { - status: internal::AuthorizationStatus::Authorized.into(), + let reply = libcommand::internal::AuthorizeResponse { + status: libcommand::internal::AuthorizationStatus::Authorized.into(), error_message: "".into(), log_file: "".into(), session_uuid: "".into() @@ -52,8 +48,8 @@ impl Unix for DaemonServer { println!("Got a request {:?} with info {:?}", request, conn_info); } - let reply = internal::TerminateResponse { - status: internal::TerminateStatus::Ok.into(), + let reply = libcommand::internal::TerminateResponse { + status: libcommand::internal::TerminateStatus::Ok.into(), error_message: "".into(), }; Ok(Response::new(reply)) diff --git a/src/lib.rs b/src/lib.rs index aa72d3b..300937a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,24 @@ +use std::collections::HashMap; use serde::{Serialize, Deserialize}; pub const SOCK_FILE : &'static str = "command_gateway.sock"; +pub mod internal { + tonic::include_proto!("internal"); +} + #[derive(Serialize, Deserialize, Debug)] pub struct Command { + command: String, + envs: HashMap, + args: Vec +} +impl Into for Command { + fn into(self) -> std::process::Command { + let mut command = std::process::Command::new(self.command); + command.args(self.args); + command.envs(self.envs); + command + } } \ No newline at end of file