more helpers

This commit is contained in:
Cadey Ratio 2020-07-27 21:03:50 -04:00
parent b47c1a69a2
commit 04a0c8e988
2 changed files with 31 additions and 6 deletions

View File

@ -5,7 +5,9 @@ use maj::{
split, Response, split, Response,
}; };
use rustls::internal::pemfile::{certs, rsa_private_keys}; use rustls::internal::pemfile::{certs, rsa_private_keys};
use rustls::{Certificate, NoClientAuth, PrivateKey, ServerConfig}; use rustls::{
AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig,
};
use std::fs::File; use std::fs::File;
use std::io::{self, BufReader}; use std::io::{self, BufReader};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -62,7 +64,9 @@ fn main() -> Result<(), maj::server::Error> {
opts.port opts.port
); );
let mut config = ServerConfig::new(NoClientAuth::new()); let mut config = ServerConfig::new(AllowAnyAnonymousOrAuthenticatedClient::new(
RootCertStore::empty(),
));
config config
.set_single_cert(certs, keys.remove(0)) .set_single_cert(certs, keys.remove(0))
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
@ -83,16 +87,20 @@ struct Handler {
hostname: String, hostname: String,
} }
fn index(_req: Request) -> Result<maj::Response, maj::server::Error> { async fn index() -> Result<maj::Response, maj::server::Error> {
let msg = include_bytes!("index.gmi"); let msg = include_bytes!("index.gmi");
Ok(Response::gemini(msg.to_vec())) Ok(Response::gemini(msg.to_vec()))
} }
fn majc(_req: Request) -> Result<maj::Response, maj::server::Error> { async fn majc() -> Result<maj::Response, maj::server::Error> {
let msg = include_bytes!("majc.gmi"); let msg = include_bytes!("majc.gmi");
Ok(Response::gemini(msg.to_vec())) Ok(Response::gemini(msg.to_vec()))
} }
async fn need_cert() -> Result<Response, Error> {
Ok(Response::need_cert("test"))
}
#[async_trait::async_trait] #[async_trait::async_trait]
impl MajHandler for Handler { impl MajHandler for Handler {
async fn handle(&self, req: Request) -> Result<Response, Error> { async fn handle(&self, req: Request) -> Result<Response, Error> {
@ -108,8 +116,9 @@ impl MajHandler for Handler {
} }
route!(req.url.path(), { route!(req.url.path(), {
(/) => index(req); (/) => index().await;
(/"majc") => majc(req); (/"cert") => need_cert().await;
(/"majc") => majc().await;
}); });
Ok(Response::not_found()) Ok(Response::not_found())

View File

@ -53,6 +53,22 @@ impl Response {
body: vec![], body: vec![],
} }
} }
pub fn input<T: Into<String>>(msg: T) -> Response {
Response {
status: StatusCode::Input,
meta: msg.into(),
body: vec![],
}
}
pub fn need_cert<T: Into<String>>(msg: T) -> Response {
Response {
status: StatusCode::ClientCertificateRequired,
meta: msg.into(),
body: vec![],
}
}
} }
/// The parser state. /// The parser state.