1
0
Fork 0

Refactor session

This commit is contained in:
Florian RICHER 2023-01-31 20:59:14 +01:00
parent 2b028c694f
commit a18fec21cd
6 changed files with 47 additions and 26 deletions

View file

@ -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;
}

22
src/command.rs Normal file
View file

@ -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<String, String>,
pub args: Vec<String>
}
impl Into<std::process::Command> 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
}
}

View file

@ -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<Response<AuthorizeResponse>, 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))
}

View file

@ -19,7 +19,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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<AuthorizeResponse> = client.authorize(request).await?;

View file

@ -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<String, String>,
pub args: Vec<String>
}
impl Into<std::process::Command> 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
}
}

13
src/session.rs Normal file
View file

@ -0,0 +1,13 @@
pub struct Session {
pub id: String,
pub pid: usize,
}
impl From<usize> for Session {
fn from(pid: usize) -> Self {
Self {
id: format!("ID{pid}"),
pid
}
}
}