diff options
author | FxQnLr <[email protected]> | 2024-04-10 20:15:39 +0200 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-04-10 20:15:39 +0200 |
commit | 69d3e0e6564b416637978a69f0a035066aea4759 (patch) | |
tree | 90c5b80a808f9c71cd3440add52b023973ae5b3b /src | |
parent | 07740f3d985b4cc921b69cd45ec9191a2daecd67 (diff) | |
download | webol-69d3e0e6564b416637978a69f0a035066aea4759.tar webol-69d3e0e6564b416637978a69f0a035066aea4759.tar.gz webol-69d3e0e6564b416637978a69f0a035066aea4759.zip |
Closes #30 and #27. At least a little
Diffstat (limited to 'src')
-rw-r--r-- | src/auth.rs | 5 | ||||
-rw-r--r-- | src/error.rs | 13 | ||||
-rw-r--r-- | src/main.rs | 5 | ||||
-rw-r--r-- | src/storage.rs | 7 |
4 files changed, 23 insertions, 7 deletions
diff --git a/src/auth.rs b/src/auth.rs index 74008b5..c662e36 100644 --- a/src/auth.rs +++ b/src/auth.rs | |||
@@ -6,6 +6,7 @@ use axum::{ | |||
6 | response::Response, | 6 | response::Response, |
7 | }; | 7 | }; |
8 | use serde::Deserialize; | 8 | use serde::Deserialize; |
9 | use tracing::trace; | ||
9 | 10 | ||
10 | #[derive(Debug, Clone, Deserialize)] | 11 | #[derive(Debug, Clone, Deserialize)] |
11 | pub enum Methods { | 12 | pub enum Methods { |
@@ -20,15 +21,19 @@ pub async fn auth( | |||
20 | next: Next, | 21 | next: Next, |
21 | ) -> Result<Response, StatusCode> { | 22 | ) -> Result<Response, StatusCode> { |
22 | let auth = state.config.auth; | 23 | let auth = state.config.auth; |
24 | trace!(?auth.method, "auth request"); | ||
23 | match auth.method { | 25 | match auth.method { |
24 | Methods::Key => { | 26 | Methods::Key => { |
25 | if let Some(secret) = headers.get("authorization") { | 27 | if let Some(secret) = headers.get("authorization") { |
26 | if auth.secret.as_str() != secret { | 28 | if auth.secret.as_str() != secret { |
29 | trace!("auth failed, unknown secret"); | ||
27 | return Err(StatusCode::UNAUTHORIZED); | 30 | return Err(StatusCode::UNAUTHORIZED); |
28 | }; | 31 | }; |
32 | trace!("auth successfull"); | ||
29 | let response = next.run(request).await; | 33 | let response = next.run(request).await; |
30 | Ok(response) | 34 | Ok(response) |
31 | } else { | 35 | } else { |
36 | trace!("auth failed, no secret"); | ||
32 | Err(StatusCode::UNAUTHORIZED) | 37 | Err(StatusCode::UNAUTHORIZED) |
33 | } | 38 | } |
34 | } | 39 | } |
diff --git a/src/error.rs b/src/error.rs index 8a011bf..2d70592 100644 --- a/src/error.rs +++ b/src/error.rs | |||
@@ -7,7 +7,7 @@ use mac_address::MacParseError; | |||
7 | use serde_json::json; | 7 | use serde_json::json; |
8 | use utoipa::ToSchema; | 8 | use utoipa::ToSchema; |
9 | use std::io; | 9 | use std::io; |
10 | use tracing::error; | 10 | use tracing::{error, warn}; |
11 | 11 | ||
12 | #[derive(Debug, thiserror::Error, ToSchema)] | 12 | #[derive(Debug, thiserror::Error, ToSchema)] |
13 | pub enum Error { | 13 | pub enum Error { |
@@ -50,15 +50,20 @@ pub enum Error { | |||
50 | 50 | ||
51 | impl IntoResponse for Error { | 51 | impl IntoResponse for Error { |
52 | fn into_response(self) -> Response { | 52 | fn into_response(self) -> Response { |
53 | error!("{}", self.to_string()); | 53 | // error!("{}", self.to_string()); |
54 | let (status, error_message) = match self { | 54 | let (status, error_message) = match self { |
55 | Self::Json { source } => { | 55 | Self::Json { source } => { |
56 | error!("{source}"); | 56 | error!("{source}"); |
57 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 57 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") |
58 | } | 58 | } |
59 | Self::Io { source } => { | 59 | Self::Io { source } => { |
60 | error!("{source}"); | 60 | if source.kind() == io::ErrorKind::NotFound { |
61 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | 61 | warn!("unknown device requested"); |
62 | (StatusCode::NOT_FOUND, "Requested device not found") | ||
63 | } else { | ||
64 | error!("{source}"); | ||
65 | (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") | ||
66 | } | ||
62 | } | 67 | } |
63 | Self::ParseHeader { source } => { | 68 | Self::ParseHeader { source } => { |
64 | error!("{source}"); | 69 | error!("{source}"); |
diff --git a/src/main.rs b/src/main.rs index 8af8c63..204c318 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -10,7 +10,7 @@ use dashmap::DashMap; | |||
10 | use std::{env, sync::Arc}; | 10 | use std::{env, sync::Arc}; |
11 | use time::UtcOffset; | 11 | use time::UtcOffset; |
12 | use tokio::sync::broadcast::{channel, Sender}; | 12 | use tokio::sync::broadcast::{channel, Sender}; |
13 | use tracing::{info, level_filters::LevelFilter}; | 13 | use tracing::{info, level_filters::LevelFilter, trace}; |
14 | use tracing_subscriber::{ | 14 | use tracing_subscriber::{ |
15 | fmt::{self, time::OffsetTime}, | 15 | fmt::{self, time::OffsetTime}, |
16 | prelude::*, | 16 | prelude::*, |
@@ -89,11 +89,12 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
89 | .from_env_lossy(), | 89 | .from_env_lossy(), |
90 | ) | 90 | ) |
91 | .init(); | 91 | .init(); |
92 | trace!("logging initialized"); | ||
92 | 93 | ||
93 | Device::setup()?; | 94 | Device::setup()?; |
94 | 95 | ||
95 | let version = env!("CARGO_PKG_VERSION"); | 96 | let version = env!("CARGO_PKG_VERSION"); |
96 | info!("start webol v{}", version); | 97 | info!(?version, "start webol"); |
97 | 98 | ||
98 | let (tx, _) = channel(32); | 99 | let (tx, _) = channel(32); |
99 | 100 | ||
diff --git a/src/storage.rs b/src/storage.rs index 6ba5ee1..0da245b 100644 --- a/src/storage.rs +++ b/src/storage.rs | |||
@@ -8,7 +8,7 @@ use ipnetwork::IpNetwork; | |||
8 | use mac_address::MacAddress; | 8 | use mac_address::MacAddress; |
9 | use serde::{Deserialize, Serialize}; | 9 | use serde::{Deserialize, Serialize}; |
10 | use serde_json::json; | 10 | use serde_json::json; |
11 | use tracing::{debug, warn}; | 11 | use tracing::{debug, trace, warn}; |
12 | use utoipa::ToSchema; | 12 | use utoipa::ToSchema; |
13 | 13 | ||
14 | use crate::error::Error; | 14 | use crate::error::Error; |
@@ -26,6 +26,7 @@ impl Device { | |||
26 | const STORAGE_PATH: &'static str = "devices"; | 26 | const STORAGE_PATH: &'static str = "devices"; |
27 | 27 | ||
28 | pub fn setup() -> Result<String, Error> { | 28 | pub fn setup() -> Result<String, Error> { |
29 | trace!("check for storage at {}", Self::STORAGE_PATH); | ||
29 | let sp = Path::new(Self::STORAGE_PATH); | 30 | let sp = Path::new(Self::STORAGE_PATH); |
30 | if !sp.exists() { | 31 | if !sp.exists() { |
31 | warn!("device storage path doesn't exist, creating it"); | 32 | warn!("device storage path doesn't exist, creating it"); |
@@ -38,17 +39,21 @@ impl Device { | |||
38 | } | 39 | } |
39 | 40 | ||
40 | pub fn read(id: &str) -> Result<Self, Error> { | 41 | pub fn read(id: &str) -> Result<Self, Error> { |
42 | trace!(?id, "attempt to read file"); | ||
41 | let mut file = File::open(format!("{}/{id}.json", Self::STORAGE_PATH))?; | 43 | let mut file = File::open(format!("{}/{id}.json", Self::STORAGE_PATH))?; |
42 | let mut buf = String::new(); | 44 | let mut buf = String::new(); |
43 | file.read_to_string(&mut buf)?; | 45 | file.read_to_string(&mut buf)?; |
46 | trace!(?id, ?buf, "read successfully from file"); | ||
44 | 47 | ||
45 | let dev = serde_json::from_str(&buf)?; | 48 | let dev = serde_json::from_str(&buf)?; |
46 | Ok(dev) | 49 | Ok(dev) |
47 | } | 50 | } |
48 | 51 | ||
49 | pub fn write(&self) -> Result<(), Error> { | 52 | pub fn write(&self) -> Result<(), Error> { |
53 | trace!(?self.id, ?self, "attempt to write to file"); | ||
50 | let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; | 54 | let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; |
51 | file.write_all(json!(self).to_string().as_bytes())?; | 55 | file.write_all(json!(self).to_string().as_bytes())?; |
56 | trace!(?self.id, "wrote successfully to file"); | ||
52 | 57 | ||
53 | Ok(()) | 58 | Ok(()) |
54 | } | 59 | } |