Skip to content

Commit

Permalink
feat(router): setters for direct key and certificate content
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuwn committed Jun 24, 2024
1 parent 98b347e commit 701cffd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = ["rossweisse"]

[package]
name = "windmark"
version = "0.3.9"
version = "0.3.10"
authors = ["Fuwn <contact@fuwn.me>"]
edition = "2021"
description = "An elegant and highly performant async Gemini server framework"
Expand Down
2 changes: 1 addition & 1 deletion src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Response {
#[must_use]
pub fn binary_success_auto(content: &[u8]) -> Self {
Self::new(22, String::from_utf8_lossy(content))
.with_mime(&tree_magic::from_u8(content))
.with_mime(tree_magic::from_u8(content))
.clone()
}

Expand Down
81 changes: 72 additions & 9 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ pub struct Router {
routes: matchit::Router<Arc<AsyncMutex<Box<dyn RouteResponse>>>>,
error_handler: Arc<AsyncMutex<Box<dyn ErrorResponse>>>,
private_key_file_name: String,
ca_file_name: String,
private_key_content: Option<String>,
certificate_file_name: String,
certificate_content: Option<String>,
headers: Arc<Mutex<Vec<Box<dyn Partial>>>>,
footers: Arc<Mutex<Vec<Box<dyn Partial>>>>,
ssl_acceptor: Arc<SslAcceptor>,
Expand Down Expand Up @@ -137,6 +139,22 @@ impl Router {
self
}

/// Set the content of the private key
///
/// # Examples
///
/// ```rust
/// windmark::router::Router::new().set_private_key("...");
/// ```
pub fn set_private_key(
&mut self,
private_key_content: impl Into<String> + AsRef<str>,
) -> &mut Self {
self.private_key_content = Some(private_key_content.into());

self
}

/// Set the filename of the certificate chain file.
///
/// # Examples
Expand All @@ -148,7 +166,23 @@ impl Router {
&mut self,
certificate_name: impl Into<String> + AsRef<str>,
) -> &mut Self {
self.ca_file_name = certificate_name.into();
self.certificate_file_name = certificate_name.into();

self
}

/// Set the content of the certificate chain file.
///
/// # Examples
///
/// ```rust
/// windmark::router::Router::new().set_certificate("...");
/// ```
pub fn set_certificate(
&mut self,
certificate_content: impl Into<String> + AsRef<str>,
) -> &mut Self {
self.certificate_content = Some(certificate_content.into());

self
}
Expand Down Expand Up @@ -340,7 +374,11 @@ impl Router {
// Ok(())
}

#[allow(clippy::too_many_lines)]
#[allow(
clippy::too_many_lines,
clippy::needless_pass_by_ref_mut,
clippy::significant_drop_in_scrutinee
)]
async fn handle(
&mut self,
stream: &mut Stream,
Expand Down Expand Up @@ -515,11 +553,34 @@ impl Router {
fn create_acceptor(&mut self) -> Result<(), Box<dyn Error>> {
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls())?;

builder.set_private_key_file(
&self.private_key_file_name,
ssl::SslFiletype::PEM,
)?;
builder.set_certificate_file(&self.ca_file_name, ssl::SslFiletype::PEM)?;
if self.certificate_content.is_some() {
builder.set_certificate(
openssl::x509::X509::from_pem(
self.certificate_content.clone().unwrap().as_bytes(),
)?
.as_ref(),
)?;
} else {
builder.set_certificate_file(
&self.certificate_file_name,
ssl::SslFiletype::PEM,
)?;
}

if self.private_key_content.is_some() {
builder.set_private_key(
openssl::pkey::PKey::private_key_from_pem(
self.private_key_content.clone().unwrap().as_bytes(),
)?
.as_ref(),
)?;
} else {
builder.set_private_key_file(
&self.private_key_file_name,
ssl::SslFiletype::PEM,
)?;
}

builder.check_private_key()?;
builder.set_verify_callback(ssl::SslVerifyMode::PEER, |_, _| true);
builder.set_session_id_context(
Expand Down Expand Up @@ -919,7 +980,7 @@ impl Default for Router {
}
}))),
private_key_file_name: String::new(),
ca_file_name: String::new(),
certificate_file_name: String::new(),
headers: Arc::new(Mutex::new(vec![])),
footers: Arc::new(Mutex::new(vec![])),
ssl_acceptor: Arc::new(
Expand All @@ -939,6 +1000,8 @@ impl Default for Router {
modules: Arc::new(Mutex::new(vec![])),
async_modules: Arc::new(AsyncMutex::new(vec![])),
fix_path: false,
private_key_content: None,
certificate_content: None,
}
}
}

0 comments on commit 701cffd

Please sign in to comment.