diff --git a/README.md b/README.md index f457af4..d6c2aa0 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,6 @@ A HTTP proxy server library intended to be a backend of application like Burp pr ## Usage ```rust, no_run -use std::sync::Arc; - use futures::StreamExt; use http_mitm_proxy::MitmProxy; @@ -34,11 +32,12 @@ fn make_root_cert() -> rcgen::Certificate { #[tokio::main] async fn main() { - let root_cert = Arc::new(make_root_cert()); + let root_cert = make_root_cert(); + let root_cert_pem = root_cert.serialize_pem().unwrap(); let proxy = MitmProxy::new( // This is the root cert that will be used to sign the fake certificates - Some(root_cert.clone()), + Some(root_cert), // This is the connector that will be used to connect to the upstream server from proxy tokio_native_tls::native_tls::TlsConnector::new().unwrap(), ); @@ -51,7 +50,7 @@ async fn main() { println!(); println!("Trust this cert if you want to use HTTPS"); println!(); - println!("{}", root_cert.serialize_pem().unwrap()); + println!("{}", root_cert_pem); println!(); /* diff --git a/examples/proxy.rs b/examples/proxy.rs index ffac2e5..dfad4a7 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use futures::StreamExt; use http_mitm_proxy::MitmProxy; @@ -21,11 +19,12 @@ fn make_root_cert() -> rcgen::Certificate { #[tokio::main] async fn main() { - let root_cert = Arc::new(make_root_cert()); + let root_cert = make_root_cert(); + let root_cert_pem = root_cert.serialize_pem().unwrap(); let proxy = MitmProxy::new( // This is the root cert that will be used to sign the fake certificates - Some(root_cert.clone()), + Some(root_cert), // This is the connector that will be used to connect to the upstream server from proxy tokio_native_tls::native_tls::TlsConnector::new().unwrap(), ); @@ -38,7 +37,7 @@ async fn main() { println!(); println!("Trust this cert if you want to use HTTPS"); println!(); - println!("{}", root_cert.serialize_pem().unwrap()); + println!("{}", root_cert_pem); println!(); /* diff --git a/src/lib.rs b/src/lib.rs index ae28aa7..35c247d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ use hyper::{ Method, Request, Response, StatusCode, Uri, }; use hyper_util::rt::TokioIo; -use std::{future::Future, ops::Deref, sync::Arc}; +use std::{borrow::Borrow, future::Future, sync::Arc}; use tls::server_config; use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, @@ -83,7 +83,7 @@ pub struct Communication { } /// C is typically Arc or &'static Certificate -impl + Send + Sync + 'static> MitmProxy { +impl + Send + Sync + 'static> MitmProxy { /// Bind proxy server to address. /// You can observe communications between client and server by receiving stream. /// To run proxy server, you need to run returned future. This API design give you an ability to cancel proxy server when you want. @@ -183,7 +183,7 @@ impl + Send + Sync + 'static> MitmProxy }; if let Some(root_cert) = proxy.root_cert.as_ref() { - let Ok(server_config) = server_config(host.to_string(), root_cert.deref()) + let Ok(server_config) = server_config(host.to_string(), root_cert.borrow()) else { tracing::error!("Failed to create server config for {}", host); return; diff --git a/tests/test.rs b/tests/test.rs index a790916..f37bb8b 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -150,14 +150,11 @@ where tokio::spawn(server); - let root_cert = Arc::new(root_cert()); + let root_cert = root_cert(); + let root_cert_der = root_cert.serialize_der().unwrap(); let proxy = http_mitm_proxy::MitmProxy::new( - if without_cert { - None - } else { - Some(root_cert.clone()) - }, + if without_cert { None } else { Some(root_cert) }, tokio_native_tls::native_tls::TlsConnector::builder() .danger_accept_invalid_certs(true) .danger_accept_invalid_hostnames(true) @@ -176,9 +173,7 @@ where let client = if !without_cert { client_builder - .add_root_certificate( - reqwest::Certificate::from_der(&root_cert.serialize_der().unwrap()).unwrap(), - ) + .add_root_certificate(reqwest::Certificate::from_der(&root_cert_der).unwrap()) .build() .unwrap() } else {