I tried getting rid of tokio, it didn't work out

This commit is contained in:
Ramil_Algayev 2024-12-28 07:07:30 +04:00
parent b1049c4dcc
commit 5aa562fb01
4 changed files with 17 additions and 22 deletions

5
dtrfs_api/Cargo.lock generated

@ -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",

@ -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"

@ -94,7 +94,7 @@ async fn post_install_form(req: HttpRequest, form: web::Form<InstallForm>) -> 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:?}")),
}
}

@ -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<HttpResponse, actix_web::Error> {
) -> Result<impl tokio_stream::Stream<Item = Result<Bytes>>> {
// 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<()> {