Serve PKI files
This commit is contained in:
93
central_backend/src/server/custom_error.rs
Normal file
93
central_backend/src/server/custom_error.rs
Normal file
@ -0,0 +1,93 @@
|
||||
use actix_web::body::BoxBody;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::HttpResponse;
|
||||
use std::error::Error;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::io::ErrorKind;
|
||||
|
||||
/// Custom error to ease controller writing
|
||||
#[derive(Debug)]
|
||||
pub enum HttpErr {
|
||||
Err(anyhow::Error),
|
||||
HTTPResponse(HttpResponse),
|
||||
}
|
||||
|
||||
impl Display for HttpErr {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
HttpErr::Err(err) => Display::fmt(err, f),
|
||||
HttpErr::HTTPResponse(res) => {
|
||||
Display::fmt(&format!("HTTP RESPONSE {}", res.status().as_str()), f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl actix_web::error::ResponseError for HttpErr {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match self {
|
||||
HttpErr::Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
HttpErr::HTTPResponse(r) => r.status(),
|
||||
}
|
||||
}
|
||||
fn error_response(&self) -> HttpResponse<BoxBody> {
|
||||
log::error!("Error while processing request! {}", self);
|
||||
|
||||
HttpResponse::InternalServerError().body("Failed to execute request!")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<anyhow::Error> for HttpErr {
|
||||
fn from(err: anyhow::Error) -> HttpErr {
|
||||
HttpErr::Err(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for HttpErr {
|
||||
fn from(value: serde_json::Error) -> Self {
|
||||
HttpErr::Err(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<dyn Error>> for HttpErr {
|
||||
fn from(value: Box<dyn Error>) -> Self {
|
||||
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for HttpErr {
|
||||
fn from(value: std::io::Error) -> Self {
|
||||
HttpErr::Err(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::num::ParseIntError> for HttpErr {
|
||||
fn from(value: std::num::ParseIntError) -> Self {
|
||||
HttpErr::Err(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for HttpErr {
|
||||
fn from(value: reqwest::Error) -> Self {
|
||||
HttpErr::Err(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<reqwest::header::ToStrError> for HttpErr {
|
||||
fn from(value: reqwest::header::ToStrError) -> Self {
|
||||
HttpErr::Err(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<actix_web::Error> for HttpErr {
|
||||
fn from(value: actix_web::Error) -> Self {
|
||||
HttpErr::Err(std::io::Error::new(ErrorKind::Other, value.to_string()).into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HttpResponse> for HttpErr {
|
||||
fn from(value: HttpResponse) -> Self {
|
||||
HttpErr::HTTPResponse(value)
|
||||
}
|
||||
}
|
||||
pub type HttpResult = Result<HttpResponse, HttpErr>;
|
@ -1,22 +1,30 @@
|
||||
use actix_web::middleware::Logger;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use openssl::ssl::{SslAcceptor, SslMethod};
|
||||
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::crypto::pki;
|
||||
|
||||
pub mod custom_error;
|
||||
pub mod pki_controller;
|
||||
pub mod server_controller;
|
||||
|
||||
/// Start unsecure (HTTP) server
|
||||
pub async fn unsecure_server() -> anyhow::Result<()> {
|
||||
log::info!(
|
||||
"Unecure server starting to listen on {} for {}",
|
||||
"Unsecure server starting to listen on {} for {}",
|
||||
AppConfig::get().unsecure_listen_address,
|
||||
AppConfig::get().unsecure_origin()
|
||||
);
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(server_controller::unsecure_home)))
|
||||
.bind(&AppConfig::get().unsecure_listen_address)?
|
||||
.run()
|
||||
.await?;
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.route("/", web::get().to(server_controller::unsecure_home))
|
||||
.route("/pki/{file}", web::get().to(pki_controller::serve_pki_file))
|
||||
})
|
||||
.bind(&AppConfig::get().unsecure_listen_address)?
|
||||
.run()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -36,10 +44,14 @@ pub async fn secure_server() -> anyhow::Result<()> {
|
||||
AppConfig::get().listen_address,
|
||||
AppConfig::get().secure_origin()
|
||||
);
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(server_controller::secure_home)))
|
||||
.bind_openssl(&AppConfig::get().listen_address, builder)?
|
||||
.run()
|
||||
.await?;
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.route("/", web::get().to(server_controller::secure_home))
|
||||
})
|
||||
.bind_openssl(&AppConfig::get().listen_address, builder)?
|
||||
.run()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
32
central_backend/src/server/pki_controller.rs
Normal file
32
central_backend/src/server/pki_controller.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::server::custom_error::HttpResult;
|
||||
use actix_web::{web, HttpResponse};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct ServeCRLPath {
|
||||
file: String,
|
||||
}
|
||||
|
||||
/// Serve PKI files (unsecure server)
|
||||
pub async fn serve_pki_file(path: web::Path<ServeCRLPath>) -> HttpResult {
|
||||
for f in std::fs::read_dir(AppConfig::get().pki_path())? {
|
||||
let f = f?;
|
||||
let file_name = f.file_name().to_string_lossy().to_string();
|
||||
if !file_name.ends_with(".crl") && !file_name.ends_with(".pem") {
|
||||
continue;
|
||||
}
|
||||
|
||||
if file_name != path.file {
|
||||
continue;
|
||||
}
|
||||
|
||||
let crl = std::fs::read(f.path())?;
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type("application/x-pem-file")
|
||||
.body(crl));
|
||||
}
|
||||
|
||||
Ok(HttpResponse::NotFound()
|
||||
.content_type("text/plain")
|
||||
.body("file not found!"))
|
||||
}
|
Reference in New Issue
Block a user