diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/auth.rs | 3 | ||||
-rw-r--r-- | src/main.rs | 17 | ||||
-rw-r--r-- | src/routes/device.rs | 2 | ||||
-rw-r--r-- | src/routes/start.rs | 4 | ||||
-rw-r--r-- | src/services/ping.rs | 66 | ||||
-rw-r--r-- | src/wol.rs | 2 |
6 files changed, 46 insertions, 48 deletions
diff --git a/src/auth.rs b/src/auth.rs index e4b1c2f..90d920f 100644 --- a/src/auth.rs +++ b/src/auth.rs | |||
@@ -1,5 +1,4 @@ | |||
1 | use axum::headers::HeaderValue; | 1 | use axum::http::{StatusCode, HeaderValue}; |
2 | use axum::http::StatusCode; | ||
3 | use axum::http::header::ToStrError; | 2 | use axum::http::header::ToStrError; |
4 | use tracing::{debug, error, trace}; | 3 | use tracing::{debug, error, trace}; |
5 | use crate::auth::AuthError::{MissingSecret, WrongSecret}; | 4 | use crate::auth::AuthError::{MissingSecret, WrongSecret}; |
diff --git a/src/main.rs b/src/main.rs index e96b736..aab9df3 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | use std::env; | 1 | use std::env; |
2 | use std::net::SocketAddr; | ||
2 | use std::sync::Arc; | 3 | use std::sync::Arc; |
3 | use axum::{Router, routing::post}; | 4 | use axum::{Router, routing::post}; |
4 | use axum::routing::{get, put}; | 5 | use axum::routing::{get, put}; |
@@ -24,7 +25,10 @@ mod error; | |||
24 | mod services; | 25 | mod services; |
25 | 26 | ||
26 | #[tokio::main] | 27 | #[tokio::main] |
27 | async fn main() { | 28 | async fn main() -> color_eyre::eyre::Result<()> { |
29 | |||
30 | color_eyre::install()?; | ||
31 | |||
28 | unsafe { local_offset::set_soundness(local_offset::Soundness::Unsound); } | 32 | unsafe { local_offset::set_soundness(local_offset::Soundness::Unsound); } |
29 | let time_format = | 33 | let time_format = |
30 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); | 34 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); |
@@ -64,14 +68,15 @@ async fn main() { | |||
64 | 68 | ||
65 | let addr = SETTINGS.get_string("serveraddr").unwrap_or("0.0.0.0:7229".to_string()); | 69 | let addr = SETTINGS.get_string("serveraddr").unwrap_or("0.0.0.0:7229".to_string()); |
66 | info!("start server on {}", addr); | 70 | info!("start server on {}", addr); |
67 | axum::Server::bind(&addr.parse().unwrap()) | 71 | let listener = tokio::net::TcpListener::bind(addr.parse::<SocketAddr>()?) |
68 | .serve(app.into_make_service()) | 72 | .await?; |
69 | .await | 73 | axum::serve(listener, app).await?; |
70 | .unwrap(); | 74 | |
75 | Ok(()) | ||
71 | } | 76 | } |
72 | 77 | ||
73 | pub struct AppState { | 78 | pub struct AppState { |
74 | db: PgPool, | 79 | db: PgPool, |
75 | ping_send: Sender<BroadcastCommands>, | 80 | ping_send: Sender<BroadcastCommands>, |
76 | ping_map: PingMap, | 81 | ping_map: PingMap, |
77 | } \ No newline at end of file | 82 | } |
diff --git a/src/routes/device.rs b/src/routes/device.rs index 678d117..a3308d4 100644 --- a/src/routes/device.rs +++ b/src/routes/device.rs | |||
@@ -1,7 +1,7 @@ | |||
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}; |
diff --git a/src/routes/start.rs b/src/routes/start.rs index 1555db3..a206cbd 100644 --- a/src/routes/start.rs +++ b/src/routes/start.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use axum::headers::HeaderMap; | ||
2 | use axum::Json; | 1 | use axum::Json; |
2 | use axum::http::HeaderMap; | ||
3 | use serde::{Deserialize, Serialize}; | 3 | use serde::{Deserialize, Serialize}; |
4 | use std::sync::Arc; | 4 | use std::sync::Arc; |
5 | use axum::extract::State; | 5 | use axum::extract::State; |
@@ -60,7 +60,7 @@ pub async fn start(State(state): State<Arc<crate::AppState>>, headers: HeaderMap | |||
60 | debug!("init ping service"); | 60 | debug!("init ping service"); |
61 | state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); | 61 | state.ping_map.insert(uuid_gen.clone(), PingValue { ip: device.ip.clone(), online: false }); |
62 | 62 | ||
63 | crate::services::ping::spawn(state.ping_send.clone(), device, uuid_gen.clone(), &state.ping_map, &state.db).await | 63 | crate::services::ping::spawn(state.ping_send.clone(), device, uuid_gen.clone(), &state.ping_map, &state.db).await; |
64 | }); | 64 | }); |
65 | Some(uuid_genc) | 65 | Some(uuid_genc) |
66 | } else { None }; | 66 | } else { None }; |
diff --git a/src/services/ping.rs b/src/services/ping.rs index c3bdced..0f773f4 100644 --- a/src/services/ping.rs +++ b/src/services/ping.rs | |||
@@ -85,17 +85,14 @@ pub async fn status_websocket(mut socket: WebSocket, state: Arc<AppState>) { | |||
85 | trace!("Search for uuid: {}", uuid); | 85 | trace!("Search for uuid: {}", uuid); |
86 | 86 | ||
87 | let eta = get_eta(&state.db).await; | 87 | let eta = get_eta(&state.db).await; |
88 | let _ = socket.send(Message::Text(format!("eta_{}_{}", eta, uuid))).await; | 88 | let _ = socket.send(Message::Text(format!("eta_{eta}_{uuid}"))).await; |
89 | 89 | ||
90 | let device_exists = state.ping_map.contains_key(&uuid); | 90 | let device_exists = state.ping_map.contains_key(&uuid); |
91 | match device_exists { | 91 | if device_exists { |
92 | true => { | 92 | let _ = socket.send(process_device(state.clone(), uuid).await).await; |
93 | let _ = socket.send(process_device(state.clone(), uuid).await).await; | 93 | } else { |
94 | }, | 94 | debug!("didn't find any device"); |
95 | false => { | 95 | let _ = socket.send(Message::Text(format!("notfound_{uuid}"))).await; |
96 | debug!("didn't find any device"); | ||
97 | let _ = socket.send(Message::Text(format!("notfound_{}", uuid))).await; | ||
98 | }, | ||
99 | }; | 96 | }; |
100 | 97 | ||
101 | let _ = socket.close().await; | 98 | let _ = socket.close().await; |
@@ -118,34 +115,31 @@ async fn process_device(state: Arc<AppState>, uuid: String) -> Message { | |||
118 | let pm = state.ping_map.clone().into_read_only(); | 115 | let pm = state.ping_map.clone().into_read_only(); |
119 | let device = pm.get(&uuid).expect("fatal error"); | 116 | let device = pm.get(&uuid).expect("fatal error"); |
120 | debug!("got device: {} (online: {})", device.ip, device.online); | 117 | debug!("got device: {} (online: {})", device.ip, device.online); |
121 | match device.online { | 118 | if device.online { |
122 | true => { | 119 | debug!("already started"); |
123 | debug!("already started"); | 120 | Message::Text(format!("start_{uuid}")) |
124 | Message::Text(format!("start_{}", uuid)) | 121 | } else { |
125 | }, | 122 | loop { |
126 | false => { | 123 | trace!("wait for tx message"); |
127 | loop{ | 124 | let message = state.ping_send.subscribe().recv().await.expect("fatal error"); |
128 | trace!("wait for tx message"); | 125 | trace!("got message {:?}", message); |
129 | let message = state.ping_send.subscribe().recv().await.expect("fatal error"); | 126 | return match message { |
130 | trace!("got message {:?}", message); | 127 | BroadcastCommands::Success(msg_uuid) => { |
131 | return match message { | 128 | if msg_uuid != uuid { continue; } |
132 | BroadcastCommands::Success(msg_uuid) => { | 129 | trace!("message == uuid success"); |
133 | if msg_uuid != uuid { continue; } | 130 | Message::Text(format!("start_{uuid}")) |
134 | trace!("message == uuid success"); | 131 | }, |
135 | Message::Text(format!("start_{}", uuid)) | 132 | BroadcastCommands::Timeout(msg_uuid) => { |
136 | }, | 133 | if msg_uuid != uuid { continue; } |
137 | BroadcastCommands::Timeout(msg_uuid) => { | 134 | trace!("message == uuid timeout"); |
138 | if msg_uuid != uuid { continue; } | 135 | Message::Text(format!("timeout_{uuid}")) |
139 | trace!("message == uuid timeout"); | 136 | }, |
140 | Message::Text(format!("timeout_{}", uuid)) | 137 | BroadcastCommands::Error(msg_uuid) => { |
141 | }, | 138 | if msg_uuid != uuid { continue; } |
142 | BroadcastCommands::Error(msg_uuid) => { | 139 | trace!("message == uuid error"); |
143 | if msg_uuid != uuid { continue; } | 140 | Message::Text(format!("error_{uuid}")) |
144 | trace!("message == uuid error"); | ||
145 | Message::Text(format!("error_{}", uuid)) | ||
146 | } | ||
147 | } | 141 | } |
148 | } | 142 | } |
149 | } | 143 | } |
150 | } | 144 | } |
151 | } \ No newline at end of file | 145 | } |
@@ -11,7 +11,7 @@ pub fn create_buffer(mac_addr: &str) -> Result<Vec<u8>, WebolError> { | |||
11 | let mut mac = Vec::new(); | 11 | let mut mac = Vec::new(); |
12 | let sp = mac_addr.split(':'); | 12 | let sp = mac_addr.split(':'); |
13 | for f in sp { | 13 | for f in sp { |
14 | mac.push(u8::from_str_radix(f, 16).map_err(WebolError::BufferParse)?) | 14 | mac.push(u8::from_str_radix(f, 16).map_err(WebolError::BufferParse)?); |
15 | }; | 15 | }; |
16 | let mut buf = vec![255; 6]; | 16 | let mut buf = vec![255; 6]; |
17 | for _ in 0..16 { | 17 | for _ in 0..16 { |