Something went wrong. Try again.
A small file server written in rust to share images/videos (aimed at discord)
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859use axum::{ extract::{Request, State}, http::StatusCode, middleware::{self, Next}, response::{IntoResponse, Response}, routing::{get, post}, Router,};use tracing::{info, warn};use view::view;
use crate::SharedState;
use self::upload::upload;
mod upload;mod view;
const BAD_AUTH: (StatusCode, &str) = (StatusCode::BAD_REQUEST, "No auth, no service :3");
pub fn add_routes(state: SharedState) -> Router<SharedState> { Router::new() .route("/upload", post(upload)) .route("/view", get(view)) .layer(middleware::from_fn_with_state(state, key_check_auth))}
pub async fn key_check_auth( State(state): State<SharedState>, req: Request, next: Next,) -> Response { let headers = req.headers();
let bits = if let Some(bits) = headers.get("magic-bits") { bits.to_str().unwrap_or_default() } else { warn!("Somebody hit auth with no magic-bits"); return BAD_AUTH.into_response(); };
{ let auth = &state.read().auth; let user = auth.users.iter().find(|user| user.magic_bit == bits);
if let Some(user) = user { info!("{} has authed", user.name); } else { warn!( "Somebody attempted to use the following magic bits: {}", bits ); return BAD_AUTH.into_response(); } }
next.run(req).await}