aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-05-11 18:47:21 +0200
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-05-11 18:47:21 +0200
commitc37a282a3fe4405e455abc4992b5516db7f75cc6 (patch)
treefdcc0990197c17bad2c0ad70eedec513d7c8a286 /src/main.rs
parent9b7a525b2dfd55c8400418045ed9342c92ca21eb (diff)
add api to read or write individual files
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs34
1 files changed, 34 insertions, 0 deletions
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<DataRequest>) -> 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<DataRequest>, 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()