2019-11-20 18:35:07 +00:00
|
|
|
#![cfg(feature = "rustls")]
|
2021-01-17 05:19:32 +00:00
|
|
|
|
2021-02-07 03:54:58 +00:00
|
|
|
extern crate tls_rustls as rustls;
|
2021-01-17 05:19:32 +00:00
|
|
|
|
|
|
|
use std::{
|
|
|
|
io::BufReader,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
2021-10-20 01:00:11 +00:00
|
|
|
time::SystemTime,
|
2021-01-17 05:19:32 +00:00
|
|
|
};
|
2019-07-31 20:02:56 +00:00
|
|
|
|
|
|
|
use actix_http::HttpService;
|
2019-12-12 17:08:38 +00:00
|
|
|
use actix_http_test::test_server;
|
2021-04-16 19:48:37 +00:00
|
|
|
use actix_service::{fn_service, map_config, ServiceFactoryExt};
|
2021-11-30 14:12:04 +00:00
|
|
|
use actix_tls::connect::rustls::webpki_roots_cert_store;
|
2021-04-01 14:26:13 +00:00
|
|
|
use actix_utils::future::ok;
|
2021-01-17 05:19:32 +00:00
|
|
|
use actix_web::{dev::AppConfig, http::Version, web, App, HttpResponse};
|
2021-10-20 01:00:11 +00:00
|
|
|
use rustls::{
|
|
|
|
client::{ServerCertVerified, ServerCertVerifier},
|
2021-11-15 04:03:33 +00:00
|
|
|
Certificate, ClientConfig, PrivateKey, ServerConfig, ServerName,
|
2021-10-20 01:00:11 +00:00
|
|
|
};
|
|
|
|
use rustls_pemfile::{certs, pkcs8_private_keys};
|
2021-01-17 05:19:32 +00:00
|
|
|
|
|
|
|
fn tls_config() -> ServerConfig {
|
|
|
|
let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_owned()]).unwrap();
|
|
|
|
let cert_file = cert.serialize_pem().unwrap();
|
|
|
|
let key_file = cert.serialize_private_key_pem();
|
|
|
|
|
|
|
|
let cert_file = &mut BufReader::new(cert_file.as_bytes());
|
|
|
|
let key_file = &mut BufReader::new(key_file.as_bytes());
|
|
|
|
|
2021-10-20 01:00:11 +00:00
|
|
|
let cert_chain = certs(cert_file)
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(Certificate)
|
|
|
|
.collect();
|
2021-01-17 05:19:32 +00:00
|
|
|
let mut keys = pkcs8_private_keys(key_file).unwrap();
|
|
|
|
|
2021-10-20 01:00:11 +00:00
|
|
|
ServerConfig::builder()
|
|
|
|
.with_safe_defaults()
|
|
|
|
.with_no_client_auth()
|
|
|
|
.with_single_cert(cert_chain, PrivateKey(keys.remove(0)))
|
|
|
|
.unwrap()
|
2019-07-31 20:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mod danger {
|
2021-10-20 01:00:11 +00:00
|
|
|
use super::*;
|
|
|
|
|
2021-01-17 05:19:32 +00:00
|
|
|
pub struct NoCertificateVerification;
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2021-10-20 01:00:11 +00:00
|
|
|
impl ServerCertVerifier for NoCertificateVerification {
|
2019-07-31 20:02:56 +00:00
|
|
|
fn verify_server_cert(
|
|
|
|
&self,
|
2021-10-20 01:00:11 +00:00
|
|
|
_end_entity: &Certificate,
|
|
|
|
_intermediates: &[Certificate],
|
|
|
|
_server_name: &ServerName,
|
|
|
|
_scts: &mut dyn Iterator<Item = &[u8]>,
|
|
|
|
_ocsp_response: &[u8],
|
|
|
|
_now: SystemTime,
|
|
|
|
) -> Result<ServerCertVerified, rustls::Error> {
|
|
|
|
Ok(ServerCertVerified::assertion())
|
2019-07-31 20:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 05:19:32 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_connection_reuse_h2() {
|
2019-11-26 05:25:50 +00:00
|
|
|
let num = Arc::new(AtomicUsize::new(0));
|
|
|
|
let num2 = num.clone();
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2019-12-12 17:08:38 +00:00
|
|
|
let srv = test_server(move || {
|
2019-11-26 05:25:50 +00:00
|
|
|
let num2 = num2.clone();
|
2021-04-16 19:48:37 +00:00
|
|
|
fn_service(move |io| {
|
2019-11-26 05:25:50 +00:00
|
|
|
num2.fetch_add(1, Ordering::Relaxed);
|
|
|
|
ok(io)
|
|
|
|
})
|
|
|
|
.and_then(
|
|
|
|
HttpService::build()
|
2019-12-20 11:36:48 +00:00
|
|
|
.h2(map_config(
|
2021-02-11 23:03:17 +00:00
|
|
|
App::new().service(web::resource("/").route(web::to(HttpResponse::Ok))),
|
2019-12-20 11:36:48 +00:00
|
|
|
|_| AppConfig::default(),
|
2019-12-20 11:27:32 +00:00
|
|
|
))
|
2021-01-17 05:19:32 +00:00
|
|
|
.rustls(tls_config())
|
2019-11-26 05:25:50 +00:00
|
|
|
.map_err(|_| ()),
|
|
|
|
)
|
2020-03-08 07:42:45 +00:00
|
|
|
})
|
|
|
|
.await;
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2021-10-20 01:00:11 +00:00
|
|
|
let mut config = ClientConfig::builder()
|
|
|
|
.with_safe_defaults()
|
2021-10-20 21:32:05 +00:00
|
|
|
.with_root_certificates(webpki_roots_cert_store())
|
2021-10-20 01:00:11 +00:00
|
|
|
.with_no_client_auth();
|
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
let protos = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
|
2021-10-20 01:00:11 +00:00
|
|
|
config.alpn_protocols = protos;
|
|
|
|
|
|
|
|
// disable TLS verification
|
2019-11-26 05:25:50 +00:00
|
|
|
config
|
|
|
|
.dangerous()
|
2021-01-17 05:19:32 +00:00
|
|
|
.set_certificate_verifier(Arc::new(danger::NoCertificateVerification));
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2020-09-11 08:24:39 +00:00
|
|
|
let client = awc::Client::builder()
|
2021-02-18 12:30:09 +00:00
|
|
|
.connector(awc::Connector::new().rustls(Arc::new(config)))
|
2019-11-26 05:25:50 +00:00
|
|
|
.finish();
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// req 1
|
|
|
|
let request = client.get(srv.surl("/")).send();
|
|
|
|
let response = request.await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// req 2
|
|
|
|
let req = client.post(srv.surl("/"));
|
|
|
|
let response = req.send().await.unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert_eq!(response.version(), Version::HTTP_2);
|
2019-07-31 20:02:56 +00:00
|
|
|
|
2019-11-26 05:25:50 +00:00
|
|
|
// one connection
|
|
|
|
assert_eq!(num.load(Ordering::Relaxed), 1);
|
2019-07-31 20:02:56 +00:00
|
|
|
}
|