Something went wrong. Try again.
A small file server written in rust to share images/videos (aimed at discord)
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465use axum::{ extract::{Request, State}, http::StatusCode, middleware::{self, Next}, response::{IntoResponse, Response}, routing::{get, post}, Router,};// use axum_macros::debug_middleware;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.clone(), key_check_auth, ))}
// #[debug_middleware]pub async fn key_check_auth( State(state): State<SharedState>, req: Request, next: Next,) -> Response { let headers = req.headers();
let bits = match headers.get("magic_bits") { Some(bits) => bits.to_str().unwrap_or_default(), None => { warn!("Somebody hit auth with no magic_bits"); return BAD_AUTH.into_response(); } };
let user = { let auth = &state.read().auth; let user = auth.users.iter().find(|user| user.magic_bit == bits); if user.is_none() { warn!( "Somebody attempted to use the following magic bits: {}", bits ); return BAD_AUTH.into_response(); }
user.unwrap().clone() };
info!("{} has authed", user.name); next.run(req).await}