Skip to content

Commit

Permalink
feature gate
Browse files Browse the repository at this point in the history
  • Loading branch information
hatoo committed Oct 30, 2024
1 parent 437ff08 commit 2dfa056
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ keywords = ["http", "proxy", "http-proxy"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
websocket = ["dep:winnow"]

[dependencies]
tokio = { version = "1.39.3", features = [
"macros",
Expand All @@ -35,11 +38,15 @@ hyper-util = { version = "0.1.7", features = ["tokio"] }
native-tls = { version = "0.2.12", features = ["alpn"] }
thiserror = "1.0.62"
moka = { version = "0.12.8", features = ["sync"] }
winnow = "0.6.20"
winnow = { version = "0.6.20", optional = true }

[dev-dependencies]
axum = { version = "0.7.2", features = ["http2"] }
clap = { version = "4.5.16", features = ["derive"] }
rcgen = { version = "0.13.1", features = ["x509-parser"] }
reqwest = { version = "0.12.7", features = ["native-tls-alpn"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

[[example]]
name = "proxy"
required-features = ["websocket"]
39 changes: 35 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use std::path::PathBuf;
use clap::{Args, Parser};
use futures::StreamExt;
use http_mitm_proxy::{default_client::Upgrade, DefaultClient, MitmProxy};
use http_mitm_proxy::{
default_client::{websocket, Upgrade},
DefaultClient, MitmProxy,
};
use moka::sync::Cache;
use tracing_subscriber::EnvFilter;
Expand Down Expand Up @@ -99,7 +102,7 @@ async fn main() {
let (res, upgrade) = client.send_request(req).await?;
println!("{} -> {}", uri, res.status());
// println!("{} -> {}", uri, res.status());
if let Some(upgrade) = upgrade {
// If the response is an upgrade, e.g. Websocket, you can see traffic.
// Modifying upgraded traffic is not supported yet.
Expand All @@ -112,14 +115,42 @@ async fn main() {
} = upgrade;
let url = uri.to_string();
tokio::spawn(async move {
let mut buf = Vec::new();
while let Some(data) = client_to_server.next().await {
println!("Client -> Server: {} {:?}", url, data);
buf.extend(data);
loop {
let input = &mut buf.as_slice();
if let Ok(frame) = websocket::frame(input) {
println!(
"Client -> Server: {} {:?}",
url,
String::from_utf8_lossy(&frame.payload_data)
);
buf = input.to_vec();
} else {
break;
}
}
}
});
let url = uri.to_string();
tokio::spawn(async move {
let mut buf = Vec::new();
while let Some(data) = server_to_client.next().await {
println!("Server -> Client: {} {:?}", url, data);
buf.extend(data);
loop {
let input = &mut buf.as_slice();
if let Ok(frame) = websocket::frame(input) {
println!(
"Server -> Client: {} {:?}",
url,
String::from_utf8_lossy(&frame.payload_data)
);
buf = input.to_vec();
} else {
break;
}
}
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/default_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ fn remove_authority<B>(req: &mut Request<B>) {
*req.uri_mut() = Uri::from_parts(parts).unwrap();
}

#[cfg(feature = "websocket")]
pub mod websocket {
/*
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
Expand Down

0 comments on commit 2dfa056

Please sign in to comment.