use bytes::Bytes; use clap::{Parser, Subcommand, ValueEnum}; use headers::Authorization; use http_body_util::Empty; use hyper::{Request, Uri}; use hyper1_proxy::{Intercept, Proxy, ProxyConnector}; use hyper_util::{ client::legacy::{connect::HttpConnector, Client}, rt::TokioExecutor, }; use std::error::Error; #[derive(Copy, Clone, PartialEq, Eq, Debug, ValueEnum)] pub enum SchemaOption { Http, Https, } #[derive(Parser, Clone, Debug)] struct CLI { #[arg(short, long)] pub proxy: Uri, #[arg(short, long)] pub schema: SchemaOption, pub url: Uri, #[command(subcommand)] pub auth: Option, } #[derive(Subcommand, Clone, Debug)] pub enum AuthorizationCLI { Basic { username: String, password: String }, } #[tokio::main] async fn main() -> Result<(), Box> { let CLI { proxy, url, auth, schema, } = CLI::parse(); let proxy = { let mut proxy = Proxy::new(Intercept::All, proxy); match &auth { Some(AuthorizationCLI::Basic { username, password }) => { proxy.set_authorization(Authorization::basic(username, password)) } None => {} } let connector = HttpConnector::new(); let proxy_connector = ProxyConnector::from_proxy(connector, proxy).unwrap(); proxy_connector }; let client = Client::builder(TokioExecutor::new()).build(proxy); match schema { SchemaOption::Http => { let mut req = Request::get(url.clone()) .body(Empty::::new()) .unwrap(); let result = client.request(req).await?; println!("{result:?}"); } SchemaOption::Https => { // Connecting to an https uri is straightforward (uses 'CONNECT' method underneath) let result = client.get(url).await; println!("{result:?}"); } } Ok(()) }