Skip to content

Commit 122b5a2

Browse files
authored
Merge pull request #55 from hatoo/websocket
Add doc for upgraded connection
2 parents d98b823 + 77925a4 commit 122b5a2

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ A HTTP proxy server library intended to be a backend of application like Burp pr
1414
use std::path::PathBuf;
1515
1616
use clap::{Args, Parser};
17-
use http_mitm_proxy::{DefaultClient, MitmProxy};
17+
use futures::StreamExt;
18+
use http_mitm_proxy::{default_client::Upgrade, DefaultClient, MitmProxy};
1819
use moka::sync::Cache;
1920
use tracing_subscriber::EnvFilter;
2021
@@ -100,11 +101,36 @@ async fn main() {
100101
let uri = req.uri().clone();
101102
102103
// You can modify request here
103-
// or You can just return response anyware
104+
// or You can just return response anywhere
104105
105-
let (res, _upgrade) = client.send_request(req).await?;
106+
let (res, upgrade) = client.send_request(req).await?;
106107
107108
println!("{} -> {}", uri, res.status());
109+
if let Some(upgrade) = upgrade {
110+
// If the response is an upgrade, e.g. Websocket, you can see traffic.
111+
// Modifying upgraded traffic is not supported yet.
112+
113+
// You can try https://echo.websocket.org/.ws to test websocket.
114+
// But you need to disable alpn of DefaultClient to disable HTTP2 because echo.websocket.org does not support HTTP/2 for Websocket.
115+
// It should be match incoming and outgoing HTTP version on DefaultClient, I'll fix this later. #54
116+
println!("Upgrade connection");
117+
let Upgrade {
118+
mut client_to_server,
119+
mut server_to_client,
120+
} = upgrade;
121+
let url = uri.to_string();
122+
tokio::spawn(async move {
123+
while let Some(data) = client_to_server.next().await {
124+
println!("Client -> Server: {} {:?}", url, data);
125+
}
126+
});
127+
let url = uri.to_string();
128+
tokio::spawn(async move {
129+
while let Some(data) = server_to_client.next().await {
130+
println!("Server -> Client: {} {:?}", url, data);
131+
}
132+
});
133+
}
108134
109135
// You can modify response here
110136

examples/proxy.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::path::PathBuf;
22

33
use clap::{Args, Parser};
4-
use http_mitm_proxy::{DefaultClient, MitmProxy};
4+
use futures::StreamExt;
5+
use http_mitm_proxy::{default_client::Upgrade, DefaultClient, MitmProxy};
56
use moka::sync::Cache;
67
use tracing_subscriber::EnvFilter;
78

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

8990
// You can modify request here
90-
// or You can just return response anyware
91+
// or You can just return response anywhere
9192

92-
let (res, _upgrade) = client.send_request(req).await?;
93+
let (res, upgrade) = client.send_request(req).await?;
9394

9495
println!("{} -> {}", uri, res.status());
96+
if let Some(upgrade) = upgrade {
97+
// If the response is an upgrade, e.g. Websocket, you can see traffic.
98+
// Modifying upgraded traffic is not supported yet.
99+
100+
// You can try https://echo.websocket.org/.ws to test websocket.
101+
// But you need to disable alpn of DefaultClient to disable HTTP2 because echo.websocket.org does not support HTTP/2 for Websocket.
102+
// It should be match incoming and outgoing HTTP version on DefaultClient, I'll fix this later. #54
103+
println!("Upgrade connection");
104+
let Upgrade {
105+
mut client_to_server,
106+
mut server_to_client,
107+
} = upgrade;
108+
let url = uri.to_string();
109+
tokio::spawn(async move {
110+
while let Some(data) = client_to_server.next().await {
111+
println!("Client -> Server: {} {:?}", url, data);
112+
}
113+
});
114+
let url = uri.to_string();
115+
tokio::spawn(async move {
116+
while let Some(data) = server_to_client.next().await {
117+
println!("Server -> Client: {} {:?}", url, data);
118+
}
119+
});
120+
}
95121

96122
// You can modify response here
97123

src/default_client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl DefaultClient {
4444
}
4545

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

0 commit comments

Comments
 (0)