maj/site/src/main.rs

136 lines
3.5 KiB
Rust
Raw Normal View History

2020-07-27 23:49:39 +00:00
use async_std::task;
2020-07-28 00:50:42 +00:00
use maj::{
2020-07-28 01:21:24 +00:00
gemini::Builder,
2020-07-28 00:50:42 +00:00
route, seg,
server::{Error, Handler as MajHandler, Request},
split, Response,
};
2020-07-27 23:49:39 +00:00
use rustls::internal::pemfile::{certs, rsa_private_keys};
2020-07-28 01:03:50 +00:00
use rustls::{
AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig,
};
2020-07-26 00:16:07 +00:00
use std::fs::File;
use std::io::{self, BufReader};
use std::path::{Path, PathBuf};
2020-07-27 23:49:39 +00:00
use std::sync::Arc;
2020-07-26 00:16:07 +00:00
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
struct Options {
/// host to listen on
#[structopt(short = "H", long, env = "HOST", default_value = "0.0.0.0")]
host: String,
/// port to listen on
#[structopt(short = "p", long, env = "PORT", default_value = "1965")]
port: u16,
/// cert file
#[structopt(short = "c", long = "cert", env = "CERT_FILE")]
cert: PathBuf,
/// key file
#[structopt(short = "k", long = "key", env = "KEY_FILE")]
key: PathBuf,
2020-07-26 12:21:02 +00:00
/// server hostname
#[structopt(
long = "hostname",
env = "SERVER_HOSTNAME",
default_value = "maj.kahless.cetacean.club"
)]
hostname: String,
2020-07-26 00:16:07 +00:00
}
fn load_certs(path: &Path) -> io::Result<Vec<Certificate>> {
certs(&mut BufReader::new(File::open(path)?))
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))
}
fn load_keys(path: &Path) -> io::Result<Vec<PrivateKey>> {
rsa_private_keys(&mut BufReader::new(File::open(path)?))
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
}
2020-07-27 23:49:39 +00:00
fn main() -> Result<(), maj::server::Error> {
2020-07-26 00:16:07 +00:00
pretty_env_logger::init();
let opts = Options::from_args();
let certs = load_certs(&opts.cert)?;
let mut keys = load_keys(&opts.key)?;
2020-07-28 00:50:42 +00:00
log::info!(
"serving gemini://{} on {}:{}",
opts.hostname,
opts.host,
opts.port
);
2020-07-26 12:21:02 +00:00
2020-07-28 01:03:50 +00:00
let mut config = ServerConfig::new(AllowAnyAnonymousOrAuthenticatedClient::new(
RootCertStore::empty(),
));
2020-07-26 00:16:07 +00:00
config
.set_single_cert(certs, keys.remove(0))
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
2020-07-27 23:49:39 +00:00
task::block_on(maj::server::serve(
Arc::new(Handler {
2020-07-26 12:21:02 +00:00
hostname: opts.hostname,
2020-07-27 23:49:39 +00:00
}),
2020-07-26 12:21:02 +00:00
config,
opts.host,
opts.port,
2020-07-27 23:49:39 +00:00
))?;
2020-07-26 00:16:07 +00:00
Ok(())
}
2020-07-26 12:21:02 +00:00
struct Handler {
hostname: String,
}
2020-07-26 00:16:07 +00:00
2020-07-28 01:03:50 +00:00
async fn index() -> Result<maj::Response, maj::server::Error> {
let msg = include_bytes!("index.gmi");
2020-07-28 00:50:42 +00:00
Ok(Response::gemini(msg.to_vec()))
2020-07-26 00:16:07 +00:00
}
2020-07-28 01:03:50 +00:00
async fn majc() -> Result<maj::Response, maj::server::Error> {
2020-07-26 12:21:02 +00:00
let msg = include_bytes!("majc.gmi");
2020-07-28 00:50:42 +00:00
Ok(Response::gemini(msg.to_vec()))
2020-07-26 12:21:02 +00:00
}
2020-07-28 01:21:24 +00:00
async fn need_cert(req: Request) -> Result<Response, Error> {
match req.certs {
None => Ok(Response::need_cert("test")),
Some(certs) => Ok(Response::render(
Builder::new()
.heading(1, "Cert test")
.text(format!("{:?}", certs))
.build(),
)),
}
2020-07-28 01:03:50 +00:00
}
2020-07-26 00:16:07 +00:00
#[async_trait::async_trait]
2020-07-28 00:50:42 +00:00
impl MajHandler for Handler {
async fn handle(&self, req: Request) -> Result<Response, Error> {
if req.url.has_host() && req.url.host_str().unwrap().to_string() != self.hostname {
return Ok(Response::no_proxy());
2020-07-26 12:21:02 +00:00
}
2020-07-28 00:50:42 +00:00
if req.url.path() == "" {
return Ok(Response::perm_redirect(format!(
"gemini://{}/",
self.hostname
)));
2020-07-26 00:16:07 +00:00
}
2020-07-28 00:50:42 +00:00
route!(req.url.path(), {
2020-07-28 01:03:50 +00:00
(/) => index().await;
2020-07-28 01:21:24 +00:00
(/"cert") => need_cert(req).await;
2020-07-28 01:03:50 +00:00
(/"majc") => majc().await;
2020-07-28 00:50:42 +00:00
});
Ok(Response::not_found())
2020-07-26 00:16:07 +00:00
}
}