From 253e5850589d7d0193caaec25ff0728956c171ba Mon Sep 17 00:00:00 2001 From: hatoo Date: Fri, 8 Nov 2024 00:28:22 +0900 Subject: [PATCH 1/3] clean types --- src/lib.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5e533d9..98bc646 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ use hyper::{ }; use hyper_util::rt::{TokioExecutor, TokioIo}; use moka::sync::Cache; -use std::{borrow::Borrow, future::Future, sync::Arc}; +use std::{borrow::Borrow, convert::Infallible, future::Future, sync::Arc}; use tls::{generate_cert, CertifiedKeyDer}; use tokio::net::{TcpListener, TcpStream, ToSocketAddrs}; @@ -53,20 +53,17 @@ impl MitmProxy { impl + Send + Sync + 'static> MitmProxy { /// Bind to a socket address and return a future that runs the proxy server. /// URL for requests that passed to service are full URL including scheme. - pub async fn bind( + pub async fn bind( self, addr: A, service: S, ) -> Result, std::io::Error> where - B: Body + Send + Sync + 'static, + S: HttpService + Send + Clone + 'static, + B::Data: Send + 'static, + B: Body + Send + Sync + 'static, + B::Error: Into>, E: std::error::Error + Send + Sync + 'static, - E2: std::error::Error + Send + Sync + 'static, - S: HttpService - + Send - + Sync - + Clone - + 'static, { let listener = TcpListener::bind(addr).await?; @@ -104,15 +101,16 @@ impl + Send + Sync + 'static> MitmProxy { /// See `examples/https.rs` for usage. /// If you want to serve simple HTTP proxy server, you can use `bind` method instead. /// `bind` will call this method internally. - pub fn wrap_service( + pub fn wrap_service( proxy: Arc, service: S, - ) -> impl HttpService, Error = E2, Future: Send> + ) -> impl HttpService, Error = E, Future: Send> where - S: HttpService + Send + Clone + 'static, - B: Body + Send + Sync + 'static, + S: HttpService + Send + Clone + 'static, + B::Data: Send + 'static, + B: Body + Send + Sync + 'static, + B::Error: Into>, E: std::error::Error + Send + Sync + 'static, - E2: std::error::Error + Send + Sync + 'static, { service_fn(move |req| { let proxy = proxy.clone(); @@ -126,7 +124,8 @@ impl + Send + Sync + 'static> MitmProxy { "Bad CONNECT request: {}, Reason: Invalid Authority", req.uri() ); - return Ok(no_body(StatusCode::BAD_REQUEST)); + return Ok(no_body(StatusCode::BAD_REQUEST) + .map(|b| b.boxed().map_err(|never| match never {}).boxed())); }; tokio::spawn(async move { @@ -259,8 +258,8 @@ impl + Send + Sync + 'static> MitmProxy { } } -fn no_body(status: StatusCode) -> Response> { - let mut res = Response::new(Empty::new().map_err(|never| match never {}).boxed()); +fn no_body(status: StatusCode) -> Response> { + let mut res = Response::new(Empty::new()); *res.status_mut() = status; res } From dff0f6ef4d3e6b22704d085c78b2a07b9e8e0997 Mon Sep 17 00:00:00 2001 From: hatoo Date: Fri, 8 Nov 2024 00:29:58 +0900 Subject: [PATCH 2/3] wip --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 98bc646..baf610c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,7 +104,7 @@ impl + Send + Sync + 'static> MitmProxy { pub fn wrap_service( proxy: Arc, service: S, - ) -> impl HttpService, Error = E, Future: Send> + ) -> impl HttpService, Future: Send> where S: HttpService + Send + Clone + 'static, B::Data: Send + 'static, From f9b577cf63a415760eac2096470f6b429c54ad78 Mon Sep 17 00:00:00 2001 From: hatoo Date: Fri, 8 Nov 2024 00:35:38 +0900 Subject: [PATCH 3/3] clean --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index baf610c..26c57e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ #![doc = include_str!("../README.md")] -use bytes::Bytes; use http_body_util::{combinators::BoxBody, BodyExt, Empty}; use hyper::{ body::{Body, Incoming}, @@ -10,7 +9,7 @@ use hyper::{ }; use hyper_util::rt::{TokioExecutor, TokioIo}; use moka::sync::Cache; -use std::{borrow::Borrow, convert::Infallible, future::Future, sync::Arc}; +use std::{borrow::Borrow, future::Future, sync::Arc}; use tls::{generate_cert, CertifiedKeyDer}; use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};