Skip to content

Commit

Permalink
feat(jrec): better session recording file layout (#408)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc-André Moreau <mamoreau@devolutions.net>
  • Loading branch information
awakecoding and Marc-André Moreau authored Apr 24, 2023
1 parent abcdb4b commit 51355a1
Showing 1 changed file with 43 additions and 7 deletions.
50 changes: 43 additions & 7 deletions devolutions-gateway/src/jrec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use crate::config::Conf;
use crate::token::{CurrentJrl, JrecTokenClaims, TokenCache, TokenError};

use anyhow::Context as _;
use camino::{Utf8Path, Utf8PathBuf};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncWrite, BufWriter};
use tokio::{fs, io};
use typed_builder::TypedBuilder;
use uuid::Uuid;

#[derive(Debug, Error)]
pub enum AuthorizationError {
Expand Down Expand Up @@ -36,6 +39,23 @@ pub fn authorize(
}
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct JrecManifest {
session_id: Uuid,
file_type: String,
start_time: i64,
duration: i64,
}

impl JrecManifest {
pub fn save_to_file(&self, path: &Utf8Path) -> anyhow::Result<()> {
let json = serde_json::to_string_pretty(&self)?;
std::fs::write(path, json)?;
Ok(())
}
}

#[derive(TypedBuilder)]
pub struct PlainForward<S> {
conf: Arc<Conf>,
Expand All @@ -55,18 +75,32 @@ where
mut client_stream,
} = self;

let recording_path = &conf.recording_path.clone();
let session_id = claims.jet_aid;
let session_id_str = session_id.hyphenated().to_string();

let mut recording_path = conf.recording_path.clone();
recording_path.push(session_id_str.as_str());

if !recording_path.exists() {
fs::create_dir_all(recording_path)
fs::create_dir_all(&recording_path)
.await
.with_context(|| format!("Failed to create recording path: {recording_path}"))?;
}

let session_id = claims.jet_aid;
let file_ext = claims.jet_rft.as_str();
let filename = format!("{session_id}.{file_ext}");
let start_time = chrono::Utc::now().timestamp();
let file_type = claims.jet_rft.as_str();

let mut manifest = JrecManifest {
session_id: session_id.clone(),
file_type: file_type.to_string(),
start_time: start_time,
duration: 0,
};

let manifest_file = recording_path.join("session.json");
manifest.save_to_file(&manifest_file)?;

let filename = format!("session.{0}", file_type);
let path = recording_path.join(filename);

debug!(%path, "Opening file");
Expand All @@ -80,12 +114,14 @@ where
.with_context(|| format!("Failed to open file at {path}"))
.map(BufWriter::new)?;

debug!(%path, "File opened");

io::copy(&mut client_stream, &mut file)
.await
.context("JREC streaming to file")?;

let end_time = chrono::Utc::now().timestamp();
manifest.duration = end_time - start_time;
manifest.save_to_file(&manifest_file)?;

Ok(())
}
}

0 comments on commit 51355a1

Please sign in to comment.