Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc for upgraded connection #55

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ A HTTP proxy server library intended to be a backend of application like Burp pr
use std::path::PathBuf;

use clap::{Args, Parser};
use http_mitm_proxy::{DefaultClient, MitmProxy};
use futures::StreamExt;
use http_mitm_proxy::{default_client::Upgrade, DefaultClient, MitmProxy};
use moka::sync::Cache;
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -100,11 +101,36 @@ async fn main() {
let uri = req.uri().clone();

// You can modify request here
// or You can just return response anyware
// 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());
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.

// You can try https://echo.websocket.org/.ws to test websocket.
// But you need to disable alpn of DefaultClient to disable HTTP2 because echo.websocket.org does not support HTTP/2 for Websocket.
// It should be match incoming and outgoing HTTP version on DefaultClient, I'll fix this later. #54
println!("Upgrade connection");
let Upgrade {
mut client_to_server,
mut server_to_client,
} = upgrade;
let url = uri.to_string();
tokio::spawn(async move {
while let Some(data) = client_to_server.next().await {
println!("Client -> Server: {} {:?}", url, data);
}
});
let url = uri.to_string();
tokio::spawn(async move {
while let Some(data) = server_to_client.next().await {
println!("Server -> Client: {} {:?}", url, data);
}
});
}

// You can modify response here

Expand Down
32 changes: 29 additions & 3 deletions examples/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::path::PathBuf;

use clap::{Args, Parser};
use http_mitm_proxy::{DefaultClient, MitmProxy};
use futures::StreamExt;
use http_mitm_proxy::{default_client::Upgrade, DefaultClient, MitmProxy};
use moka::sync::Cache;
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -87,11 +88,36 @@ async fn main() {
let uri = req.uri().clone();

// You can modify request here
// or You can just return response anyware
// 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());
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.

// You can try https://echo.websocket.org/.ws to test websocket.
// But you need to disable alpn of DefaultClient to disable HTTP2 because echo.websocket.org does not support HTTP/2 for Websocket.
// It should be match incoming and outgoing HTTP version on DefaultClient, I'll fix this later. #54
println!("Upgrade connection");
let Upgrade {
mut client_to_server,
mut server_to_client,
} = upgrade;
let url = uri.to_string();
tokio::spawn(async move {
while let Some(data) = client_to_server.next().await {
println!("Client -> Server: {} {:?}", url, data);
}
});
let url = uri.to_string();
tokio::spawn(async move {
while let Some(data) = server_to_client.next().await {
println!("Server -> Client: {} {:?}", url, data);
}
});
}

// You can modify response here

Expand Down
1 change: 1 addition & 0 deletions src/default_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl DefaultClient {
}

/// Send a request and return a response.
/// If the response is an upgrade (= if status code is 101 Switching Protocols), it will return a response and an Upgrade struct.
/// Request should have a full URL including scheme.
pub async fn send_request<B>(
&self,
Expand Down
Loading