Add session manager
This commit is contained in:
parent
a18fec21cd
commit
0e1a2dc817
7 changed files with 58 additions and 41 deletions
|
@ -20,13 +20,7 @@ message AuthorizeRequest {
|
|||
uint32 pid = 4;
|
||||
}
|
||||
|
||||
enum AuthorizationStatus {
|
||||
AUTHORIZED = 0;
|
||||
PERMISSION_DENIED = 1;
|
||||
}
|
||||
|
||||
message AuthorizeResponse {
|
||||
AuthorizationStatus status = 1;
|
||||
string session_id = 2;
|
||||
}
|
||||
|
||||
|
@ -35,11 +29,4 @@ message TerminateRequest {
|
|||
string log_file = 2;
|
||||
}
|
||||
|
||||
enum TerminateStatus {
|
||||
OK = 0;
|
||||
FAILED = 1;
|
||||
}
|
||||
|
||||
message TerminateResponse {
|
||||
TerminateStatus status = 1;
|
||||
}
|
||||
message TerminateResponse {}
|
|
@ -1,14 +1,24 @@
|
|||
#![cfg_attr(not(unix), allow(unused_imports))]
|
||||
|
||||
use std::sync::Mutex;
|
||||
mod server;
|
||||
mod sessions;
|
||||
|
||||
pub(self) static SESSIONS : Mutex<Vec<libcommand::Session>> = Mutex::new(Vec::new());
|
||||
|
||||
pub use sessions::{
|
||||
get_sessions_lock,
|
||||
remove_session_by_id
|
||||
};
|
||||
|
||||
use std::path::Path;
|
||||
#[cfg(unix)]
|
||||
use tokio::net::UnixListener;
|
||||
|
||||
#[cfg(unix)]
|
||||
use tokio_stream::wrappers::UnixListenerStream;
|
||||
|
||||
use tonic::transport::Server;
|
||||
|
||||
use libcommand::interpreter::unix_server::UnixServer;
|
||||
|
||||
#[cfg(unix)]
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#![cfg_attr(not(unix), allow(unused_imports))]
|
||||
|
||||
use tonic::{Request, Response, Status};
|
||||
use tonic::{Code, Request, Response, Status};
|
||||
|
||||
use libcommand::interpreter::{
|
||||
unix_server::Unix,
|
||||
AuthorizeRequest, AuthorizeResponse, AuthorizationStatus,
|
||||
TerminateRequest, TerminateResponse, TerminateStatus
|
||||
AuthorizeRequest, AuthorizeResponse,
|
||||
TerminateRequest, TerminateResponse
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -15,22 +15,25 @@ pub struct DaemonServer;
|
|||
impl Unix for DaemonServer {
|
||||
async fn authorize(
|
||||
&self,
|
||||
_request: Request<AuthorizeRequest>,
|
||||
request: Request<AuthorizeRequest>,
|
||||
) -> Result<Response<AuthorizeResponse>, Status> {
|
||||
let reply = AuthorizeResponse {
|
||||
status: AuthorizationStatus::Authorized.into(),
|
||||
session_id: uuid::Uuid::new_v4().to_string()
|
||||
};
|
||||
Ok(Response::new(reply))
|
||||
let session = libcommand::Session::from(request.get_ref().pid);
|
||||
let session_id = session.id.clone();
|
||||
super::SESSIONS.lock().unwrap().push(session);
|
||||
|
||||
Ok(Response::new(AuthorizeResponse {
|
||||
session_id
|
||||
}))
|
||||
}
|
||||
|
||||
async fn terminate(
|
||||
&self,
|
||||
_request: Request<TerminateRequest>,
|
||||
request: Request<TerminateRequest>,
|
||||
) -> Result<Response<TerminateResponse>, Status> {
|
||||
let reply = TerminateResponse {
|
||||
status: TerminateStatus::Ok.into()
|
||||
};
|
||||
Ok(Response::new(reply))
|
||||
let mut lock = super::get_sessions_lock()
|
||||
.map_err(|e| Status::new(Code::Internal, e))?;
|
||||
super::remove_session_by_id(&mut lock, &request.get_ref().session_id)
|
||||
.map_err(|e| Status::new(Code::NotFound, e))?;
|
||||
Ok(Response::new(TerminateResponse {}))
|
||||
}
|
||||
}
|
15
src/daemon/sessions.rs
Normal file
15
src/daemon/sessions.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use std::sync::MutexGuard;
|
||||
|
||||
pub type LockedSessions<'a> = MutexGuard<'a, Vec<libcommand::Session>>;
|
||||
|
||||
pub fn get_sessions_lock() -> Result<LockedSessions<'static>, String> {
|
||||
super::SESSIONS.lock()
|
||||
.map_err(|_| format!("Failed to get Mutex lock of sessions manager"))
|
||||
}
|
||||
|
||||
pub fn remove_session_by_id(lock: &mut LockedSessions<'static>, id: &str) -> Result<(), String> {
|
||||
let position = lock.iter().position(|s| s.id == id)
|
||||
.ok_or(format!("Session id not found"))?;
|
||||
lock.remove(position);
|
||||
Ok(())
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
pub mod client;
|
||||
|
||||
use libcommand::interpreter::{AuthorizationStatus, AuthorizeRequest, AuthorizeResponse};
|
||||
use libcommand::interpreter::{AuthorizeRequest, AuthorizeResponse, TerminateRequest};
|
||||
use tonic::Response;
|
||||
|
||||
#[cfg(unix)]
|
||||
|
@ -25,13 +25,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
|
||||
let response : Response<AuthorizeResponse> = client.authorize(request).await?;
|
||||
|
||||
if AuthorizationStatus::from_i32(response.get_ref().status) == Some(AuthorizationStatus::Authorized) {
|
||||
let mut command : std::process::Command = command_arg.into();
|
||||
let mut child = command.spawn().unwrap();
|
||||
child.wait().unwrap();
|
||||
} else {
|
||||
eprintln!("Permission denied");
|
||||
}
|
||||
let mut command : std::process::Command = command_arg.into();
|
||||
let mut child = command.spawn().unwrap();
|
||||
child.wait().unwrap();
|
||||
|
||||
client.terminate(tonic::Request::new(TerminateRequest {
|
||||
session_id: response.get_ref().session_id.clone(),
|
||||
log_file: "".to_string()
|
||||
})).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -2,8 +2,9 @@ mod command;
|
|||
mod session;
|
||||
|
||||
pub use command::Command;
|
||||
pub use session::Session;
|
||||
|
||||
pub const SOCK_FILE : &'static str = "/var/run/command_gateway.sock";
|
||||
pub const SOCK_FILE : &'static str = "command_gateway.sock";
|
||||
|
||||
pub mod interpreter {
|
||||
tonic::include_proto!("interpreter");
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
pub struct Session {
|
||||
pub id: String,
|
||||
pub pid: usize,
|
||||
pub pid: u32,
|
||||
}
|
||||
|
||||
impl From<usize> for Session {
|
||||
fn from(pid: usize) -> Self {
|
||||
impl From<u32> for Session {
|
||||
fn from(pid: u32) -> Self {
|
||||
Self {
|
||||
id: format!("ID{pid}"),
|
||||
pid
|
||||
|
|
Reference in a new issue