From 5aa562fb01c5487b8b6884168d17f3c40c67f615 Mon Sep 17 00:00:00 2001 From: Ramil_Algayev Date: Sat, 28 Dec 2024 07:07:30 +0400 Subject: [PATCH] I tried getting rid of tokio, it didn't work out --- dtrfs_api/Cargo.lock | 5 +++-- dtrfs_api/Cargo.toml | 1 + dtrfs_api/src/main.rs | 2 +- dtrfs_api/src/os.rs | 31 ++++++++++++------------------- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/dtrfs_api/Cargo.lock b/dtrfs_api/Cargo.lock index d73b5bd..4ad8d44 100644 --- a/dtrfs_api/Cargo.lock +++ b/dtrfs_api/Cargo.lock @@ -417,9 +417,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" [[package]] name = "bytestring" @@ -670,6 +670,7 @@ dependencies = [ "anyhow", "base64", "bincode", + "bytes", "ed25519-dalek", "lazy_static", "regex", diff --git a/dtrfs_api/Cargo.toml b/dtrfs_api/Cargo.toml index 2f66da4..45d8772 100644 --- a/dtrfs_api/Cargo.toml +++ b/dtrfs_api/Cargo.toml @@ -19,3 +19,4 @@ sha3 = "0.10.8" rustls = "0.23.18" rustls-pemfile = "2.2.0" serde = { version = "1.0.215", features = ["derive"] } +bytes = "1.9.0" \ No newline at end of file diff --git a/dtrfs_api/src/main.rs b/dtrfs_api/src/main.rs index b174538..391c062 100644 --- a/dtrfs_api/src/main.rs +++ b/dtrfs_api/src/main.rs @@ -94,7 +94,7 @@ async fn post_install_form(req: HttpRequest, form: web::Form) -> Ht return HttpResponse::BadRequest().body(format!("Signature verification failed: {}", e)); }; match os::encrypt_and_install_os(&form.url, &form.sha, &form.keyfile).await { - Ok(s) => s, + Ok(s) => HttpResponse::Ok().streaming(s), Err(e) => HttpResponse::InternalServerError().body(format!("{e:?}")), } } diff --git a/dtrfs_api/src/os.rs b/dtrfs_api/src/os.rs index 32d3c1d..bf47540 100644 --- a/dtrfs_api/src/os.rs +++ b/dtrfs_api/src/os.rs @@ -1,5 +1,5 @@ use crate::snp::get_derived_key; -use actix_web::{web::Bytes, HttpResponse}; +use bytes::Bytes; use anyhow::{anyhow, Result}; use base64::prelude::{Engine, BASE64_URL_SAFE}; use std::process::Command; @@ -17,18 +17,18 @@ pub async fn encrypt_and_install_os( install_url: &str, install_sha: &str, keyfile: &str, -) -> Result { +) -> Result>> { + // I swear I tried my best to do without tokio + // It is just such a pain to do to anything without tokio + // I am sorry for the mess but I gave up after countless tries use tokio::process::Command; use tokio::io::{BufReader, AsyncBufReadExt}; use tokio_stream::{StreamExt, wrappers::LinesStream}; - let binary_keyfile = base64::engine::general_purpose::URL_SAFE.decode(keyfile) - .map_err(|e| actix_web::error::ErrorBadRequest(e.to_string()))?; + let binary_keyfile = base64::engine::general_purpose::URL_SAFE.decode(keyfile)?; // Write the decoded keyfile to the backup path asynchronously - tokio::fs::write(BACKUP_KEYFILE_PATH, &binary_keyfile) - .await - .map_err(|e| actix_web::error::ErrorInternalServerError(e.to_string()))?; + tokio::fs::write(BACKUP_KEYFILE_PATH, &binary_keyfile).await?; // Spawn the installation script as a child process let mut child = Command::new("/usr/lib/dtrfs/install_os.sh") @@ -38,16 +38,11 @@ pub async fn encrypt_and_install_os( .env("ROOT_KEYFILE", BACKUP_KEYFILE_PATH) .stdout(Stdio::piped()) .stderr(Stdio::piped()) - .spawn() - .map_err(|e| actix_web::error::ErrorInternalServerError(e.to_string()))?; + .spawn()?; // Take stdout and stderr from the child process - let stdout = child.stdout.take().ok_or_else(|| { - actix_web::error::ErrorInternalServerError("Failed to capture stdout".to_string()) - })?; - let stderr = child.stderr.take().ok_or_else(|| { - actix_web::error::ErrorInternalServerError("Failed to capture stderr".to_string()) - })?; + let stdout = child.stdout.take().unwrap(); + let stderr = child.stderr.take().unwrap(); // Merge stdout and stderr into a single stream let stdout_lines = LinesStream::new(BufReader::new(stdout).lines()); @@ -55,13 +50,11 @@ pub async fn encrypt_and_install_os( let merged_stream = stdout_lines.merge(stderr_lines).map(|line_result| { line_result .map(|line| Bytes::from(line + "\n")) - .map_err(|e| actix_web::error::ErrorInternalServerError(e.to_string())) + .map_err(|e| anyhow::Error::new(e)) }); // Return the merged stream as the HTTP response - Ok(HttpResponse::Ok() - .content_type("text/plain; charset=utf-8") - .streaming(merged_stream)) + Ok(merged_stream) } pub fn try_hot_keyfile() -> Result<()> {