use axum::{Router, routing::get};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
println!("⚠️ Dangerous HTTPS Client Example");
println!("==================================");
println!();
println!("This example shows how to create HTTPS clients that accept invalid certificates.");
println!("This should ONLY be used for development/testing with self-signed certificates!");
println!();
#[cfg(all(feature = "tls", not(feature = "native-tls")))]
{
use axum_reverse_proxy::create_dangerous_rustls_config;
use bytes::Bytes;
use http_body_util::Empty;
use hyper_rustls::HttpsConnectorBuilder;
use hyper_util::client::legacy::Client;
use hyper_util::rt::TokioExecutor;
println!("Creating dangerous client with rustls...");
let tls_config = create_dangerous_rustls_config();
let https = HttpsConnectorBuilder::new()
.with_tls_config(tls_config)
.https_or_http()
.enable_http1()
.build();
let _client: Client<_, Empty<Bytes>> = Client::builder(TokioExecutor::new()).build(https);
println!("✓ Created hyper client that accepts invalid certificates");
}
#[cfg(feature = "native-tls")]
{
use axum_reverse_proxy::create_dangerous_native_tls_connector;
use bytes::Bytes;
use http_body_util::Empty;
use hyper_tls::HttpsConnector;
use hyper_util::client::legacy::{Client, connect::HttpConnector};
use hyper_util::rt::TokioExecutor;
println!("Creating dangerous client with native-tls...");
let tls = create_dangerous_native_tls_connector().expect("Failed to create TLS connector");
let mut http = HttpConnector::new();
http.enforce_http(false);
let tls = tokio_native_tls::TlsConnector::from(tls);
let https = HttpsConnector::from((http, tls));
let _client: Client<_, Empty<Bytes>> = Client::builder(TokioExecutor::new()).build(https);
println!("✓ Created hyper client that accepts invalid certificates");
}
println!();
println!("You can now use these clients to connect to servers with:");
println!("- Self-signed certificates");
println!("- Expired certificates");
println!("- Wrong hostname certificates");
println!("- Untrusted CA certificates");
println!();
async fn root() -> &'static str {
"Dangerous client example server"
}
let app = Router::new().route("/", get(root));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("Example server listening on {addr}");
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}