use crate::app_config::AppConfig; use crate::controllers::HttpResult; use crate::extractors::auth_extractor::AuthExtractor; use crate::virtweb_client; use crate::virtweb_client::{GroupID, VMInfo}; use actix_web::HttpResponse; #[derive(serde::Serialize)] struct ServerConfig { authenticated: bool, disable_auth: bool, } pub async fn config(auth: AuthExtractor) -> HttpResult { Ok(HttpResponse::Ok().json(ServerConfig { authenticated: auth.is_authenticated(), disable_auth: AppConfig::get().unsecure_disable_login, })) } #[derive(Default, Debug, serde::Serialize)] pub struct Rights { groups: Vec, vms: Vec, sys_info: bool, } #[derive(Debug, serde::Serialize)] pub struct GroupInfo { id: GroupID, vms: Vec, can_get_state: bool, can_start: bool, can_shutdown: bool, can_kill: bool, can_reset: bool, can_suspend: bool, can_resume: bool, can_screenshot: bool, } #[derive(Debug, serde::Serialize)] pub struct VMInfoAndCaps { #[serde(flatten)] info: VMInfo, can_get_state: bool, can_start: bool, can_shutdown: bool, can_kill: bool, can_reset: bool, can_suspend: bool, can_resume: bool, can_screenshot: bool, } pub async fn rights() -> HttpResult { let rights = virtweb_client::get_token_info().await?; let mut res = Rights { groups: vec![], vms: vec![], sys_info: rights.can_retrieve_system_info(), }; for g in rights.list_groups() { let group_vms = virtweb_client::group_vm_info(&g).await?; res.groups.push(GroupInfo { id: g, vms: group_vms, can_get_state: false, //TODO can_start: false, can_shutdown: false, can_kill: false, can_reset: false, can_suspend: false, can_resume: false, can_screenshot: false, }) } for v in rights.list_vm() { let vm_info = virtweb_client::vm_info(v).await?; res.vms.push(VMInfoAndCaps { info: vm_info, can_get_state: rights.is_route_allowed("GET", &v.route_state()), can_start: rights.is_route_allowed("GET", &v.route_start()), can_shutdown: rights.is_route_allowed("GET", &v.route_shutdown()), can_kill: rights.is_route_allowed("GET", &v.route_kill()), can_reset: rights.is_route_allowed("GET", &v.route_reset()), can_suspend: rights.is_route_allowed("GET", &v.route_suspend()), can_resume: rights.is_route_allowed("GET", &v.route_resume()), can_screenshot: rights.is_route_allowed("GET", &v.route_screenshot()), }) } Ok(HttpResponse::Ok().json(res)) }