aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-05-05 16:54:20 +0200
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-05-05 16:54:20 +0200
commit4c6e67de82206e8a09bb3da8f2bc2c7332f99d99 (patch)
treeb90ac1316ee19f97691e1e831ef0e1e88f8677a1
parentf82b0b4831ac60efdaec104881639b3ee5b562a9 (diff)
add shutdown api
-rw-r--r--src/main.rs74
1 files changed, 43 insertions, 31 deletions
diff --git a/src/main.rs b/src/main.rs
index 52e233b..3abfb77 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -25,38 +25,15 @@ async fn handle_reboot() -> HttpResponse {
}
}
-async fn basic_auth_validator(
- req: ServiceRequest,
- credentials: BasicAuth,
-) -> std::result::Result<ServiceRequest, (actix_web::Error, ServiceRequest)> {
- let config = req.app_data::<Config>().cloned().unwrap_or_default();
-
- match validate_credentials(
- credentials.user_id(),
- credentials.password().unwrap_or_default().trim(),
- ) {
- Ok(res) => {
- if res {
- Ok(req)
- } else {
- Err((AuthenticationError::from(config).into(), req))
- }
- }
- Err(_) => Err((AuthenticationError::from(config).into(), req)),
- }
-}
-
-fn validate_credentials(user_id: &str, user_password: &str) -> io::Result<bool> {
- let correct_password = fs::read("/data/admind.passwd")?;
-
- if user_id == "rustkrazy" && constant_time_eq(user_password.as_bytes(), &correct_password) {
- return Ok(true);
+async fn handle_shutdown() -> HttpResponse {
+ match reboot(RebootMode::RB_POWER_OFF) {
+ Ok(_) => HttpResponse::Ok()
+ .content_type(ContentType::plaintext())
+ .body("shutting down..."),
+ Err(e) => HttpResponse::InternalServerError()
+ .content_type(ContentType::plaintext())
+ .body(format!("can't shut down: {}", e)),
}
-
- Err(io::Error::new(
- io::ErrorKind::PermissionDenied,
- "Invalid credentials",
- ))
}
#[actix_web::main]
@@ -82,6 +59,7 @@ async fn start() -> Result<()> {
App::new()
.wrap(auth)
.service(web::resource("/rustkrazy/reboot").to(handle_reboot))
+ .service(web::resource("/rustkrazy/shutdown").to(handle_shutdown))
})
.bind_rustls("[::]:8443", config)?
.run()
@@ -109,3 +87,37 @@ fn load_rustls_config() -> Result<ServerConfig> {
Ok(config.with_single_cert(cert_chain, keys.remove(0))?)
}
+
+async fn basic_auth_validator(
+ req: ServiceRequest,
+ credentials: BasicAuth,
+) -> std::result::Result<ServiceRequest, (actix_web::Error, ServiceRequest)> {
+ let config = req.app_data::<Config>().cloned().unwrap_or_default();
+
+ match validate_credentials(
+ credentials.user_id(),
+ credentials.password().unwrap_or_default().trim(),
+ ) {
+ Ok(res) => {
+ if res {
+ Ok(req)
+ } else {
+ Err((AuthenticationError::from(config).into(), req))
+ }
+ }
+ Err(_) => Err((AuthenticationError::from(config).into(), req)),
+ }
+}
+
+fn validate_credentials(user_id: &str, user_password: &str) -> io::Result<bool> {
+ let correct_password = fs::read("/data/admind.passwd")?;
+
+ if user_id == "rustkrazy" && constant_time_eq(user_password.as_bytes(), &correct_password) {
+ return Ok(true);
+ }
+
+ Err(io::Error::new(
+ io::ErrorKind::PermissionDenied,
+ "Invalid credentials",
+ ))
+}