aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs31
1 files changed, 22 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 807d0ab..07f3d5c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,19 +2,16 @@
#![feature(exclusive_range_pattern)]
#![feature(slice_split_once)]
-pub mod auth;
pub mod config;
pub mod error;
-pub mod files;
+pub mod filters;
pub mod helper;
-pub mod proxy;
#[cfg(feature = "mond")]
pub mod reporting;
use crate::{
config::{Config, RouteFilter},
- files::serve_files,
- proxy::proxy_request,
+ filters::{files::serve_files, proxy::proxy_request},
};
use anyhow::{bail, Context, Result};
use bytes::Bytes;
@@ -35,7 +32,7 @@ use log::{debug, error, info, warn};
#[cfg(feature = "mond")]
use reporting::Reporting;
use std::{
- fs::File,
+ collections::HashMap,
io::BufReader,
net::SocketAddr,
ops::ControlFlow,
@@ -45,6 +42,8 @@ use std::{
sync::Arc,
};
use tokio::{
+ fs::File,
+ io::BufWriter,
net::TcpListener,
signal::ctrl_c,
sync::{RwLock, Semaphore},
@@ -53,11 +52,13 @@ use tokio_rustls::TlsAcceptor;
pub struct State {
pub config: RwLock<Arc<Config>>,
+ pub access_logs: RwLock<HashMap<String, BufWriter<File>>>,
pub l_incoming: Semaphore,
pub l_outgoing: Semaphore,
#[cfg(feature = "mond")]
pub reporting: Reporting,
}
+pub struct HostState {}
pub type FilterRequest = Request<Incoming>;
pub type FilterResponseOut = Option<Response<BoxBody<Bytes, ServiceError>>>;
@@ -89,6 +90,7 @@ async fn main() -> anyhow::Result<()> {
#[cfg(feature = "mond")]
reporting: Reporting::new(&config),
config: RwLock::new(Arc::new(config)),
+ access_logs: Default::default(),
});
if state.config.read().await.watch_config {
@@ -228,12 +230,12 @@ pub async fn serve_stream<T: Unpin + Send + 'static + hyper::rt::Read + hyper::r
}
fn load_certs(path: &Path) -> anyhow::Result<Vec<rustls::Certificate>> {
- let mut reader = BufReader::new(File::open(path).context("reading tls certs")?);
+ let mut reader = BufReader::new(std::fs::File::open(path).context("reading tls certs")?);
let certs = rustls_pemfile::certs(&mut reader).context("parsing tls certs")?;
Ok(certs.into_iter().map(rustls::Certificate).collect())
}
fn load_private_key(path: &Path) -> anyhow::Result<rustls::PrivateKey> {
- let mut reader = BufReader::new(File::open(path).context("reading tls private key")?);
+ let mut reader = BufReader::new(std::fs::File::open(path).context("reading tls private key")?);
let keys =
rustls_pemfile::pkcs8_private_keys(&mut reader).context("parsing tls private key")?;
if keys.len() != 1 {
@@ -263,6 +265,7 @@ async fn service(
#[cfg(feature = "mond")]
state.reporting.hosts.get(host).unwrap().requests_in.inc();
+ // TODO this code is horrible
let mut req = Some(req);
let mut resp = None;
for filter in &route.0 {
@@ -285,11 +288,21 @@ async fn service(
);
ControlFlow::Continue(())
}
- RouteFilter::HttpBasicAuth { config } => auth::http_basic(
+ RouteFilter::HttpBasicAuth { config } => filters::auth::http_basic(
config,
req.as_ref().ok_or(ServiceError::RequestTaken)?,
&mut resp,
)?,
+ RouteFilter::AccessLog { config } => {
+ filters::accesslog::access_log(
+ &state,
+ host,
+ addr,
+ config,
+ req.as_ref().ok_or(ServiceError::RequestTaken)?,
+ )
+ .await?
+ }
};
match cf {
ControlFlow::Continue(_) => continue,