diff options
Diffstat (limited to 'src/routes/start.rs')
-rw-r--r-- | src/routes/start.rs | 65 |
1 files changed, 28 insertions, 37 deletions
diff --git a/src/routes/start.rs b/src/routes/start.rs index 4888325..d4c0802 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -1,10 +1,8 @@ | |||
1 | use crate::auth::auth; | ||
2 | use crate::db::Device; | 1 | use crate::db::Device; |
3 | use crate::error::Error; | 2 | use crate::error::Error; |
4 | use crate::services::ping::Value as PingValue; | 3 | use crate::services::ping::Value as PingValue; |
5 | use crate::wol::{create_buffer, send_packet}; | 4 | use crate::wol::{create_buffer, send_packet}; |
6 | use axum::extract::State; | 5 | use axum::extract::State; |
7 | use axum::http::HeaderMap; | ||
8 | use axum::Json; | 6 | use axum::Json; |
9 | use serde::{Deserialize, Serialize}; | 7 | use serde::{Deserialize, Serialize}; |
10 | use serde_json::{json, Value}; | 8 | use serde_json::{json, Value}; |
@@ -14,48 +12,41 @@ use uuid::Uuid; | |||
14 | 12 | ||
15 | pub async fn start( | 13 | pub async fn start( |
16 | State(state): State<Arc<crate::AppState>>, | 14 | State(state): State<Arc<crate::AppState>>, |
17 | headers: HeaderMap, | ||
18 | Json(payload): Json<Payload>, | 15 | Json(payload): Json<Payload>, |
19 | ) -> Result<Json<Value>, Error> { | 16 | ) -> Result<Json<Value>, Error> { |
20 | info!("POST request"); | 17 | info!("POST request"); |
21 | let secret = headers.get("authorization"); | 18 | let device = sqlx::query_as!( |
22 | let authorized = matches!(auth(&state.config, secret)?, crate::auth::Response::Success); | 19 | Device, |
23 | if authorized { | 20 | r#" |
24 | let device = sqlx::query_as!( | 21 | SELECT id, mac, broadcast_addr, ip, times |
25 | Device, | 22 | FROM devices |
26 | r#" | 23 | WHERE id = $1; |
27 | SELECT id, mac, broadcast_addr, ip, times | 24 | "#, |
28 | FROM devices | 25 | payload.id |
29 | WHERE id = $1; | 26 | ) |
30 | "#, | 27 | .fetch_one(&state.db) |
31 | payload.id | 28 | .await?; |
32 | ) | ||
33 | .fetch_one(&state.db) | ||
34 | .await?; | ||
35 | 29 | ||
36 | info!("starting {}", device.id); | 30 | info!("starting {}", device.id); |
37 | 31 | ||
38 | let bind_addr = "0.0.0.0:0"; | 32 | let bind_addr = "0.0.0.0:0"; |
39 | 33 | ||
40 | let _ = send_packet( | 34 | let _ = send_packet( |
41 | bind_addr, | 35 | bind_addr, |
42 | &device.broadcast_addr, | 36 | &device.broadcast_addr, |
43 | &create_buffer(&device.mac.to_string())?, | 37 | &create_buffer(&device.mac.to_string())?, |
44 | )?; | 38 | )?; |
45 | let dev_id = device.id.clone(); | 39 | let dev_id = device.id.clone(); |
46 | let uuid = if payload.ping.is_some_and(|ping| ping) { | 40 | let uuid = if payload.ping.is_some_and(|ping| ping) { |
47 | Some(setup_ping(state, device)) | 41 | Some(setup_ping(state, device)) |
48 | } else { | ||
49 | None | ||
50 | }; | ||
51 | Ok(Json(json!(Response { | ||
52 | id: dev_id, | ||
53 | boot: true, | ||
54 | uuid | ||
55 | }))) | ||
56 | } else { | 42 | } else { |
57 | Err(Error::Generic) | 43 | None |
58 | } | 44 | }; |
45 | Ok(Json(json!(Response { | ||
46 | id: dev_id, | ||
47 | boot: true, | ||
48 | uuid | ||
49 | }))) | ||
59 | } | 50 | } |
60 | 51 | ||
61 | fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String { | 52 | fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String { |