add 404 page for incorrect download codes

This commit is contained in:
xenofem 2022-05-03 18:31:50 -04:00
parent 106c99d398
commit 9d87934cf4
8 changed files with 92 additions and 36 deletions

View file

@ -5,8 +5,10 @@ mod zip;
use std::{fmt::Debug, fs::File, path::PathBuf, str::FromStr};
use actix_files::NamedFile;
use actix_web::{
get, middleware::Logger, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder,
get, http::StatusCode, middleware::Logger, post, web, App, HttpRequest, HttpResponse,
HttpServer, Responder,
};
use actix_web_actors::ws;
use bytesize::ByteSize;
@ -27,6 +29,7 @@ struct Config {
max_lifetime: u16,
upload_password: String,
storage_dir: PathBuf,
static_dir: PathBuf,
reverse_proxy: bool,
mnemonic_codes: bool,
}
@ -57,14 +60,11 @@ async fn handle_download(
download: web::Query<DownloadRequest>,
data: web::Data<AppState>,
) -> actix_web::Result<HttpResponse> {
let ip_addr = get_ip_addr(&req, data.config.reverse_proxy);
let code = &download.code;
if !store::is_valid_storage_code(code) {
log_auth_failure(&ip_addr);
return Ok(HttpResponse::NotFound().finish());
return download_not_found(req, data);
}
let file_store = data.file_store.read().await;
let info = file_store.lookup_file(code);
let info = data.file_store.read().await.lookup_file(code);
if let Some(info) = info {
let storage_path = data.config.storage_dir.join(code);
let file = File::open(&storage_path)?;
@ -75,11 +75,21 @@ async fn handle_download(
}
.into_response(&req))
} else {
log_auth_failure(&ip_addr);
Ok(HttpResponse::NotFound().finish())
download_not_found(req, data)
}
}
fn download_not_found(
req: HttpRequest,
data: web::Data<AppState>,
) -> actix_web::Result<HttpResponse> {
let ip_addr = get_ip_addr(&req, data.config.reverse_proxy);
log_auth_failure(&ip_addr);
Ok(NamedFile::open(data.config.static_dir.join("404.html"))?
.set_status_code(StatusCode::NOT_FOUND)
.into_response(&req))
}
#[get("/upload")]
async fn handle_upload(
req: HttpRequest,
@ -183,6 +193,7 @@ async fn main() -> std::io::Result<()> {
max_lifetime,
upload_password,
storage_dir,
static_dir: static_dir.clone(),
reverse_proxy,
mnemonic_codes,
},