diff --git a/src/server/conv.rs b/src/server/conv.rs new file mode 100644 index 0000000..81c8f73 --- /dev/null +++ b/src/server/conv.rs @@ -0,0 +1,120 @@ +//! Conversion formats +use super::*; +use std::{ + ops::Deref, + +}; + +/// A string of modified base64, appropriate for URL paths etc. +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +pub struct ModifiedBase64String(String); + +impl ModifiedBase64String +{ + /// Get as a reference + #[inline(always)] pub fn as_str(&self) -> &ModifiedBase64 + { + ModifiedBase64::new_unchecked(&self.0[..]) + } + + /// Get as a mutable reference + #[inline(always)] fn as_mut_str(&mut self) -> &mut ModifiedBase64 + { + ModifiedBase64::new_unchecked_mut(&mut self.0[..]) + } + + /// Consume into regular base64 string + pub fn into_base64(mut self) -> String + { + self.as_mut_str().unmodify(); + self.0 + } + + /// Create from a refular base64 string + pub fn from_base64(mut string: String) -> Self + { + ModifiedBase64::modify(&mut string[..]); + Self(string) + } +} + +impl AsRef for ModifiedBase64String +{ + #[inline] fn as_ref(&self) -> &ModifiedBase64 + { + self.as_str() + } +} + + +impl Deref for ModifiedBase64String +{ + type Target = ModifiedBase64; + + #[inline] fn deref(&self) -> &Self::Target { + self.as_str() + } +} + +/// A string slice of modified base64, appropriate for URL paths etc. +#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)] +#[repr(transparent)] +pub struct ModifiedBase64(str); + +impl ModifiedBase64 +{ + #[inline(always)] fn new_unchecked<'a>(from: &'a str) -> &'a ModifiedBase64 + { + unsafe { + std::mem::transmute(from) + } + } + + #[inline(always)] fn new_unchecked_mut<'a>(from: &'a mut str) -> &'a mut ModifiedBase64 + { + unsafe { + std::mem::transmute(from) + } + } + + /// Get the underlying string slice + #[inline] pub fn as_str(&self) -> &str + { + unsafe { + std::mem::transmute::<&'_ Self, &'_ str>(self) + } + } + + /// Get the underlying string slice as a mutable reference. + /// + /// # This is not safe to expose in the API. + #[inline] fn as_mut_str(&mut self) -> &mut str + { + unsafe { + std::mem::transmute::<&'_ mut Self, &'_ mut str>(self) + } + } + + fn unmodify(&mut self) -> &mut str + { + todo!(); + + self.as_mut_str() + } + + /// Modify this base64 string into a mutable reference to self + pub fn modify(base64: &mut str) -> &mut Self + { + todo!(); + + Self::new_unchecked_mut(base64) + } +} + +impl AsRef for ModifiedBase64 +{ + #[inline(always)] fn as_ref(&self) -> &ModifiedBase64 + { + self + } +} diff --git a/src/server/mod.rs b/src/server/mod.rs index 6674aa3..6e767e9 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -7,6 +7,7 @@ pub struct Config } mod state; +mod conv; #[cfg(feature="server-http")] pub mod web; #[cfg(feature="server-tcp")] pub mod tcp; diff --git a/src/server/web/auth.rs b/src/server/web/auth.rs index 9dbf212..1a8011b 100644 --- a/src/server/web/auth.rs +++ b/src/server/web/auth.rs @@ -15,7 +15,7 @@ impl str::FromStr for Sha256Hash #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct AuthRequest { - id: uuid::Uuid, + id: Uuid, sign_this: [u8; 32], salt: [u8; 16], @@ -29,7 +29,7 @@ impl AuthRequest pub fn new(cfg: &settings::Settings) -> Self { let mut empty = Self { - id: uuid::Uuid::new_v4(), + id: Uuid::new_v4(), sign_this: [0; 32], salt: [0;16], passwd_is_allowed: cfg.allow_passwd_auth, @@ -41,7 +41,7 @@ impl AuthRequest } } -pub async fn auth_req(who: source::IpAddr, state: Arc) -> Result +pub async fn auth_req(who: source::IpAddr, state: Arc) -> Result { let req = AuthRequest::new(state.cfg()); trace!("{:?} auth req", who); @@ -51,14 +51,14 @@ pub async fn auth_req(who: source::IpAddr, state: Arc) -> Result, req_id: uuid::Uuid, num: usize, body: Bytes) -> Result<(), Infallible> +pub async fn auth_key(who: source::IpAddr, state: Arc, req_id: Uuid, num: usize, body: Bytes) -> Result<(), Infallible> { trace!("{:?} auth resp key <{}>:{}", who, req_id, num); Ok(()) } -pub async fn auth_pass(who: source::IpAddr, state: Arc, req_id: uuid::Uuid, passhash: sha256::Sha256Hash) -> Result<(), Infallible> +pub async fn auth_pass(who: source::IpAddr, state: Arc, req_id: Uuid, passhash: sha256::Sha256Hash) -> Result<(), Infallible> { trace!("{:?} auth resp pass <{}>: \"{}\"", who, req_id, passhash); diff --git a/src/server/web/mod.rs b/src/server/web/mod.rs index d9d4bfa..1b41070 100644 --- a/src/server/web/mod.rs +++ b/src/server/web/mod.rs @@ -14,6 +14,11 @@ use cryptohelpers::{ rsa, }; +use state::State; +use uuid::Uuid; +use server::state::ServerState; +use settings::Settings; + /// Web server config #[derive(Debug, Clone, PartialEq, Eq)] pub struct Config @@ -29,9 +34,9 @@ mod forwarded_list; mod auth; /// Main entry point for web server -pub async fn main(state: server::state::ServerState, cfg: settings::Settings) -> eyre::Result<()> +pub async fn main(state: ServerState, cfg: Settings) -> eyre::Result<()> { - let state = Arc::new(state::State::new(state, cfg.clone())); + let state = Arc::new(State::new(state, cfg.clone())); let state = warp::any().map(move || state.clone()); // Extract the client IP or fail with custom rejection diff --git a/src/server/web/state.rs b/src/server/web/state.rs index 023265a..d8a7009 100644 --- a/src/server/web/state.rs +++ b/src/server/web/state.rs @@ -9,14 +9,14 @@ use tokio::{ #[derive(Debug)] pub struct State { - backend: RwLock, - settings: settings::Settings, + backend: RwLock, + settings: Settings, } impl State { /// Create state from a backend state and server settings - pub fn new(backend: server::state::ServerState, settings: settings::Settings) -> Self + pub fn new(backend: ServerState, settings: Settings) -> Self { Self { backend: RwLock::new(backend), @@ -25,7 +25,7 @@ impl State } /// The web server settings - pub fn cfg(&self) -> &settings::Settings + pub fn cfg(&self) -> &Settings { &self.settings }