Update low-level-rustls example (#3013)

Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
This commit is contained in:
Hayashi Mikihiro 2024-11-12 01:55:43 +09:00 committed by GitHub
parent 269565ff93
commit 6e0559e687
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 13 deletions

View file

@ -9,9 +9,8 @@ axum = { path = "../../axum" }
futures-util = { version = "0.3", default-features = false } futures-util = { version = "0.3", default-features = false }
hyper = { version = "1.0.0", features = ["full"] } hyper = { version = "1.0.0", features = ["full"] }
hyper-util = { version = "0.1" } hyper-util = { version = "0.1" }
rustls-pemfile = "1.0.4"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
tokio-rustls = "0.24.1" tokio-rustls = "0.26"
tower-service = "0.3.2" tower-service = "0.3.2"
tracing = "0.1" tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter"] }

View file

@ -8,16 +8,14 @@ use axum::{extract::Request, routing::get, Router};
use futures_util::pin_mut; use futures_util::pin_mut;
use hyper::body::Incoming; use hyper::body::Incoming;
use hyper_util::rt::{TokioExecutor, TokioIo}; use hyper_util::rt::{TokioExecutor, TokioIo};
use rustls_pemfile::{certs, pkcs8_private_keys};
use std::{ use std::{
fs::File,
io::BufReader,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::Arc,
}; };
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio_rustls::{ use tokio_rustls::{
rustls::{Certificate, PrivateKey, ServerConfig}, rustls::pki_types::{pem::PemObject, CertificateDer, PrivateKeyDer},
rustls::ServerConfig,
TlsAcceptor, TlsAcceptor,
}; };
use tower_service::Service; use tower_service::Service;
@ -95,18 +93,14 @@ async fn handler() -> &'static str {
} }
fn rustls_server_config(key: impl AsRef<Path>, cert: impl AsRef<Path>) -> Arc<ServerConfig> { fn rustls_server_config(key: impl AsRef<Path>, cert: impl AsRef<Path>) -> Arc<ServerConfig> {
let mut key_reader = BufReader::new(File::open(key).unwrap()); let key = PrivateKeyDer::from_pem_file(key).unwrap();
let mut cert_reader = BufReader::new(File::open(cert).unwrap());
let key = PrivateKey(pkcs8_private_keys(&mut key_reader).unwrap().remove(0)); let certs = CertificateDer::pem_file_iter(cert)
let certs = certs(&mut cert_reader)
.unwrap() .unwrap()
.into_iter() .map(|cert| cert.unwrap())
.map(Certificate)
.collect(); .collect();
let mut config = ServerConfig::builder() let mut config = ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth() .with_no_client_auth()
.with_single_cert(certs, key) .with_single_cert(certs, key)
.expect("bad certificate/key"); .expect("bad certificate/key");