@@ -13,35 +13,50 @@ use http_mitm_proxy::{
1313#[ derive( Parser ) ]
1414struct Opt {
1515 #[ clap( flatten) ]
16- external_cert : Option < ExternalCert > ,
16+ external_issuer : Option < ExternalIssuer > ,
1717}
1818
1919#[ derive( Args , Debug ) ]
20- struct ExternalCert {
20+ struct ExternalIssuer {
2121 #[ arg( required = false ) ]
2222 cert : PathBuf ,
2323 #[ arg( required = false ) ]
2424 private_key : PathBuf ,
2525}
2626
27- fn make_root_cert ( ) -> rcgen:: CertifiedKey {
28- let mut param = rcgen:: CertificateParams :: default ( ) ;
27+ fn make_root_issuer ( ) -> rcgen:: Issuer < ' static , rcgen :: KeyPair > {
28+ let mut params = rcgen:: CertificateParams :: default ( ) ;
2929
30- param . distinguished_name = rcgen:: DistinguishedName :: new ( ) ;
31- param . distinguished_name . push (
30+ params . distinguished_name = rcgen:: DistinguishedName :: new ( ) ;
31+ params . distinguished_name . push (
3232 rcgen:: DnType :: CommonName ,
3333 rcgen:: DnValue :: Utf8String ( "<HTTP-MITM-PROXY CA>" . to_string ( ) ) ,
3434 ) ;
35- param . key_usages = vec ! [
35+ params . key_usages = vec ! [
3636 rcgen:: KeyUsagePurpose :: KeyCertSign ,
3737 rcgen:: KeyUsagePurpose :: CrlSign ,
3838 ] ;
39- param . is_ca = rcgen:: IsCa :: Ca ( rcgen:: BasicConstraints :: Unconstrained ) ;
39+ params . is_ca = rcgen:: IsCa :: Ca ( rcgen:: BasicConstraints :: Unconstrained ) ;
4040
41- let key_pair = rcgen:: KeyPair :: generate ( ) . unwrap ( ) ;
42- let cert = param. self_signed ( & key_pair) . unwrap ( ) ;
41+ let signing_key = rcgen:: KeyPair :: generate ( ) . unwrap ( ) ;
4342
44- rcgen:: CertifiedKey { cert, key_pair }
43+ let cert = params. self_signed ( & signing_key) . unwrap ( ) ;
44+
45+ println ! ( ) ;
46+ println ! ( "Trust this cert if you want to use HTTPS" ) ;
47+ println ! ( ) ;
48+ println ! ( "{}" , cert. pem( ) ) ;
49+ println ! ( ) ;
50+
51+ /*
52+ Save this cert to ca.crt and use it with curl like this:
53+ curl https://www.google.com -x http://127.0.0.1:3003 --cacert ca.crt
54+ */
55+
56+ println ! ( "Private key" ) ;
57+ println ! ( "{}" , signing_key. serialize_pem( ) ) ;
58+
59+ rcgen:: Issuer :: new ( params, signing_key)
4560}
4661
4762#[ tokio:: main]
@@ -56,29 +71,25 @@ async fn main() {
5671 . unwrap ( ) ;
5772 tokio:: spawn ( async { axum:: serve ( listener, app) . await } ) ;
5873
59- let root_cert = if let Some ( external_cert ) = opt. external_cert {
74+ let root_issuer = if let Some ( external_issuer ) = opt. external_issuer {
6075 // Use existing key
61- let param = rcgen:: CertificateParams :: from_ca_cert_pem (
62- & std:: fs:: read_to_string ( & external_cert . cert ) . unwrap ( ) ,
76+ let signing_key = rcgen:: KeyPair :: from_pem (
77+ & std:: fs:: read_to_string ( & external_issuer . private_key ) . unwrap ( ) ,
6378 )
6479 . unwrap ( ) ;
65- let key_pair =
66- rcgen:: KeyPair :: from_pem ( & std:: fs:: read_to_string ( & external_cert. private_key ) . unwrap ( ) )
67- . unwrap ( ) ;
68-
69- let cert = param. self_signed ( & key_pair) . unwrap ( ) ;
7080
71- rcgen:: CertifiedKey { cert, key_pair }
81+ rcgen:: Issuer :: from_ca_cert_pem (
82+ & std:: fs:: read_to_string ( & external_issuer. cert ) . unwrap ( ) ,
83+ signing_key,
84+ )
85+ . unwrap ( )
7286 } else {
73- make_root_cert ( )
87+ make_root_issuer ( )
7488 } ;
7589
76- let root_cert_pem = root_cert. cert . pem ( ) ;
77- let root_cert_key = root_cert. key_pair . serialize_pem ( ) ;
78-
7990 let proxy = MitmProxy :: new (
8091 // This is the root cert that will be used to sign the fake certificates
81- Some ( root_cert ) ,
92+ Some ( root_issuer ) ,
8293 Some ( Cache :: new ( 128 ) ) ,
8394 ) ;
8495
@@ -107,8 +118,7 @@ async fn main() {
107118 req. headers_mut ( ) . insert (
108119 hyper:: header:: HOST ,
109120 hyper:: header:: HeaderValue :: from_maybe_shared ( format ! (
110- "127.0.0.1:{}" ,
111- port
121+ "127.0.0.1:{port}"
112122 ) )
113123 . unwrap ( ) ,
114124 ) ;
@@ -117,8 +127,7 @@ async fn main() {
117127 parts. scheme = Some ( hyper:: http:: uri:: Scheme :: HTTP ) ;
118128 parts. authority = Some (
119129 hyper:: http:: uri:: Authority :: from_maybe_shared ( format ! (
120- "127.0.0.1:{}" ,
121- port
130+ "127.0.0.1:{port}"
122131 ) )
123132 . unwrap ( ) ,
124133 ) ;
@@ -136,19 +145,5 @@ async fn main() {
136145
137146 println ! ( "HTTP Proxy is listening on http://127.0.0.1:3003" ) ;
138147
139- println ! ( ) ;
140- println ! ( "Trust this cert if you want to use HTTPS" ) ;
141- println ! ( ) ;
142- println ! ( "{}" , root_cert_pem) ;
143- println ! ( ) ;
144-
145- /*
146- Save this cert to ca.crt and use it with curl like this:
147- curl https://www.google.com -x http://127.0.0.1:3003 --cacert ca.crt
148- */
149-
150- println ! ( "Private key" ) ;
151- println ! ( "{}" , root_cert_key) ;
152-
153148 proxy. await ;
154149}
0 commit comments