From 2bc557bbddb01b535dd8512fe3aadb0d4091a42d Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 28 Aug 2023 14:52:24 +0200 Subject: update all deps, including a new horrible version hyper --- src/main.rs | 67 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 31 insertions(+), 36 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index aa13609..5230434 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ pub mod config; pub mod error; pub mod files; +pub mod helper; pub mod limiter; pub mod proxy; @@ -15,6 +16,7 @@ use crate::{ use anyhow::{anyhow, bail, Context, Result}; use error::ServiceError; use futures::future::try_join_all; +use helper::TokioIo; use http_body_util::{combinators::BoxBody, BodyExt}; use hyper::{ body::Incoming, @@ -27,11 +29,7 @@ use hyper::{ use limiter::Limiter; use log::{debug, error, info, warn}; use std::{fs::File, io::BufReader, net::SocketAddr, path::Path, process::exit, sync::Arc}; -use tokio::{ - io::{AsyncRead, AsyncWrite}, - net::TcpListener, - signal::ctrl_c, -}; +use tokio::{net::TcpListener, signal::ctrl_c}; use tokio_rustls::TlsAcceptor; pub struct State { @@ -90,18 +88,17 @@ async fn serve_http(state: Arc) -> Result<()> { None => return Ok(()), }; - let listen_futs: Result> = try_join_all(http_config.bind - .iter() - .map(|e| async { - let l = TcpListener::bind(e.clone()).await?; - loop { - let (stream, addr) = l.accept().await.context("accepting connection")?; - debug!("connection from {addr}"); - let config = state.clone(); - tokio::spawn(async move { serve_stream(config, stream, addr).await }); - } - })) - .await; + let listen_futs: Result> = try_join_all(http_config.bind.iter().map(|e| async { + let l = TcpListener::bind(e.clone()).await?; + loop { + let (stream, addr) = l.accept().await.context("accepting connection")?; + debug!("connection from {addr}"); + let stream = TokioIo(stream); + let config = state.clone(); + tokio::spawn(async move { serve_stream(config, stream, addr).await }); + } + })) + .await; info!("serving http"); @@ -129,31 +126,29 @@ async fn serve_https(state: Arc) -> Result<()> { }; let tls_acceptor = Arc::new(TlsAcceptor::from(tls_config)); - let listen_futs: Result> = try_join_all(https_config.bind - .iter() - .map(|e| async { - let l = TcpListener::bind(e.clone()).await?; - loop { - let (stream, addr) = l.accept().await.context("accepting connection")?; - let state = state.clone(); - let tls_acceptor = tls_acceptor.clone(); - tokio::task::spawn(async move { - debug!("connection from {addr}"); - match tls_acceptor.accept(stream).await { - Ok(stream) => serve_stream(state, stream, addr).await, - Err(e) => warn!("error accepting tls: {e}"), - }; - }); - } - })) - .await; + let listen_futs: Result> = try_join_all(https_config.bind.iter().map(|e| async { + let l = TcpListener::bind(e.clone()).await?; + loop { + let (stream, addr) = l.accept().await.context("accepting connection")?; + let state = state.clone(); + let tls_acceptor = tls_acceptor.clone(); + tokio::task::spawn(async move { + debug!("connection from {addr}"); + match tls_acceptor.accept(stream).await { + Ok(stream) => serve_stream(state, TokioIo(stream), addr).await, + Err(e) => warn!("error accepting tls: {e}"), + }; + }); + } + })) + .await; info!("serving https"); listen_futs?; Ok(()) } -pub async fn serve_stream( +pub async fn serve_stream( state: Arc, stream: T, addr: SocketAddr, -- cgit v1.2.3-70-g09d2