Fix VNC connection issue

This commit is contained in:
2023-12-07 00:23:19 +01:00
parent 7d88cd576c
commit d1a9b6c3bb
7 changed files with 78 additions and 55 deletions

View File

@ -6,14 +6,14 @@ use std::time::Duration;
const TOKENS_CLEAN_INTERVAL: Duration = Duration::from_secs(60);
const VNC_TOKEN_LEN: usize = 15;
const VNC_TOKEN_LIFETIME: u64 = 120;
pub const VNC_TOKEN_LIFETIME: u64 = 30;
#[derive(thiserror::Error, Debug)]
enum VNCTokenError {
#[error("Could not consume token, because it does not exist!")]
ConsumeErrorTokenNotFound,
#[error("Could not consume token, because it has expired!")]
ConsumeErrorTokenExpired,
#[error("Could not use token, because it does not exist!")]
UseErrorTokenNotFound,
#[error("Could not use token, because it has expired!")]
UseErrorTokenExpired,
}
#[derive(Debug, Clone)]
@ -64,24 +64,22 @@ impl Handler<IssueTokenReq> for VNCTokensActor {
#[derive(Message)]
#[rtype(result = "anyhow::Result<XMLUuid>")]
pub struct ConsumeTokenReq(String);
pub struct UseTokenReq(String);
impl Handler<ConsumeTokenReq> for VNCTokensActor {
impl Handler<UseTokenReq> for VNCTokensActor {
type Result = anyhow::Result<XMLUuid>;
fn handle(&mut self, msg: ConsumeTokenReq, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Attempt to consume a token {:?}", msg.0);
fn handle(&mut self, msg: UseTokenReq, _ctx: &mut Self::Context) -> Self::Result {
log::debug!("Attempt to use a token {:?}", msg.0);
let token_index = self
let token = self
.0
.iter()
.position(|i| i.token == msg.0)
.ok_or(VNCTokenError::ConsumeErrorTokenNotFound)?;
let token = self.0.remove(token_index);
.find(|i| i.token == msg.0)
.ok_or(VNCTokenError::UseErrorTokenNotFound)?;
if token.is_expired() {
return Err(VNCTokenError::ConsumeErrorTokenExpired.into());
return Err(VNCTokenError::UseErrorTokenExpired.into());
}
Ok(token.vm)
@ -101,8 +99,8 @@ impl VNCTokensManager {
self.0.send(IssueTokenReq(id)).await?
}
/// Consume a VNC access token
pub async fn consume_token(&self, token: String) -> anyhow::Result<XMLUuid> {
self.0.send(ConsumeTokenReq(token)).await?
/// Use a VNC access token
pub async fn use_token(&self, token: String) -> anyhow::Result<XMLUuid> {
self.0.send(UseTokenReq(token)).await?
}
}

View File

@ -1,3 +1,4 @@
use crate::actors::vnc_tokens_actor::VNC_TOKEN_LIFETIME;
use crate::app_config::AppConfig;
use crate::constants;
use crate::constants::{DISK_NAME_MAX_LEN, DISK_NAME_MIN_LEN, DISK_SIZE_MAX, DISK_SIZE_MIN};
@ -29,6 +30,7 @@ struct LenConstraints {
#[derive(serde::Serialize)]
struct ServerConstraints {
iso_max_size: usize,
vnc_token_duration: u64,
vm_name_size: LenConstraints,
vm_title_size: LenConstraints,
memory_size: LenConstraints,
@ -47,6 +49,8 @@ pub async fn static_config(local_auth: LocalAuthEnabled) -> impl Responder {
constraints: ServerConstraints {
iso_max_size: constants::ISO_MAX_SIZE,
vnc_token_duration: VNC_TOKEN_LIFETIME,
vm_name_size: LenConstraints { min: 2, max: 50 },
vm_title_size: LenConstraints { min: 0, max: 50 },
memory_size: LenConstraints {

View File

@ -269,7 +269,7 @@ pub async fn vnc(
req: HttpRequest,
stream: web::Payload,
) -> HttpResult {
let domain_id = manager.consume_token(token.0.token).await?;
let domain_id = manager.use_token(token.0.token).await?;
let domain = client.get_single_domain(domain_id).await?;
let socket_path = match domain.devices.graphics {