From c37a282a3fe4405e455abc4992b5516db7f75cc6 Mon Sep 17 00:00:00 2001 From: HimbeerserverDE Date: Thu, 11 May 2023 18:47:21 +0200 Subject: add api to read or write individual files --- Cargo.lock | 15 +++++++++++++++ Cargo.toml | 1 + src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index b477233..540b77a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -910,6 +910,7 @@ dependencies = [ "nix", "rustls", "rustls-pemfile", + "serde", "thiserror", ] @@ -967,6 +968,20 @@ name = "serde" version = "1.0.162" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71b2f6e1ab5c2b98c05f0f35b236b22e8df7ead6ffbf51d7808da7f8817e7ab6" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.162" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2a0814352fd64b58489904a44ea8d90cb1a91dcb6b4f5ebabc32c8318e93cb6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] [[package]] name = "serde_json" diff --git a/Cargo.toml b/Cargo.toml index 810b357..b180fd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ constant_time_eq = "0.2.5" nix = "0.26.2" rustls = "0.20.0" rustls-pemfile = "1.0.2" +serde = { version = "1.0", features = ["derive"] } thiserror = "1.0" diff --git a/src/main.rs b/src/main.rs index 8513288..2c11228 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,12 +13,18 @@ use constant_time_eq::constant_time_eq; use nix::sys::reboot::{reboot, RebootMode}; use rustls::{Certificate, PrivateKey, ServerConfig}; use rustls_pemfile::{certs, pkcs8_private_keys}; +use serde::Deserialize; #[allow(non_upper_case_globals)] const KiB: usize = 1024; #[allow(non_upper_case_globals)] const MiB: usize = 1024 * KiB; +#[derive(Clone, Debug, Deserialize)] +struct DataRequest { + path: String, +} + async fn handle_reboot() -> HttpResponse { println!("[admind] request reboot"); @@ -136,6 +142,32 @@ async fn handle_switch() -> HttpResponse { } } +async fn handle_data_read(info: web::Query) -> HttpResponse { + let query = info.into_inner(); + + match fs::read(&query.path) { + Ok(data) => HttpResponse::Ok() + .content_type(ContentType::octet_stream()) + .body(data), + Err(e) => HttpResponse::NotFound() + .content_type(ContentType::plaintext()) + .body(format!("can't read file at {}: {}", query.path, e)), + } +} + +async fn handle_data_write(info: web::Query, data: web::Bytes) -> HttpResponse { + let query = info.into_inner(); + + match stream_to(&query.path, &data).await { + Ok(_) => HttpResponse::Ok() + .content_type(ContentType::plaintext()) + .body(format!("successfully wrote to {}", query.path)), + Err(e) => HttpResponse::InternalServerError() + .content_type(ContentType::plaintext()) + .body(format!("can't write file at {}: {}", query.path, e)), + } +} + #[actix_web::main] async fn main() -> io::Result<()> { match start().await { @@ -165,6 +197,8 @@ async fn start() -> Result<()> { .service(web::resource("/update/mbr").to(handle_update_mbr)) .service(web::resource("/update/root").to(handle_update_root)) .service(web::resource("/switch").to(handle_switch)) + .service(web::resource("/data/read").to(handle_data_read)) + .service(web::resource("/data/write").to(handle_data_write)) }) .bind_rustls("[::]:8443", config)? .run() -- cgit v1.2.3