From a18fec21cdc22d6756e4cb6b3287f15d5ac0b50b Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Tue, 31 Jan 2023 20:59:14 +0100 Subject: [PATCH] Refactor session --- proto/interpreter.proto | 6 ++++-- src/command.rs | 22 ++++++++++++++++++++++ src/daemon/server.rs | 4 ++-- src/interpreter/main.rs | 3 ++- src/lib.rs | 25 ++++--------------------- src/session.rs | 13 +++++++++++++ 6 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 src/command.rs create mode 100644 src/session.rs diff --git a/proto/interpreter.proto b/proto/interpreter.proto index f5ea845..46b1871 100644 --- a/proto/interpreter.proto +++ b/proto/interpreter.proto @@ -16,6 +16,8 @@ message AuthorizeRequest { string token = 2; // command like /bin/bash string command = 3; + // pid + uint32 pid = 4; } enum AuthorizationStatus { @@ -25,11 +27,11 @@ enum AuthorizationStatus { message AuthorizeResponse { AuthorizationStatus status = 1; - string session_uuid = 2; + string session_id = 2; } message TerminateRequest { - string session_uuid = 1; + string session_id = 1; string log_file = 2; } diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000..6c09cd3 --- /dev/null +++ b/src/command.rs @@ -0,0 +1,22 @@ +use std::collections::HashMap; +use serde::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Command { + pub identifier: String, + pub token: String, + pub command: String, + pub envs: HashMap, + pub args: Vec +} + +impl Into for Command { + fn into(self) -> std::process::Command { + let mut command = std::process::Command::new("/usr/bin/script"); + command.env_clear(); + let final_command = format!("{} {}", self.command, self.args.join(" ")); + command.args(vec!["-q", "-f", "session.log", "-c", &final_command]); + command.envs(self.envs); + command + } +} \ No newline at end of file diff --git a/src/daemon/server.rs b/src/daemon/server.rs index 7684caf..c18c6b7 100644 --- a/src/daemon/server.rs +++ b/src/daemon/server.rs @@ -9,7 +9,7 @@ use libcommand::interpreter::{ }; #[derive(Default)] -pub struct DaemonServer {} +pub struct DaemonServer; #[tonic::async_trait] impl Unix for DaemonServer { @@ -19,7 +19,7 @@ impl Unix for DaemonServer { ) -> Result, Status> { let reply = AuthorizeResponse { status: AuthorizationStatus::Authorized.into(), - session_uuid: uuid::Uuid::new_v4().to_string() + session_id: uuid::Uuid::new_v4().to_string() }; Ok(Response::new(reply)) } diff --git a/src/interpreter/main.rs b/src/interpreter/main.rs index 4c64669..3529a04 100644 --- a/src/interpreter/main.rs +++ b/src/interpreter/main.rs @@ -19,7 +19,8 @@ async fn main() -> Result<(), Box> { let request = tonic::Request::new(AuthorizeRequest { identifier: command_arg.identifier.clone(), token: command_arg.token.clone(), - command: command_arg.command.clone() + command: command_arg.command.clone(), + pid: std::process::id() }); let response : Response = client.authorize(request).await?; diff --git a/src/lib.rs b/src/lib.rs index 323eb02..3ea43e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ -use std::collections::HashMap; -use serde::{Serialize, Deserialize}; +mod command; +mod session; + +pub use command::Command; pub const SOCK_FILE : &'static str = "/var/run/command_gateway.sock"; @@ -7,22 +9,3 @@ pub mod interpreter { tonic::include_proto!("interpreter"); } -#[derive(Serialize, Deserialize, Debug)] -pub struct Command { - pub identifier: String, - pub token: String, - pub command: String, - pub envs: HashMap, - pub args: Vec -} - -impl Into for Command { - fn into(self) -> std::process::Command { - let mut command = std::process::Command::new("/usr/bin/script"); - command.env_clear(); - let final_command = format!("{} {}", self.command, self.args.join(" ")); - command.args(vec!["-q", "-f", "session.log", "-c", &final_command]); - command.envs(self.envs); - command - } -} \ No newline at end of file diff --git a/src/session.rs b/src/session.rs new file mode 100644 index 0000000..6e84174 --- /dev/null +++ b/src/session.rs @@ -0,0 +1,13 @@ +pub struct Session { + pub id: String, + pub pid: usize, +} + +impl From for Session { + fn from(pid: usize) -> Self { + Self { + id: format!("ID{pid}"), + pid + } + } +} \ No newline at end of file