Skip to content

Commit e58a94b

Browse files
authored
Merge pull request #23 from MMukundi/feature/type-consistency
Implement Request/Response Type Consistency
2 parents 3451058 + a3d40ee commit e58a94b

File tree

6 files changed

+120
-214
lines changed

6 files changed

+120
-214
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/target
2-
.DS_Store
2+
.DS_Store

mitm_proxy/src/mitm_proxy.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ pub struct MitmProxy {
9393
requests: Vec<RequestInfo>,
9494
config: MitmProxyConfig,
9595
state: MitmProxyState,
96-
rx: Receiver<Output>,
96+
rx: Receiver<ProxyHandler>,
9797
}
9898

9999
impl MitmProxy {
100-
pub fn new(cc: &eframe::CreationContext<'_>, rx: Receiver<Output>) -> Self {
100+
pub fn new(cc: &eframe::CreationContext<'_>, rx: Receiver<ProxyHandler>) -> Self {
101101
Self::configure_fonts(cc);
102102
let config: MitmProxyConfig = confy::load("MitmProxy", None).unwrap_or_default();
103103
let state = MitmProxyState::new();
@@ -257,7 +257,10 @@ impl MitmProxy {
257257

258258
pub fn update_requests(&mut self) -> Option<RequestInfo> {
259259
match self.rx.try_recv() {
260-
Ok(l) => Some(RequestInfo::from(l)),
260+
Ok(l) => {
261+
let (request,response) = l.to_parts();
262+
Some(RequestInfo::new(request,response))
263+
},
261264
_ => None,
262265
}
263266
}

mitm_proxy/src/requests.rs

Lines changed: 32 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,7 @@
1-
use std::collections::HashMap;
2-
31
use eframe::egui::{self};
42
use egui_extras::TableRow;
53
use proxyapi::{*, hyper::Method};
64

7-
struct Request {
8-
http_method: Method,
9-
method: String,
10-
uri: String,
11-
version: String,
12-
headers: HashMap<String, String>,
13-
body: String,
14-
time: i64,
15-
}
16-
17-
impl Request {
18-
fn new(
19-
http_method: Method,
20-
method: String,
21-
uri: String,
22-
version: String,
23-
headers: HashMap<String, String>,
24-
body: String,
25-
time: i64,
26-
) -> Self {
27-
Self {
28-
http_method,
29-
method,
30-
uri,
31-
version,
32-
headers,
33-
body,
34-
time,
35-
}
36-
}
37-
}
38-
39-
pub struct Response {
40-
status: String,
41-
version: String,
42-
headers: HashMap<String, String>,
43-
body: String,
44-
time: i64,
45-
}
46-
47-
impl Response {
48-
fn new(
49-
status: String,
50-
version: String,
51-
headers: HashMap<String, String>,
52-
body: String,
53-
time: i64,
54-
) -> Self {
55-
Self {
56-
status,
57-
version,
58-
headers,
59-
body,
60-
time,
61-
}
62-
}
63-
}
64-
655
pub struct Details;
666

677
#[derive(PartialEq)]
@@ -77,116 +17,73 @@ impl Default for InfoOptions {
7717
}
7818
}
7919
pub struct RequestInfo {
80-
request: Option<Request>,
81-
response: Option<Response>,
20+
request: Option<ProxiedRequest>,
21+
response: Option<ProxiedResponse>,
8222
details: Option<Details>,
8323
}
84-
85-
impl Default for RequestInfo {
86-
fn default() -> Self {
87-
RequestInfo {
88-
request: None,
89-
response: None,
90-
details: None,
91-
}
92-
}
93-
}
94-
95-
impl From<Output> for RequestInfo {
96-
fn from(value: Output) -> Self {
97-
let request = match value.req() {
98-
Some(r) => Some(Request::new(
99-
r.http_method().clone(),
100-
r.method().to_string(),
101-
r.uri().to_string(),
102-
r.version().to_string(),
103-
r.headers()
104-
.into_iter()
105-
.map(|h| (h.0.to_string(), h.1.to_string()))
106-
.collect(),
107-
r.body().to_string(),
108-
r.time(),
109-
)),
110-
None => None,
111-
};
112-
113-
let response = match value.res() {
114-
Some(r) => Some(Response::new(
115-
r.status().to_string(),
116-
r.version().to_string(),
117-
r.headers()
118-
.into_iter()
119-
.map(|h| (h.0.to_string(), h.1.to_string()))
120-
.collect(),
121-
r.body().to_string(),
122-
r.time(),
123-
)),
124-
None => None,
125-
};
126-
127-
let details = None;
128-
129-
RequestInfo {
24+
impl RequestInfo {
25+
pub fn new(request: Option<ProxiedRequest>, response: Option<ProxiedResponse>)->Self{
26+
Self {
13027
request,
13128
response,
132-
details,
29+
details:None
13330
}
13431
}
13532
}
136-
13733
impl RequestInfo {
138-
pub fn show_request(&mut self, ui: &mut egui::Ui) {
34+
pub fn show_request(&self, ui: &mut egui::Ui) {
13935
if let Some(r) = &self.request {
14036
ui.strong("Method");
141-
ui.label(&r.method);
37+
ui.label(r.method().to_string());
14238

14339
ui.strong("Version");
144-
ui.label(&r.version);
40+
ui.label(format!("{:?}",r.version()));
14541

14642
ui.strong("Headers");
147-
for (k, v) in r.headers.iter() {
148-
ui.label(format!("{}: {}", &k, &v));
43+
for (k, v) in r.headers().iter() {
44+
if let Ok(value_str) = v.to_str(){
45+
ui.label(format!("{}: {}", &k, &value_str));
46+
}
14947
}
15048

151-
ui.strong("body");
152-
ui.label(&r.body);
49+
ui.strong("Body");
50+
ui.label(format!("{:?}",r.body().as_ref()));
15351

15452
ui.strong("Time");
155-
ui.label(&r.time.to_string());
53+
ui.label(&r.time().to_string());
15654
} else {
15755
ui.label("No requests");
15856
}
15957
}
16058

161-
pub fn show_response(&mut self, ui: &mut egui::Ui) {
59+
pub fn show_response(&self, ui: &mut egui::Ui) {
16260
if let Some(r) = &self.response {
16361
ui.strong("Status");
164-
ui.label(&r.status);
62+
ui.label(&r.status().to_string());
16563

16664
ui.strong("Version");
167-
ui.label(&r.version);
168-
169-
ui.strong("Status");
170-
ui.label(&r.status);
65+
ui.label(format!("{:?}",r.version()));
17166

17267
ui.strong("Headers");
173-
for (k, v) in r.headers.iter() {
174-
ui.label(format!("{}: {}", &k, &v));
68+
for (k, v) in r.headers().iter() {
69+
if let Ok(value_str) = v.to_str(){
70+
ui.label(format!("{}: {}", &k, &value_str));
71+
}
17572
}
17673

177-
ui.strong("body");
178-
ui.label(&r.body);
74+
ui.strong("Body");
75+
ui.label(format!("{:?}",r.body().as_ref()));
17976

18077
ui.strong("Time");
181-
ui.label(&r.time.to_string());
78+
ui.label(&r.time().to_string());
18279
} else {
18380
ui.label("No Response");
18481
}
18582
}
18683

18784
pub fn should_show(&self, method:&Method)->bool {
18885
if let Some(req) = &self.request {
189-
req.http_method == method
86+
req.method() == method
19087
}else{
19188
false
19289
}
@@ -202,21 +99,21 @@ impl RequestInfo {
20299
pub fn render_row(&self, row: &mut TableRow) {
203100
let req = self.request.as_ref().unwrap();
204101
let res = self.response.as_ref().unwrap();
205-
let time = (res.time as f64 - req.time as f64) * 10_f64.powf(-9.0) as f64;
102+
let time = (res.time() as f64 - req.time() as f64) * 10_f64.powf(-9.0) as f64;
206103
row.col(|ui| {
207-
ui.label(&req.uri);
104+
ui.label(req.uri().to_string());
208105
});
209106

210107
row.col(|ui| {
211-
ui.label(&req.method);
108+
ui.label(req.method().to_string());
212109
});
213110

214111
row.col(|ui| {
215-
ui.label(&res.status);
112+
ui.label(res.status().to_string());
216113
});
217114

218115
row.col(|ui| {
219-
ui.label(format!("{} bytes", &res.body.len()));
116+
ui.label(format!("{} bytes", res.body().len()));
220117
});
221118

222119
row.col(|ui| {

proxyapi/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod error;
22
mod rewind;
33
pub mod proxy;
4-
mod output;
4+
mod proxy_handler;
55

66
pub mod ca;
77

@@ -20,7 +20,7 @@ pub use tokio_tungstenite;
2020
// pub use error;
2121
// pub use noop;
2222
pub use proxy::*;
23-
pub use output::*;
23+
pub use proxy_handler::*;
2424

2525
#[derive(Debug)]
2626
pub enum RequestResponse{

proxyapi/src/proxy/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{future::Future, net::{SocketAddr}, sync::{Arc, mpsc::SyncSender}, conv
55

66
use internal::InternalProxy;
77

8-
use crate::{ca::{Ssl}, error::Error, output};
8+
use crate::{ca::{Ssl}, error::Error, proxy_handler};
99

1010
//use builder::{AddrListenerServer, WantsAddr};
1111

@@ -17,12 +17,11 @@ use hyper_rustls::{ HttpsConnectorBuilder};
1717

1818
pub struct Proxy{
1919
addr: SocketAddr,
20-
tx: Option<SyncSender<output::Output>>
20+
tx: Option<SyncSender<proxy_handler::ProxyHandler>>
2121
}
2222

2323
impl Proxy {
24-
25-
pub fn new(addr: SocketAddr, tx: Option<SyncSender<output::Output>>) -> Self{
24+
pub fn new(addr: SocketAddr, tx: Option<SyncSender<proxy_handler::ProxyHandler>>) -> Self{
2625
Self {
2726
addr,
2827
tx
@@ -31,7 +30,7 @@ impl Proxy {
3130

3231
pub async fn start<F: Future<Output = ()>>(self, signal: F) -> Result<(), Error>{
3332

34-
let addr = self.addr.clone();
33+
let addr = self.addr;
3534

3635
let https = HttpsConnectorBuilder::new()
3736
.with_webpki_roots()
@@ -53,7 +52,7 @@ impl Proxy {
5352
let make_service = make_service_fn(move |conn: &AddrStream| {
5453
let client = client.clone();
5554
let ca = Arc::clone(&ssl);
56-
let http_handler = output::Output::new(self.tx.clone().unwrap());
55+
let http_handler = proxy_handler::ProxyHandler::new(self.tx.clone().unwrap());
5756
let websocket_connector = None;
5857
let remote_addr = conn.remote_addr();
5958
async move {

0 commit comments

Comments
 (0)