got rid of tokio, a bit akward hehe
This commit is contained in:
parent
b1049c4dcc
commit
323063144c
86
dtrfs_api/Cargo.lock
generated
86
dtrfs_api/Cargo.lock
generated
@ -671,6 +671,7 @@ dependencies = [
|
|||||||
"base64",
|
"base64",
|
||||||
"bincode",
|
"bincode",
|
||||||
"ed25519-dalek",
|
"ed25519-dalek",
|
||||||
|
"futures",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"regex",
|
"regex",
|
||||||
"rustls",
|
"rustls",
|
||||||
@ -678,9 +679,6 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"sev",
|
"sev",
|
||||||
"sha3",
|
"sha3",
|
||||||
"tokio",
|
|
||||||
"tokio-stream",
|
|
||||||
"tokio-util",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -832,12 +830,65 @@ version = "1.3.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
|
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
|
||||||
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
|
"futures-core",
|
||||||
|
"futures-executor",
|
||||||
|
"futures-io",
|
||||||
|
"futures-sink",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-channel"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"futures-sink",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-core"
|
name = "futures-core"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-executor"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"futures-task",
|
||||||
|
"futures-util",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-io"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-macro"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-sink"
|
name = "futures-sink"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@ -856,10 +907,16 @@ version = "0.3.31"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"futures-channel",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-io",
|
||||||
|
"futures-macro",
|
||||||
|
"futures-sink",
|
||||||
"futures-task",
|
"futures-task",
|
||||||
|
"memchr",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
|
"slab",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -2119,21 +2176,9 @@ dependencies = [
|
|||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"signal-hook-registry",
|
"signal-hook-registry",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tokio-macros"
|
|
||||||
version = "2.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-rustls"
|
name = "tokio-rustls"
|
||||||
version = "0.26.0"
|
version = "0.26.0"
|
||||||
@ -2145,17 +2190,6 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tokio-stream"
|
|
||||||
version = "0.1.17"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047"
|
|
||||||
dependencies = [
|
|
||||||
"futures-core",
|
|
||||||
"pin-project-lite",
|
|
||||||
"tokio",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-util"
|
name = "tokio-util"
|
||||||
version = "0.7.13"
|
version = "0.7.13"
|
||||||
|
@ -11,9 +11,7 @@ regex = "1.11.1"
|
|||||||
sev = { version = "4.0", default-features = false, features = ['crypto_nossl','snp'] }
|
sev = { version = "4.0", default-features = false, features = ['crypto_nossl','snp'] }
|
||||||
ed25519-dalek = { version = "2.1.1", features = ["pem", "pkcs8"] }
|
ed25519-dalek = { version = "2.1.1", features = ["pem", "pkcs8"] }
|
||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
tokio = { version = "1.42.0", features = ["full"] }
|
futures = "0.3.31"
|
||||||
tokio-util = "0.7.13"
|
|
||||||
tokio-stream = { version = "0.1.17", features = ["io-util"] }
|
|
||||||
actix-web = { version = "4.9.0", features = ["rustls-0_23"] }
|
actix-web = { version = "4.9.0", features = ["rustls-0_23"] }
|
||||||
sha3 = "0.10.8"
|
sha3 = "0.10.8"
|
||||||
rustls = "0.23.18"
|
rustls = "0.23.18"
|
||||||
|
@ -94,7 +94,10 @@ async fn post_install_form(req: HttpRequest, form: web::Form<InstallForm>) -> Ht
|
|||||||
return HttpResponse::BadRequest().body(format!("Signature verification failed: {}", e));
|
return HttpResponse::BadRequest().body(format!("Signature verification failed: {}", e));
|
||||||
};
|
};
|
||||||
match os::encrypt_and_install_os(&form.url, &form.sha, &form.keyfile).await {
|
match os::encrypt_and_install_os(&form.url, &form.sha, &form.keyfile).await {
|
||||||
Ok(s) => s,
|
Ok(s) => {
|
||||||
|
let stream = futures::stream::iter(s.map(|item| item.map(|s| actix_web::web::Bytes::from(s))));
|
||||||
|
HttpResponse::Ok().content_type("text/plain; charset=utf-8").streaming(stream)
|
||||||
|
},
|
||||||
Err(e) => HttpResponse::InternalServerError().body(format!("{e:?}")),
|
Err(e) => HttpResponse::InternalServerError().body(format!("{e:?}")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
use crate::snp::get_derived_key;
|
use crate::snp::get_derived_key;
|
||||||
use actix_web::{web::Bytes, HttpResponse};
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use base64::prelude::{Engine, BASE64_URL_SAFE};
|
use base64::prelude::{Engine, BASE64_URL_SAFE};
|
||||||
use std::process::Command;
|
|
||||||
use std::{
|
use std::{
|
||||||
fs::File,
|
fs::{self, File},
|
||||||
io::{BufRead, BufReader, Write},
|
io::{self, BufRead, BufReader, Write},
|
||||||
path::Path,
|
path::Path,
|
||||||
process::Stdio,
|
process::{Command, Stdio},
|
||||||
};
|
};
|
||||||
|
use std::thread;
|
||||||
|
use std::sync::mpsc::{self, Sender, Receiver};
|
||||||
|
|
||||||
const SNP_KEYFILE_PATH: &str = "/tmp/detee_snp_keyfile";
|
const SNP_KEYFILE_PATH: &str = "/tmp/detee_snp_keyfile";
|
||||||
const BACKUP_KEYFILE_PATH: &str = "/tmp/detee_backup_keyfile";
|
const BACKUP_KEYFILE_PATH: &str = "/tmp/detee_backup_keyfile";
|
||||||
@ -17,18 +17,14 @@ pub async fn encrypt_and_install_os(
|
|||||||
install_url: &str,
|
install_url: &str,
|
||||||
install_sha: &str,
|
install_sha: &str,
|
||||||
keyfile: &str,
|
keyfile: &str,
|
||||||
) -> Result<HttpResponse, actix_web::Error> {
|
) -> Result<impl Iterator<Item = Result<String, io::Error>>, Box<dyn std::error::Error>> {
|
||||||
use tokio::process::Command;
|
// Decode the keyfile from base64
|
||||||
use tokio::io::{BufReader, AsyncBufReadExt};
|
let binary_keyfile = BASE64_URL_SAFE.decode(keyfile)?;
|
||||||
use tokio_stream::{StreamExt, wrappers::LinesStream};
|
fs::write(BACKUP_KEYFILE_PATH, binary_keyfile.clone())?;
|
||||||
|
|
||||||
let binary_keyfile = base64::engine::general_purpose::URL_SAFE.decode(keyfile)
|
// Write the decoded keyfile to the backup path
|
||||||
.map_err(|e| actix_web::error::ErrorBadRequest(e.to_string()))?;
|
let mut backup_keyfile = File::create(BACKUP_KEYFILE_PATH)?;
|
||||||
|
backup_keyfile.write_all(&binary_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()))?;
|
|
||||||
|
|
||||||
// Spawn the installation script as a child process
|
// Spawn the installation script as a child process
|
||||||
let mut child = Command::new("/usr/lib/dtrfs/install_os.sh")
|
let mut child = Command::new("/usr/lib/dtrfs/install_os.sh")
|
||||||
@ -38,30 +34,32 @@ pub async fn encrypt_and_install_os(
|
|||||||
.env("ROOT_KEYFILE", BACKUP_KEYFILE_PATH)
|
.env("ROOT_KEYFILE", BACKUP_KEYFILE_PATH)
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::piped())
|
.stderr(Stdio::piped())
|
||||||
.spawn()
|
.spawn()?;
|
||||||
.map_err(|e| actix_web::error::ErrorInternalServerError(e.to_string()))?;
|
|
||||||
|
|
||||||
// Take stdout and stderr from the child process
|
// Take stdout and stderr from the child process
|
||||||
let stdout = child.stdout.take().ok_or_else(|| {
|
let stdout = child.stdout.take().ok_or("Failed to capture stdout")?;
|
||||||
actix_web::error::ErrorInternalServerError("Failed to capture stdout".to_string())
|
let stderr = child.stderr.take().ok_or("Failed to capture stderr")?;
|
||||||
})?;
|
|
||||||
let stderr = child.stderr.take().ok_or_else(|| {
|
|
||||||
actix_web::error::ErrorInternalServerError("Failed to capture stderr".to_string())
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// Merge stdout and stderr into a single stream
|
// Create channels to receive lines from stdout and stderr
|
||||||
let stdout_lines = LinesStream::new(BufReader::new(stdout).lines());
|
let (tx, rx): (Sender<Result<String, io::Error>>, Receiver<Result<String, io::Error>>) = mpsc::channel();
|
||||||
let stderr_lines = LinesStream::new(BufReader::new(stderr).lines());
|
|
||||||
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()))
|
|
||||||
});
|
|
||||||
|
|
||||||
// Return the merged stream as the HTTP response
|
// Function to read lines from a reader and send them through a channel
|
||||||
Ok(HttpResponse::Ok()
|
fn read_lines<R: BufRead + Send + 'static>(reader: R, sender: Sender<Result<String, io::Error>>) {
|
||||||
.content_type("text/plain; charset=utf-8")
|
thread::spawn(move || {
|
||||||
.streaming(merged_stream))
|
for line in reader.lines() {
|
||||||
|
if sender.send(line).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start threads to read stdout and stderr
|
||||||
|
read_lines(BufReader::new(stdout), tx.clone());
|
||||||
|
read_lines(BufReader::new(stderr), tx);
|
||||||
|
|
||||||
|
// Return an iterator over the received lines
|
||||||
|
Ok(rx.into_iter())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_hot_keyfile() -> Result<()> {
|
pub fn try_hot_keyfile() -> Result<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user