clippy and format
This commit is contained in:
		
							parent
							
								
									2e29825a3d
								
							
						
					
					
						commit
						8b001911f7
					
				
					 3 changed files with 53 additions and 24 deletions
				
			
		
							
								
								
									
										58
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										58
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -4,13 +4,13 @@ mod store;
 | 
			
		|||
mod upload;
 | 
			
		||||
mod zip;
 | 
			
		||||
 | 
			
		||||
use std::{fmt::Debug, path::PathBuf, str::FromStr, ops::Deref};
 | 
			
		||||
use std::{fmt::Debug, ops::Deref, path::PathBuf, str::FromStr};
 | 
			
		||||
 | 
			
		||||
use actix_http::StatusCode;
 | 
			
		||||
use actix_session::{SessionMiddleware, storage::CookieSessionStore, Session};
 | 
			
		||||
use actix_session::{storage::CookieSessionStore, Session, SessionMiddleware};
 | 
			
		||||
use actix_web::{
 | 
			
		||||
    error::InternalError, get, middleware::Logger, post, web, App, HttpRequest, HttpResponse,
 | 
			
		||||
    HttpServer, Responder, cookie,
 | 
			
		||||
    cookie, error::InternalError, get, middleware::Logger, post, web, App, HttpRequest,
 | 
			
		||||
    HttpResponse, HttpServer, Responder,
 | 
			
		||||
};
 | 
			
		||||
use actix_web_actors::ws;
 | 
			
		||||
use argon2::{Argon2, PasswordVerifier};
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +19,7 @@ use bytesize::ByteSize;
 | 
			
		|||
use log::{error, warn};
 | 
			
		||||
use password_hash::PasswordHashString;
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use state::{StateDb, prelude::SizedFile};
 | 
			
		||||
use state::{prelude::SizedFile, StateDb};
 | 
			
		||||
use store::{StoredFile, StoredFiles};
 | 
			
		||||
use tokio::fs::File;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -93,19 +93,24 @@ struct AdminPage<'a> {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
#[get("/admin")]
 | 
			
		||||
