diff options
author | metamuffin <metamuffin@disroot.org> | 2023-02-24 13:23:58 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-02-24 13:23:58 +0100 |
commit | 3ed98e04da0917e790063549676729c7051d67f7 (patch) | |
tree | 81fbb2c93172a7cbb284aaeed8c98d3f6a7c26b5 /src | |
parent | c0d504f9ae77f99e5484e92e2e9d3f68561129c5 (diff) | |
download | gnix-3ed98e04da0917e790063549676729c7051d67f7.tar gnix-3ed98e04da0917e790063549676729c7051d67f7.tar.bz2 gnix-3ed98e04da0917e790063549676729c7051d67f7.tar.zst |
static file serving + bugs
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 13 | ||||
-rw-r--r-- | src/error.rs | 21 | ||||
-rw-r--r-- | src/files.rs | 193 | ||||
-rw-r--r-- | src/main.rs | 128 | ||||
-rw-r--r-- | src/proxy.rs | 100 |
5 files changed, 351 insertions, 104 deletions
diff --git a/src/config.rs b/src/config.rs index 210a1e6..58b885a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,8 +22,17 @@ pub struct HttpsConfig { } #[derive(Debug, Serialize, Deserialize)] -pub struct HostConfig { - pub backend: SocketAddr, +#[serde(untagged)] +pub enum HostConfig { + Backend { backend: SocketAddr }, + Files { files: FileserverConfig }, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct FileserverConfig { + pub root: PathBuf, + #[serde(default)] + pub index: bool, } impl Config { diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..cbeb6a6 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,21 @@ +#[derive(Debug, thiserror::Error)] +pub enum ServiceError { + #[error("hyper error")] + Hyper(hyper::Error), + #[error("unknown host")] + NoHost, + #[error("can't connect to the backend")] + CantConnect, + #[error("not found")] + NotFound, + #[error("io error: {0}")] + Io(std::io::Error), + #[error("ohh. i didn't expect that this error can be generated.")] + Other, +} + +impl From<std::io::Error> for ServiceError { + fn from(e: std::io::Error) -> Self { + Self::Io(e) + } +} diff --git a/src/files.rs b/src/files.rs new file mode 100644 index 0000000..e092994 --- /dev/null +++ b/src/files.rs @@ -0,0 +1,193 @@ +use crate::{config::FileserverConfig, ServiceError}; +use bytes::{Bytes, BytesMut}; +use futures_util::{future, future::Either, ready, stream, FutureExt, Stream, StreamExt}; +use http_body_util::{combinators::BoxBody, BodyExt, StreamBody}; +use humansize::FormatSizeOptions; +use hyper::{ + body::{Frame, Incoming}, + header::{CONTENT_TYPE, LOCATION}, + http::HeaderValue, + Request, Response, StatusCode, +}; +use log::debug; +use markup::Render; +use std::{fs::Metadata, io, path::Path, pin::Pin, task::Poll}; +use tokio::{ + fs::{read_to_string, File}, + io::AsyncSeekExt, +}; +use tokio_util::io::poll_read_buf; + +pub async fn serve_files( + req: Request<Incoming>, + config: &FileserverConfig, +) -> Result<hyper::Response<BoxBody<Bytes, ServiceError>>, ServiceError> { + let rpath = req.uri().path(); + + let mut path = config.root.clone(); + for seg in rpath.split("/") { + if seg == "" || seg == ".." { + continue; // not ideal + } + path.push(seg) + } + + if !path.exists() { + return Err(ServiceError::NotFound); + } + + if path.is_dir() { + if !config.index { + return Err(ServiceError::NotFound); + } + + if !rpath.ends_with("/") { + let mut r = Response::new(String::new()); + *r.status_mut() = StatusCode::FOUND; + r.headers_mut().insert( + LOCATION, + HeaderValue::from_str(&format!("{}/", rpath)).map_err(|_| ServiceError::Other)?, + ); + return Ok(r.map(|b| b.map_err(|e| match e {}).boxed())); + } + + return index(&path, rpath.to_string()).await.map(|s| { + let mut r = Response::new(s); + r.headers_mut() + .insert(CONTENT_TYPE, HeaderValue::from_static("text/html")); + r.map(|b| b.map_err(|e| match e {}).boxed()) + }); + } + + let file = File::open(path.clone()).await?; + + let mut r = Response::new(BoxBody::new(StreamBody::new( + StreamBody::new(file_stream(file, 4096, (0, u64::MAX))) + .map(|e| e.map(|e| Frame::data(e)).map_err(ServiceError::Io)), + ))); + + r.headers_mut().insert( + CONTENT_TYPE, + HeaderValue::from_str( + // no allocation possible here? + &mime_guess::from_path(path) + .first() + .map(|m| m.to_string()) + .unwrap_or("text/plain".to_string()), + ) + .unwrap(), + ); + Ok(r) +} + +// Adapted from warp (https://github.com/seanmonstar/warp/blob/master/src/filters/fs.rs). Thanks! +fn file_stream( + mut file: File, + buf_size: usize, + (start, end): (u64, u64), +) -> impl Stream<Item = Result<Bytes, io::Error>> + Send { + use std::io::SeekFrom; + + let seek = async move { + if start != 0 { + file.seek(SeekFrom::Start(start)).await?; + } + Ok(file) + }; + + seek.into_stream() + .map(move |result| { + let mut buf = BytesMut::new(); + let mut len = end - start; + let mut f = match result { + Ok(f) => f, + Err(f) => return Either::Left(stream::once(future::err(f))), + }; + + Either::Right(stream::poll_fn(move |cx| { + if len == 0 { + return Poll::Ready(None); + } + reserve_at_least(&mut buf, buf_size); + + let n = match ready!(poll_read_buf(Pin::new(&mut f), cx, &mut buf)) { + Ok(n) => n as u64, + Err(err) => { + debug!("file read error: {}", err); + return Poll::Ready(Some(Err(err))); + } + }; + + if n == 0 { + debug!("file read found EOF before expected length"); + return Poll::Ready(None); + } + + let mut chunk = buf.split().freeze(); + if n > len { + chunk = chunk.split_to(len as usize); + len = 0; + } else { + len -= n; + } + + Poll::Ready(Some(Ok(chunk))) + })) + }) + .flatten() +} + +fn reserve_at_least(buf: &mut BytesMut, cap: usize) { + if buf.capacity() - buf.len() < cap { + buf.reserve(cap); + } +} + +async fn index(path: &Path, rpath: String) -> Result<String, ServiceError> { + let files = path + .read_dir()? + .map(|e| e.and_then(|e| Ok((e.file_name().into_string().unwrap(), e.metadata()?)))) + .collect::<Result<Vec<_>, _>>()?; + if let Ok(indexhtml) = read_to_string(path.join("index.html")).await { + Ok(indexhtml) + } else { + let banner = read_to_string(path.join("index.banner.html")).await.ok(); + let mut s = String::new(); + IndexTemplate { + files, + banner, + path: rpath, + } + .render(&mut s) + .unwrap(); + Ok(s) + } +} + +markup::define! { + IndexTemplate(path: String, banner: Option<String>, files: Vec<(String, Metadata)>) { + @markup::doctype() + html { + head { + title { "Index of " @path } + } + body { + @if let Some(banner) = banner { + @markup::raw(banner) + } else { + h1 { "Index of " @path } + } + hr; + table { + @for (name, meta) in files { tr { + td { @if meta.file_type().is_dir() { "(dir)" } else { "(file)" } } + td { a[href=name] { @name } } + td { @humansize::format_size(meta.len(), FormatSizeOptions::default()) } + } } + } + hr; + footer { sub { "served by gnix" } } + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 2edbe3d..c325e61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,39 +1,35 @@ +#![feature(try_trait_v2)] + pub mod config; +pub mod error; +pub mod files; +pub mod proxy; -use crate::config::Config; +use crate::{ + config::{Config, HostConfig}, + files::serve_files, + proxy::proxy_request, +}; use anyhow::{anyhow, bail, Context, Result}; +use error::ServiceError; use http_body_util::{combinators::BoxBody, BodyExt}; use hyper::{ body::Incoming, - header::{HOST, UPGRADE}, - http::{ - uri::{PathAndQuery, Scheme}, - HeaderValue, - }, + header::{CONTENT_TYPE, HOST}, + http::HeaderValue, server::conn::http1, service::service_fn, - upgrade::OnUpgrade, - Request, Response, StatusCode, Uri, + Request, Response, StatusCode, }; -use log::{debug, error, info, warn}; +use log::{debug, info, warn}; use std::{fs::File, io::BufReader, net::SocketAddr, path::Path, sync::Arc}; use tokio::{ io::{AsyncRead, AsyncWrite}, - net::{TcpListener, TcpStream}, + net::TcpListener, signal::ctrl_c, }; use tokio_rustls::TlsAcceptor; -#[derive(Debug, thiserror::Error)] -enum ServiceError { - #[error("hyper error")] - Hyper(hyper::Error), - #[error("unknown host")] - NoHost, - #[error("can't connect to the backend")] - CantConnect, -} - #[tokio::main] async fn main() -> anyhow::Result<()> { env_logger::init_from_env("LOG"); @@ -60,7 +56,7 @@ async fn serve_http(config: Arc<Config>) -> Result<()> { let (stream, addr) = listener.accept().await.context("accepting connection")?; debug!("connection from {addr}"); let config = config.clone(); - tokio::spawn(async move { serve_stream(config, stream, addr) }); + tokio::spawn(async move { serve_stream(config, stream, addr).await }); } } async fn serve_https(config: Arc<Config>) -> Result<()> { @@ -114,10 +110,11 @@ pub async fn serve_stream<T: AsyncRead + AsyncWrite + Unpin + Send + 'static>( Ok(r) => Ok(r), Err(ServiceError::Hyper(e)) => Err(e), Err(error) => Ok({ - let mut resp = Response::new(format!( - "the reverse proxy encountered an issue: {error}" - )); + let mut resp = + Response::new(format!("gnix encountered an issue: {error}")); *resp.status_mut() = StatusCode::BAD_REQUEST; + resp.headers_mut() + .insert(CONTENT_TYPE, HeaderValue::from_static("text/plain")); resp } .map(|b| b.map_err(|e| match e {}).boxed())), @@ -148,34 +145,10 @@ fn load_private_key(path: &Path) -> anyhow::Result<rustls::PrivateKey> { async fn service( config: Arc<Config>, - mut req: Request<Incoming>, + req: Request<Incoming>, addr: SocketAddr, -) -> Result<hyper::Response<BoxBody<bytes::Bytes, hyper::Error>>, ServiceError> { - let scheme_secure = req.uri().scheme() == Some(&Scheme::HTTPS); +) -> Result<hyper::Response<BoxBody<bytes::Bytes, ServiceError>>, ServiceError> { debug!("{addr} ~> {:?} {}", req.headers().get(HOST), req.uri()); - *req.uri_mut() = Uri::builder() - .path_and_query( - req.uri() - .clone() - .path_and_query() - .cloned() - .unwrap_or(PathAndQuery::from_static("/")), - ) - .build() - .unwrap(); - - req.headers_mut().insert( - "x-forwarded-for", - HeaderValue::from_str(&format!("{addr}")).unwrap(), - ); - req.headers_mut().insert( - "x-forwarded-proto", - if scheme_secure { - HeaderValue::from_static("https") - } else { - HeaderValue::from_static("http") - }, - ); let route = config .hosts @@ -188,59 +161,10 @@ async fn service( )) .ok_or(ServiceError::NoHost)?; - let do_upgrade = req.headers().contains_key(UPGRADE); - let on_upgrade_downstream = req.extensions_mut().remove::<OnUpgrade>(); - - debug!("\tforwarding to {}", route.backend); - let mut resp = { - let client_stream = TcpStream::connect(&route.backend) - .await - .map_err(|_| ServiceError::CantConnect)?; - - let (mut sender, conn) = hyper::client::conn::http1::handshake(client_stream) - .await - .map_err(ServiceError::Hyper)?; - tokio::task::spawn(async move { - if let Err(err) = conn.await { - warn!("connection failed: {:?}", err); - } - }); - sender - .send_request(req) - .await - .map_err(ServiceError::Hyper)? - }; - - resp.headers_mut() - .insert("server", HeaderValue::from_static("gnix")); - - if do_upgrade { - let on_upgrade_upstream = resp.extensions_mut().remove::<OnUpgrade>(); - tokio::task::spawn(async move { - debug!("about upgrading connection, sending empty response"); - match ( - on_upgrade_upstream.unwrap().await, - on_upgrade_downstream.unwrap().await, - ) { - (Ok(mut upgraded_upstream), Ok(mut upgraded_downstream)) => { - debug!("upgrade successful"); - match tokio::io::copy_bidirectional( - &mut upgraded_downstream, - &mut upgraded_upstream, - ) - .await - { - Ok((from_client, from_server)) => { - debug!("proxy socket terminated: {from_server} sent, {from_client} received") - } - Err(e) => warn!("proxy socket error: {e}"), - } - } - (a, b) => eprintln!("upgrade error: upstream={a:?} downstream={b:?}"), - } - }); + match route { + HostConfig::Backend { backend } => proxy_request(req, addr, backend).await, + HostConfig::Files { files } => serve_files(req, files).await, } - Ok(resp.map(|b| b.boxed())) } pub fn remove_port(s: &str) -> &str { diff --git a/src/proxy.rs b/src/proxy.rs new file mode 100644 index 0000000..65fc5a8 --- /dev/null +++ b/src/proxy.rs @@ -0,0 +1,100 @@ +use crate::ServiceError; +use http_body_util::{combinators::BoxBody, BodyExt}; +use hyper::{ + body::Incoming, + header::UPGRADE, + http::{ + uri::{PathAndQuery, Scheme}, + HeaderValue, + }, + upgrade::OnUpgrade, + Request, Uri, +}; +use log::{debug, error, warn}; +use std::net::SocketAddr; +use tokio::net::TcpStream; + +pub async fn proxy_request( + mut req: Request<Incoming>, + addr: SocketAddr, + backend: &SocketAddr, +) -> Result<hyper::Response<BoxBody<bytes::Bytes, ServiceError>>, ServiceError> { + let scheme_secure = req.uri().scheme() == Some(&Scheme::HTTPS); + *req.uri_mut() = Uri::builder() + .path_and_query( + req.uri() + .clone() + .path_and_query() + .cloned() + .unwrap_or(PathAndQuery::from_static("/")), + ) + .build() + .unwrap(); + + req.headers_mut().insert( + "x-forwarded-for", + HeaderValue::from_str(&format!("{addr}")).unwrap(), + ); + req.headers_mut().insert( + "x-forwarded-proto", + if scheme_secure { + HeaderValue::from_static("https") + } else { + HeaderValue::from_static("http") + }, + ); + + let do_upgrade = req.headers().contains_key(UPGRADE); + let on_upgrade_downstream = req.extensions_mut().remove::<OnUpgrade>(); + + debug!("\tforwarding to {}", backend); + let mut resp = { + let client_stream = TcpStream::connect(backend) + .await + .map_err(|_| ServiceError::CantConnect)?; + + let (mut sender, conn) = hyper::client::conn::http1::handshake(client_stream) + .await + .map_err(ServiceError::Hyper)?; + tokio::task::spawn(async move { + if let Err(err) = conn.await { + warn!("connection failed: {:?}", err); + } + }); + sender + .send_request(req) + .await + .map_err(ServiceError::Hyper)? + }; + + resp.headers_mut() + .insert("server", HeaderValue::from_static("gnix")); + + if do_upgrade { + let on_upgrade_upstream = resp.extensions_mut().remove::<OnUpgrade>(); + tokio::task::spawn(async move { + debug!("about upgrading connection, sending empty response"); + match ( + on_upgrade_upstream.unwrap().await, + on_upgrade_downstream.unwrap().await, + ) { + (Ok(mut upgraded_upstream), Ok(mut upgraded_downstream)) => { + debug!("upgrade successful"); + match tokio::io::copy_bidirectional( + &mut upgraded_downstream, + &mut upgraded_upstream, + ) + .await + { + Ok((from_client, from_server)) => { + debug!("proxy socket terminated: {from_server} sent, {from_client} received") + } + Err(e) => warn!("proxy socket error: {e}"), + } + } + (a, b) => error!("upgrade error: upstream={a:?} downstream={b:?}"), + } + }); + } + Ok(resp.map(|b| b.map_err(ServiceError::Hyper).boxed())) +} |