diff options
Diffstat (limited to 'src/routes')
-rw-r--r-- | src/routes/device.rs | 28 | ||||
-rw-r--r-- | src/routes/start.rs | 76 | ||||
-rw-r--r-- | src/routes/status.rs | 2 |
3 files changed, 65 insertions, 41 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs index 678d117..c85df1b 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs | |||
@@ -1,18 +1,18 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | use axum::extract::State; | 2 | use axum::extract::State; |
3 | use axum::headers::HeaderMap; | ||
4 | use axum::Json; | 3 | use axum::Json; |
4 | use axum::http::HeaderMap; | ||
5 | use serde::{Deserialize, Serialize}; | 5 | use serde::{Deserialize, Serialize}; |
6 | use serde_json::{json, Value}; | 6 | use serde_json::{json, Value}; |
7 | use tracing::{debug, info}; | 7 | use tracing::{debug, info}; |
8 | use crate::auth::auth; | 8 | use crate::auth::auth; |
9 | use crate::db::Device; | 9 | use crate::db::Device; |
10 | use crate::error::WebolError; | 10 | use crate::error::Error; |
11 | 11 | ||
12 | pub async fn get_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, WebolError> { | 12 | pub async fn get(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<GetDevicePayload>) -> Result<Json<Value>, Error> { |
13 | info!("add device {}", payload.id); | 13 | info!("add device {}", payload.id); |
14 | let secret = headers.get("authorization"); | 14 | let secret = headers.get("authorization"); |
15 | if auth(secret).map_err(WebolError::Auth)? { | 15 | if auth(&state.config, secret).map_err(Error::Auth)? { |
16 | let device = sqlx::query_as!( | 16 | let device = sqlx::query_as!( |
17 | Device, | 17 | Device, |
18 | r#" | 18 | r#" |
@@ -21,13 +21,13 @@ pub async fn get_device(State(state): State<Arc<crate::AppState>>, headers: Head | |||
21 | WHERE id = $1; | 21 | WHERE id = $1; |
22 | "#, | 22 | "#, |
23 | payload.id | 23 | payload.id |
24 | ).fetch_one(&state.db).await.map_err(WebolError::DB)?; | 24 | ).fetch_one(&state.db).await.map_err(Error::DB)?; |
25 | 25 | ||
26 | debug!("got device {:?}", device); | 26 | debug!("got device {:?}", device); |
27 | 27 | ||
28 | Ok(Json(json!(device))) | 28 | Ok(Json(json!(device))) |
29 | } else { | 29 | } else { |
30 | Err(WebolError::Generic) | 30 | Err(Error::Generic) |
31 | } | 31 | } |
32 | } | 32 | } |
33 | 33 | ||
@@ -36,10 +36,10 @@ pub struct GetDevicePayload { | |||
36 | id: String, | 36 | id: String, |
37 | } | 37 | } |
38 | 38 | ||
39 | pub async fn put_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, WebolError> { | 39 | pub async fn put(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PutDevicePayload>) -> Result<Json<Value>, Error> { |
40 | info!("add device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); | 40 | info!("add device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); |
41 | let secret = headers.get("authorization"); | 41 | let secret = headers.get("authorization"); |
42 | if auth(secret).map_err(WebolError::Auth)? { | 42 | if auth(&state.config, secret).map_err(Error::Auth)? { |
43 | sqlx::query!( | 43 | sqlx::query!( |
44 | r#" | 44 | r#" |
45 | INSERT INTO devices (id, mac, broadcast_addr, ip) | 45 | INSERT INTO devices (id, mac, broadcast_addr, ip) |
@@ -49,11 +49,11 @@ pub async fn put_device(State(state): State<Arc<crate::AppState>>, headers: Head | |||
49 | payload.mac, | 49 | payload.mac, |
50 | payload.broadcast_addr, | 50 | payload.broadcast_addr, |
51 | payload.ip | 51 | payload.ip |
52 | ).execute(&state.db).await.map_err(WebolError::DB)?; | 52 | ).execute(&state.db).await.map_err(Error::DB)?; |
53 | 53 | ||
54 | Ok(Json(json!(PutDeviceResponse { success: true }))) | 54 | Ok(Json(json!(PutDeviceResponse { success: true }))) |
55 | } else { | 55 | } else { |
56 | Err(WebolError::Generic) | 56 | Err(Error::Generic) |
57 | } | 57 | } |
58 | } | 58 | } |
59 | 59 | ||
@@ -70,10 +70,10 @@ pub struct PutDeviceResponse { | |||
70 | success: bool | 70 | success: bool |
71 | } | 71 | } |
72 | 72 | ||
73 | pub async fn post_device(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, WebolError> { | 73 | pub async fn post(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<PostDevicePayload>) -> Result<Json<Value>, Error> { |
74 | info!("edit device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); | 74 | info!("edit device {} ({}, {}, {})", payload.id, payload.mac, payload.broadcast_addr, payload.ip); |
75 | let secret = headers.get("authorization"); | 75 | let secret = headers.get("authorization"); |
76 | if auth(secret).map_err(WebolError::Auth)? { | 76 | if auth(&state.config, secret).map_err(Error::Auth)? { |
77 | let device = sqlx::query_as!( | 77 | let device = sqlx::query_as!( |
78 | Device, | 78 | Device, |
79 | r#" | 79 | r#" |
@@ -85,11 +85,11 @@ pub async fn post_device(State(state): State<Arc<crate::AppState>>, headers: Hea | |||
85 | payload.broadcast_addr, | 85 | payload.broadcast_addr, |
86 | payload.ip, | 86 | payload.ip, |
87 | payload.id | 87 | payload.id |
88 | ).fetch_one(&state.db).await.map_err(WebolError::DB)?; | 88 | ).fetch_one(&state.db).await.map_err(Error::DB)?; |
89 | 89 | ||
90 | Ok(Json(json!(device))) | 90 | Ok(Json(json!(device))) |
91 | } else { | 91 | } else { |
92 | Err(WebolError::Generic) | 92 | Err(Error::Generic) |
93 | } | 93 | } |
94 | } | 94 | } |
95 | 95 | ||
diff --git a/src/routes/start.rs b/src/routes/start.rs index 1555db3..ce95bf3 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -1,23 +1,26 @@ | |||
1 | use axum::headers::HeaderMap; | 1 | use crate::auth::auth; |
2 | use crate::db::Device; | ||
3 | use crate::error::Error; | ||
4 | use crate::services::ping::Value as PingValue; | ||
5 | use crate::wol::{create_buffer, send_packet}; | ||
6 | use axum::extract::State; | ||
7 | use axum::http::HeaderMap; | ||
2 | use axum::Json; | 8 | use axum::Json; |
3 | use serde::{Deserialize, Serialize}; | 9 | use serde::{Deserialize, Serialize}; |
4 | use std::sync::Arc; | ||
5 | use axum::extract::State; | ||
6 | use serde_json::{json, Value}; | 10 | use serde_json::{json, Value}; |
11 | use std::sync::Arc; | ||
7 | use tracing::{debug, info}; | 12 | use tracing::{debug, info}; |
8 | use uuid::Uuid; | 13 | use uuid::Uuid; |
9 | use crate::auth::auth; | ||
10 | use crate::config::SETTINGS; | ||
11 | use crate::wol::{create_buffer, send_packet}; | ||
12 | use crate::db::Device; | ||
13 | use crate::error::WebolError; | ||
14 | use crate::services::ping::PingValue; | ||
15 | 14 | ||
16 | #[axum_macros::debug_handler] | 15 | #[axum_macros::debug_handler] |
17 | pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap, Json(payload): Json<StartPayload>) -> Result<Json<Value>, WebolError> { | 16 | pub async fn start( |
17 | State(state): State<Arc<crate::AppState>>, | ||
18 | headers: HeaderMap, | ||
19 | Json(payload): Json<Payload>, | ||
20 | ) -> Result<Json<Value>, Error> { | ||
18 | info!("POST request"); | 21 | info!("POST request"); |
19 | let secret = headers.get("authorization"); | 22 | let secret = headers.get("authorization"); |
20 | let authorized = auth(secret).map_err(WebolError::Auth)?; | 23 | let authorized = auth(&state.config, secret).map_err(Error::Auth)?; |
21 | if authorized { | 24 | if authorized { |
22 | let device = sqlx::query_as!( | 25 | let device = sqlx::query_as!( |
23 | Device, | 26 | Device, |
@@ -27,18 +30,19 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
27 | WHERE id = $1; | 30 | WHERE id = $1; |
28 | "#, | 31 | "#, |
29 | payload.id | 32 | payload.id |
30 | ).fetch_one(&state.db).await.map_err(WebolError::DB)?; | 33 | ) |
34 | .fetch_one(&state.db) | ||
35 | .await | ||
36 | .map_err(Error::DB)?; | ||
31 | 37 | ||
32 | info!("starting {}", device.id); | 38 | info!("starting {}", device.id); |
33 | 39 | ||
34 | let bind_addr = SETTINGS | 40 | let bind_addr = "0.0.0.0:0"; |
35 | .get_string("bindaddr") | ||
36 | .unwrap_or("0.0.0.0:1111".to_string()); | ||
37 | 41 | ||
38 | let _ = send_packet( | 42 | let _ = send_packet( |
39 | &bind_addr.parse().map_err(WebolError::IpParse)?, | 43 | &bind_addr.parse().map_err(Error::IpParse)?, |
40 | &device.broadcast_addr.parse().map_err(WebolError::IpParse)?, | 44 | &device.broadcast_addr.parse().map_err(Error::IpParse)?, |
41 | create_buffer(&device.mac)? | 45 | &create_buffer(&device.mac)?, |
42 | )?; | 46 | )?; |
43 | let dev_id = device.id.clone(); | 47 | let dev_id = device.id.clone(); |
44 | let uuid = if payload.ping.is_some_and(|ping| ping) { | 48 | let uuid = if payload.ping.is_some_and(|ping| ping) { |
@@ -49,7 +53,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
49 | uuid = Some(key); | 53 | uuid = Some(key); |
50 | break; | 54 | break; |
51 | } | 55 | } |
52 | }; | 56 | } |
53 | let uuid_gen = match uuid { | 57 | let uuid_gen = match uuid { |
54 | Some(u) => u, | 58 | Some(u) => u, |
55 | None => Uuid::new_v4().to_string(), | 59 | None => Uuid::new_v4().to_string(), |
@@ -58,26 +62,46 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
58 | 62 | ||
59 | tokio::spawn(async move { | 63 | tokio::spawn(async move { |
60 | debug!("init ping service"); | 64 | debug!("init ping service"); |
61 | state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); | 65 | state.ping_map.insert( |
66 | uuid_gen.clone(), | ||
67 | PingValue { | ||
68 | ip: device.ip.clone(), | ||
69 | online: false, | ||
70 | }, | ||
71 | ); | ||
62 | 72 | ||
63 | crate::services::ping::spawn(state.ping_send.clone(), device, uuid_gen.clone(), &state.ping_map, &state.db).await | 73 | crate::services::ping::spawn( |
74 | state.ping_send.clone(), | ||
75 | &state.config, | ||
76 | device, | ||
77 | uuid_gen.clone(), | ||
78 | &state.ping_map, | ||
79 | &state.db, | ||
80 | ) | ||
81 | .await; | ||
64 | }); | 82 | }); |
65 | Some(uuid_genc) | 83 | Some(uuid_genc) |
66 | } else { None }; | 84 | } else { |
67 | Ok(Json(json!(StartResponse { id: dev_id, boot: true, uuid }))) | 85 | None |
86 | }; | ||
87 | Ok(Json(json!(Response { | ||
88 | id: dev_id, | ||
89 | boot: true, | ||
90 | uuid | ||
91 | }))) | ||
68 | } else { | 92 | } else { |
69 | Err(WebolError::Generic) | 93 | Err(Error::Generic) |
70 | } | 94 | } |
71 | } | 95 | } |
72 | 96 | ||
73 | #[derive(Deserialize)] | 97 | #[derive(Deserialize)] |
74 | pub struct StartPayload { | 98 | pub struct Payload { |
75 | id: String, | 99 | id: String, |
76 | ping: Option<bool>, | 100 | ping: Option<bool>, |
77 | } | 101 | } |
78 | 102 | ||
79 | #[derive(Serialize)] | 103 | #[derive(Serialize)] |
80 | struct StartResponse { | 104 | struct Response { |
81 | id: String, | 105 | id: String, |
82 | boot: bool, | 106 | boot: bool, |
83 | uuid: Option<String>, | 107 | uuid: Option<String>, |
diff --git a/src/routes/status.rs b/src/routes/status.rs index 45f3e51..31ef996 100644 --- a/src/routes/status.rs +++ b/src/routes/status.rs | |||
@@ -7,4 +7,4 @@ use crate::services::ping::status_websocket; | |||
7 | #[axum_macros::debug_handler] | 7 | #[axum_macros::debug_handler] |
8 | pub async fn status(State(state): State<Arc<AppState>>, ws: WebSocketUpgrade) -> Response { | 8 | pub async fn status(State(state): State<Arc<AppState>>, ws: WebSocketUpgrade) -> Response { |
9 | ws.on_upgrade(move |socket| status_websocket(socket, state)) | 9 | ws.on_upgrade(move |socket| status_websocket(socket, state)) |
10 | } \ No newline at end of file | 10 | } |