async fn admin_panel(data: web::Data<AppData>, session: Session) -> actix_web::Result<HttpResponse> {
 | 
			
		||||
async fn admin_panel(
 | 
			
		||||
    data: web::Data<AppData>,
 | 
			
		||||
    session: Session,
 | 
			
		||||
) -> actix_web::Result<HttpResponse> {
 | 
			
		||||
    if let Some(true) = session.get::<bool>("admin")? {
 | 
			
		||||
        Ok(AdminPage {
 | 
			
		||||
            cachebuster: data.config.cachebuster.clone(),
 | 
			
		||||
            base_url: data.config.base_url.clone(),
 | 
			
		||||
            stored_files: data.state.read().await.deref(),
 | 
			
		||||
        }.to_response())
 | 
			
		||||
        }
 | 
			
		||||
        .to_response())
 | 
			
		||||
    } else {
 | 
			
		||||
        Ok(SignedOutAdminPage {
 | 
			
		||||
            cachebuster: data.config.cachebuster.clone(),
 | 
			
		||||
            base_url: data.config.base_url.clone(),
 | 
			
		||||
            incorrect_password: false,
 | 
			
		||||
        }.to_response())
 | 
			
		||||
        }
 | 
			
		||||
        .to_response())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -115,14 +120,26 @@ struct AdminPanelSignin {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
#[post("/admin")]
 | 
			
		||||
async fn admin_signin(req: HttpRequest, data: web::Data<AppData>, form: web::Form<AdminPanelSignin>, session: Session) -> actix_web::Result<HttpResponse> {
 | 
			
		||||
    if Argon2::default().verify_password(form.password.as_bytes(), &data.config.admin_password_hash.password_hash()).is_ok() {
 | 
			
		||||
async fn admin_signin(
 | 
			
		||||
    req: HttpRequest,
 | 
			
		||||
    data: web::Data<AppData>,
 | 
			
		||||
    form: web::Form<AdminPanelSignin>,
 | 
			
		||||
    session: Session,
 | 
			
		||||
) -> actix_web::Result<HttpResponse> {
 | 
			
		||||
    if Argon2::default()
 | 
			
		||||
        .verify_password(
 | 
			
		||||
            form.password.as_bytes(),
 | 
			
		||||
            &data.config.admin_password_hash.password_hash(),
 | 
			
		||||
        )
 | 
			
		||||
        .is_ok()
 | 
			
		||||
    {
 | 
			
		||||
        session.insert("admin", true)?;
 | 
			
		||||
        Ok(AdminPage {
 | 
			
		||||
            cachebuster: data.config.cachebuster.clone(),
 | 
			
		||||
            base_url: data.config.base_url.clone(),
 | 
			
		||||
            stored_files: data.state.read().await.deref(),
 | 
			
		||||
        }.to_response())
 | 
			
		||||
        }
 | 
			
		||||
        .to_response())
 | 
			
		||||
    } else {
 | 
			
		||||
        let ip_addr = get_ip_addr(&req, data.config.reverse_proxy);
 | 
			
		||||
        log_auth_failure(&ip_addr);
 | 
			
		||||
| 
						 | 
				
			
			@ -130,7 +147,8 @@ async fn admin_signin(req: HttpRequest, data: web::Data<AppData>, form: web::For
 | 
			
		|||
            cachebuster: data.config.cachebuster.clone(),
 | 
			
		||||
            base_url: data.config.base_url.clone(),
 | 
			
		||||
            incorrect_password: true,
 | 
			
		||||
        }.to_response();
 | 
			
		||||
        }
 | 
			
		||||
        .to_response();
 | 
			
		||||
        *resp.status_mut() = StatusCode::FORBIDDEN;
 | 
			
		||||
 | 
			
		||||
        Err(InternalError::from_response("Incorrect password", resp).into())
 | 
			
		||||
| 
						 | 
				
			
			@ -375,12 +393,13 @@ async fn main() -> std::io::Result<()> {
 | 
			
		|||
    let admin_password_hash: PasswordHashString = env_or_panic("TRANSBEAM_ADMIN_PASSWORD_HASH");
 | 
			
		||||
 | 
			
		||||
    let cookie_secret_base64: String = env_or_panic("TRANSBEAM_COOKIE_SECRET");
 | 
			
		||||
    let cookie_key = cookie::Key::from(
 | 
			
		||||
        &base64::decode(&cookie_secret_base64)
 | 
			
		||||
            .unwrap_or_else(
 | 
			
		||||
                |_| panic!("Value {} for TRANSBEAM_COOKIE_SECRET is not valid base64", cookie_secret_base64)
 | 
			
		||||
    let cookie_key =
 | 
			
		||||
        cookie::Key::from(&base64::decode(&cookie_secret_base64).unwrap_or_else(|_| {
 | 
			
		||||
            panic!(
 | 
			
		||||
                "Value {} for TRANSBEAM_COOKIE_SECRET is not valid base64",
 | 
			
		||||
                cookie_secret_base64
 | 
			
		||||
            )
 | 
			
		||||
    );
 | 
			
		||||
        }));
 | 
			
		||||
 | 
			
		||||
    let state_file: PathBuf = match std::env::var("TRANSBEAM_STATE_FILE") {
 | 
			
		||||
        Ok(v) => v
 | 
			
		||||
| 
						 | 
				
			
			@ -424,7 +443,10 @@ async fn main() -> std::io::Result<()> {
 | 
			
		|||
            } else {
 | 
			
		||||
                Logger::default()
 | 
			
		||||
            })
 | 
			
		||||
            .wrap(SessionMiddleware::new(CookieSessionStore::default(), cookie_key.clone()))
 | 
			
		||||
            .wrap(SessionMiddleware::new(
 | 
			
		||||
                CookieSessionStore::default(),
 | 
			
		||||
                cookie_key.clone(),
 | 
			
		||||
            ))
 | 
			
		||||
            .service(index)
 | 
			
		||||
            .service(handle_download)
 | 
			
		||||
            .service(download_info)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue