started modified base64 format

master
Avril 4 years ago
parent 14daa662bd
commit 5e24e96ffe
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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<ModifiedBase64> 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<ModifiedBase64> for ModifiedBase64
{
#[inline(always)] fn as_ref(&self) -> &ModifiedBase64
{
self
}
}

@ -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;

@ -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<state::State>) -> Result<AuthRequest, Infallible>
pub async fn auth_req(who: source::IpAddr, state: Arc<State>) -> Result<AuthRequest, Infallible>
{
let req = AuthRequest::new(state.cfg());
trace!("{:?} auth req", who);
@ -51,14 +51,14 @@ pub async fn auth_req(who: source::IpAddr, state: Arc<state::State>) -> Result<A
Ok(req)
}
pub async fn auth_key(who: source::IpAddr, state: Arc<state::State>, req_id: uuid::Uuid, num: usize, body: Bytes) -> Result<(), Infallible>
pub async fn auth_key(who: source::IpAddr, state: Arc<State>, 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<state::State>, req_id: uuid::Uuid, passhash: sha256::Sha256Hash) -> Result<(), Infallible>
pub async fn auth_pass(who: source::IpAddr, state: Arc<State>, req_id: Uuid, passhash: sha256::Sha256Hash) -> Result<(), Infallible>
{
trace!("{:?} auth resp pass <{}>: \"{}\"", who, req_id, passhash);

@ -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

@ -9,14 +9,14 @@ use tokio::{
#[derive(Debug)]
pub struct State
{
backend: RwLock<server::state::ServerState>,
settings: settings::Settings,
backend: RwLock<ServerState>,
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
}

Loading…
Cancel
Save