From bfb613aa1a8d0e6180bf94b08d612927fe07564f Mon Sep 17 00:00:00 2001 From: hatoo Date: Tue, 9 Apr 2024 00:45:49 +0900 Subject: [PATCH] possibly better clap exmaple --- README.md | 28 ++++++++++++++++++---------- examples/proxy.rs | 28 ++++++++++++++++++---------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 43c45d9..22162d5 100644 --- a/README.md +++ b/README.md @@ -13,16 +13,22 @@ A HTTP proxy server library intended to be a backend of application like Burp pr ```rust, no_run use std::path::PathBuf; -use clap::Parser; +use clap::{Args, Parser}; use futures::StreamExt; use http_mitm_proxy::MitmProxy; #[derive(Parser)] struct Opt { - #[clap(requires("private_key"))] - cert: Option, - #[clap(requires("cert"))] - private_key: Option, + #[clap(flatten)] + external_cert: Option, +} + +#[derive(Args, Debug)] +struct ExternalCert { + #[arg(required = false)] + cert: PathBuf, + #[arg(required = false)] + private_key: PathBuf, } fn make_root_cert() -> rcgen::CertifiedKey { @@ -49,13 +55,15 @@ fn make_root_cert() -> rcgen::CertifiedKey { async fn main() { let opt = Opt::parse(); - let root_cert = if let (Some(cert), Some(private_key)) = (opt.cert, opt.private_key) { + let root_cert = if let Some(external_cert) = opt.external_cert { // Use existing key - let param = - rcgen::CertificateParams::from_ca_cert_pem(&std::fs::read_to_string(cert).unwrap()) - .unwrap(); + let param = rcgen::CertificateParams::from_ca_cert_pem( + &std::fs::read_to_string(&external_cert.cert).unwrap(), + ) + .unwrap(); let key_pair = - rcgen::KeyPair::from_pem(&std::fs::read_to_string(private_key).unwrap()).unwrap(); + rcgen::KeyPair::from_pem(&std::fs::read_to_string(&external_cert.private_key).unwrap()) + .unwrap(); let cert = param.self_signed(&key_pair).unwrap(); diff --git a/examples/proxy.rs b/examples/proxy.rs index 2f184a9..7b76c63 100644 --- a/examples/proxy.rs +++ b/examples/proxy.rs @@ -1,15 +1,21 @@ use std::path::PathBuf; -use clap::Parser; +use clap::{Args, Parser}; use futures::StreamExt; use http_mitm_proxy::MitmProxy; #[derive(Parser)] struct Opt { - #[clap(requires("private_key"))] - cert: Option, - #[clap(requires("cert"))] - private_key: Option, + #[clap(flatten)] + external_cert: Option, +} + +#[derive(Args, Debug)] +struct ExternalCert { + #[arg(required = false)] + cert: PathBuf, + #[arg(required = false)] + private_key: PathBuf, } fn make_root_cert() -> rcgen::CertifiedKey { @@ -36,13 +42,15 @@ fn make_root_cert() -> rcgen::CertifiedKey { async fn main() { let opt = Opt::parse(); - let root_cert = if let (Some(cert), Some(private_key)) = (opt.cert, opt.private_key) { + let root_cert = if let Some(external_cert) = opt.external_cert { // Use existing key - let param = - rcgen::CertificateParams::from_ca_cert_pem(&std::fs::read_to_string(cert).unwrap()) - .unwrap(); + let param = rcgen::CertificateParams::from_ca_cert_pem( + &std::fs::read_to_string(&external_cert.cert).unwrap(), + ) + .unwrap(); let key_pair = - rcgen::KeyPair::from_pem(&std::fs::read_to_string(private_key).unwrap()).unwrap(); + rcgen::KeyPair::from_pem(&std::fs::read_to_string(&external_cert.private_key).unwrap()) + .unwrap(); let cert = param.self_signed(&key_pair).unwrap();