From 9407d8f0a8b4b9a4df01f409a653eb5632646fe6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 May 2024 21:24:43 +0200 Subject: [PATCH] Add system information --- remote_backend/src/controllers/mod.rs | 1 + .../src/controllers/sys_info_controller.rs | 22 ++++++++++++++++ remote_backend/src/main.rs | 12 ++++++++- remote_backend/src/virtweb_client.rs | 25 ++++++++++++++++++- 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 remote_backend/src/controllers/sys_info_controller.rs diff --git a/remote_backend/src/controllers/mod.rs b/remote_backend/src/controllers/mod.rs index bbf56d0..23ff409 100644 --- a/remote_backend/src/controllers/mod.rs +++ b/remote_backend/src/controllers/mod.rs @@ -7,6 +7,7 @@ use std::io::ErrorKind; pub mod auth_controller; pub mod server_controller; +pub mod sys_info_controller; pub mod vm_controller; /// Custom error to ease controller writing diff --git a/remote_backend/src/controllers/sys_info_controller.rs b/remote_backend/src/controllers/sys_info_controller.rs new file mode 100644 index 0000000..c07c66b --- /dev/null +++ b/remote_backend/src/controllers/sys_info_controller.rs @@ -0,0 +1,22 @@ +use crate::controllers::HttpResult; +use crate::virtweb_client; +use actix_web::HttpResponse; + +#[derive(serde::Serialize)] +struct SysInfoStatus { + allowed: bool, +} + +/// Check if system info can be retrieved +pub async fn config() -> HttpResult { + let info = virtweb_client::get_token_info().await?; + + Ok(HttpResponse::Ok().json(SysInfoStatus { + allowed: info.can_retrieve_system_info(), + })) +} + +/// Get current system status +pub async fn status() -> HttpResult { + Ok(HttpResponse::Ok().json(virtweb_client::get_server_info().await?)) +} diff --git a/remote_backend/src/main.rs b/remote_backend/src/main.rs index 6cc3568..d5e65f4 100644 --- a/remote_backend/src/main.rs +++ b/remote_backend/src/main.rs @@ -11,7 +11,9 @@ use actix_web::{web, App, HttpServer}; use light_openid::basic_state_manager::BasicStateManager; use remote_backend::app_config::AppConfig; use remote_backend::constants; -use remote_backend::controllers::{auth_controller, server_controller, vm_controller}; +use remote_backend::controllers::{ + auth_controller, server_controller, sys_info_controller, vm_controller, +}; use remote_backend::middlewares::auth_middleware::AuthChecker; use std::time::Duration; @@ -97,6 +99,14 @@ async fn main() -> std::io::Result<()> { "/api/vm/{uid}/screenshot", web::get().to(vm_controller::screenshot), ) + .route( + "/api/sysinfo/config", + web::get().to(sys_info_controller::config), + ) + .route( + "/api/sysinfo/status", + web::get().to(sys_info_controller::status), + ) }) .bind(&AppConfig::get().listen_address)? .run() diff --git a/remote_backend/src/virtweb_client.rs b/remote_backend/src/virtweb_client.rs index 9749cf7..732c251 100644 --- a/remote_backend/src/virtweb_client.rs +++ b/remote_backend/src/virtweb_client.rs @@ -84,6 +84,19 @@ pub struct VMState { pub state: String, } +#[derive(serde::Deserialize, serde::Serialize, Debug)] +pub struct SystemSystemInfo { + physical_core_count: usize, + uptime: usize, + used_memory: usize, + available_memory: usize, +} + +#[derive(serde::Deserialize, serde::Serialize, Debug)] +pub struct SystemInfo { + system: SystemSystemInfo, +} + #[derive(serde::Deserialize, Debug)] pub struct TokenRight { verb: String, @@ -134,6 +147,11 @@ impl TokenInfo { .map(|r| VMUuid::from_str(r.path.rsplit_once('/').unwrap().1).unwrap()) .collect::>() } + + /// Check if system info can be retrived + pub fn can_retrieve_system_info(&self) -> bool { + self.is_route_allowed("GET", "/api/server/info") + } } /// Perform a request on the API @@ -224,7 +242,7 @@ pub async fn vm_resume(id: VMUuid) -> anyhow::Result<()> { Ok(()) } -/// Resume a vm +/// Grab a screenshot of the VM pub async fn vm_screenshot(id: VMUuid) -> anyhow::Result> { Ok(request(id.route_screenshot()) .await? @@ -232,3 +250,8 @@ pub async fn vm_screenshot(id: VMUuid) -> anyhow::Result> { .await? .to_vec()) } + +/// Get current server information +pub async fn get_server_info() -> anyhow::Result { + json_request("/api/server/info").await +}