Skip to content

Commit

Permalink
Use Service trait
Browse files Browse the repository at this point in the history
  • Loading branch information
hatoo committed Nov 7, 2024
1 parent 83975dd commit 269f003
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 186 deletions.
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::path::PathBuf;
use clap::{Args, Parser};
use http_mitm_proxy::{DefaultClient, MitmProxy};
use hyper::service::service_fn;
use moka::sync::Cache;
use tracing_subscriber::EnvFilter;
Expand Down Expand Up @@ -88,23 +89,26 @@ async fn main() {
let client = DefaultClient::new().unwrap();
let server = proxy
.bind(("127.0.0.1", 3003), move |_client_addr, req| {
let client = client.clone();
async move {
let uri = req.uri().clone();
.bind(
("127.0.0.1", 3003),
service_fn(move |req| {
let client = client.clone();
async move {
let uri = req.uri().clone();
// You can modify request here
// or You can just return response anywhere
// You can modify request here
// or You can just return response anywhere
let (res, _upgrade) = client.send_request(req).await?;
let (res, _upgrade) = client.send_request(req).await?;
println!("{} -> {}", uri, res.status());
println!("{} -> {}", uri, res.status());
// You can modify response here
// You can modify response here
Ok::<_, http_mitm_proxy::default_client::Error>(res)
}
})
Ok::<_, http_mitm_proxy::default_client::Error>(res)
}
}),
)
.await
.unwrap();
Expand Down
87 changes: 45 additions & 42 deletions examples/dev_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bytes::Bytes;
use clap::{Args, Parser};
use http_body_util::{BodyExt, Full};
use http_mitm_proxy::{DefaultClient, MitmProxy};
use hyper::Response;
use hyper::{service::service_fn, Response};
use moka::sync::Cache;

#[derive(Parser)]
Expand Down Expand Up @@ -82,50 +82,53 @@ async fn main() {

let client = DefaultClient::new().unwrap();
let proxy = proxy
.bind(("127.0.0.1", 3003), move |_client_addr, mut req| {
let client = client.clone();
async move {
// Forward connection from http/https dev.example to http://127.0.0.1:3333
if req.uri().host() == Some("dev.example") {
// Return a response created by the proxy
if req.uri().path() == "/test.json" {
let res = Response::builder()
.header(hyper::header::CONTENT_TYPE, "application/json")
.body(
Full::new(Bytes::from("{data: 123}"))
.map_err(|e| match e {})
.boxed(),
)
.unwrap();
return Ok(res);
.bind(
("127.0.0.1", 3003),
service_fn(move |mut req| {
let client = client.clone();
async move {
// Forward connection from http/https dev.example to http://127.0.0.1:3333
if req.uri().host() == Some("dev.example") {
// Return a response created by the proxy
if req.uri().path() == "/test.json" {
let res = Response::builder()
.header(hyper::header::CONTENT_TYPE, "application/json")
.body(
Full::new(Bytes::from("{data: 123}"))
.map_err(|e| match e {})
.boxed(),
)
.unwrap();
return Ok(res);
}

req.headers_mut().insert(
hyper::header::HOST,
hyper::header::HeaderValue::from_maybe_shared(format!(
"127.0.0.1:{}",
port
))
.unwrap(),
);

let mut parts = req.uri().clone().into_parts();
parts.scheme = Some(hyper::http::uri::Scheme::HTTP);
parts.authority = Some(
hyper::http::uri::Authority::from_maybe_shared(format!(
"127.0.0.1:{}",
port
))
.unwrap(),
);
*req.uri_mut() = hyper::Uri::from_parts(parts).unwrap();
}

req.headers_mut().insert(
hyper::header::HOST,
hyper::header::HeaderValue::from_maybe_shared(format!(
"127.0.0.1:{}",
port
))
.unwrap(),
);

let mut parts = req.uri().clone().into_parts();
parts.scheme = Some(hyper::http::uri::Scheme::HTTP);
parts.authority = Some(
hyper::http::uri::Authority::from_maybe_shared(format!(
"127.0.0.1:{}",
port
))
.unwrap(),
);
*req.uri_mut() = hyper::Uri::from_parts(parts).unwrap();
}

let (res, _upgrade) = client.send_request(req).await?;
let (res, _upgrade) = client.send_request(req).await?;

Ok::<_, http_mitm_proxy::default_client::Error>(res.map(|b| b.boxed()))
}
})
Ok::<_, http_mitm_proxy::default_client::Error>(res.map(|b| b.boxed()))
}
}),
)
.await
.unwrap();

Expand Down
7 changes: 3 additions & 4 deletions examples/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async fn main() {

let server = async move {
loop {
let (stream, client_addr) = listener.accept().await.unwrap();
let (stream, _client_addr) = listener.accept().await.unwrap();
let proxy = proxy.clone();
let client = client.clone();
let tls_acceptor = tls_acceptor.clone();
Expand All @@ -111,9 +111,8 @@ async fn main() {

MitmProxy::hyper_service(
proxy.clone(),
client_addr,
req,
move |_client_addr, req| {
service_fn(move |req| {
let client = client.clone();
async move {
let uri = req.uri().clone();
Expand All @@ -129,7 +128,7 @@ async fn main() {

Ok::<_, http_mitm_proxy::default_client::Error>(res)
}
},
}),
)
});

Expand Down
28 changes: 16 additions & 12 deletions examples/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::PathBuf;

use clap::{Args, Parser};
use http_mitm_proxy::{DefaultClient, MitmProxy};
use hyper::service::service_fn;
use moka::sync::Cache;
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -75,23 +76,26 @@ async fn main() {

let client = DefaultClient::new().unwrap();
let server = proxy
.bind(("127.0.0.1", 3003), move |_client_addr, req| {
let client = client.clone();
async move {
let uri = req.uri().clone();
.bind(
("127.0.0.1", 3003),
service_fn(move |req| {
let client = client.clone();
async move {
let uri = req.uri().clone();

// You can modify request here
// or You can just return response anywhere
// You can modify request here
// or You can just return response anywhere

let (res, _upgrade) = client.send_request(req).await?;
let (res, _upgrade) = client.send_request(req).await?;

println!("{} -> {}", uri, res.status());
println!("{} -> {}", uri, res.status());

// You can modify response here
// You can modify response here

Ok::<_, http_mitm_proxy::default_client::Error>(res)
}
})
Ok::<_, http_mitm_proxy::default_client::Error>(res)
}
}),
)
.await
.unwrap();

Expand Down
Loading

0 comments on commit 269f003

Please sign in to comment.