From eeb672fd9f6c53e9eaffdb92a5b1b9d77ab24ec0 Mon Sep 17 00:00:00 2001 From: Chrislearn Young Date: Tue, 12 Dec 2023 17:14:49 +0800 Subject: [PATCH] wip --- crates/core/src/conn/acme/client.rs | 8 ++------ crates/core/src/conn/acme/issuer.rs | 7 ++----- crates/core/src/conn/acme/jose.rs | 1 - crates/core/src/conn/acme/listener.rs | 6 +++--- crates/core/src/conn/rustls/config.rs | 6 +++--- crates/otel/Cargo.toml | 1 + crates/otel/src/tracing.rs | 11 ++++++++++- crates/proxy/src/clients.rs | 11 ++++------- crates/proxy/src/lib.rs | 1 - 9 files changed, 25 insertions(+), 27 deletions(-) diff --git a/crates/core/src/conn/acme/client.rs b/crates/core/src/conn/acme/client.rs index dec609ea3..cb6994821 100644 --- a/crates/core/src/conn/acme/client.rs +++ b/crates/core/src/conn/acme/client.rs @@ -1,13 +1,9 @@ -use std::{ - io::{Error as IoError, ErrorKind, Result as IoResult}, - sync::Arc, - time::Duration, -}; +use std::sync::Arc; use base64::engine::{general_purpose::URL_SAFE_NO_PAD, Engine}; use bytes::Bytes; use http_body_util::{BodyExt, Full}; -use hyper::{body::Incoming as HyperBody, Uri}; +use hyper::Uri; use hyper_tls::HttpsConnector; use hyper_util::client::legacy::{connect::HttpConnector, Client}; use hyper_util::rt::TokioExecutor; diff --git a/crates/core/src/conn/acme/issuer.rs b/crates/core/src/conn/acme/issuer.rs index 0437ef8ce..c413d26ef 100644 --- a/crates/core/src/conn/acme/issuer.rs +++ b/crates/core/src/conn/acme/issuer.rs @@ -1,13 +1,10 @@ -use std::io::{Error as IoError, ErrorKind, Result as IoResult}; +use std::io::Result as IoResult; use std::sync::Arc; use std::time::Duration; use rcgen::{Certificate, CertificateParams, CustomExtension, DistinguishedName, PKCS_ECDSA_P256_SHA256}; use rustls_pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer}; -use tokio_rustls::rustls::{ - crypto::ring::sign::{any_ecdsa_type, any_eddsa_type}, - sign::CertifiedKey, -}; +use tokio_rustls::rustls::{crypto::ring::sign::any_ecdsa_type, sign::CertifiedKey}; use super::cache::AcmeCache; use super::client::AcmeClient; diff --git a/crates/core/src/conn/acme/jose.rs b/crates/core/src/conn/acme/jose.rs index cbf47d3bf..17b510e0e 100644 --- a/crates/core/src/conn/acme/jose.rs +++ b/crates/core/src/conn/acme/jose.rs @@ -3,7 +3,6 @@ use std::io::{Error as IoError, ErrorKind, Result as IoResult}; use super::client::HyperClient; use base64::engine::general_purpose::URL_SAFE_NO_PAD; use base64::Engine; -use bytes::Bytes; use http_body_util::{BodyExt, Full}; use hyper::{body::Incoming as HyperBody, Method}; use ring::digest::{digest, Digest, SHA256}; diff --git a/crates/core/src/conn/acme/listener.rs b/crates/core/src/conn/acme/listener.rs index d9f06565e..6eb190c03 100644 --- a/crates/core/src/conn/acme/listener.rs +++ b/crates/core/src/conn/acme/listener.rs @@ -3,11 +3,11 @@ use std::path::PathBuf; use std::sync::{Arc, Weak}; use std::time::Duration; -use rustls_pki_types::{PrivateKeyDer, CertificateDer}; +use rustls_pki_types::{CertificateDer, PrivateKeyDer}; use tokio::io::{AsyncRead, AsyncWrite}; -use tokio_rustls::rustls::crypto::ring::sign::{any_ecdsa_type, any_supported_type}; +use tokio_rustls::rustls::crypto::ring::sign::any_ecdsa_type; use tokio_rustls::rustls::server::ServerConfig; -use tokio_rustls::rustls::sign::{CertifiedKey, SigningKey}; +use tokio_rustls::rustls::sign::CertifiedKey; use tokio_rustls::server::TlsStream; use tokio_rustls::TlsAcceptor; diff --git a/crates/core/src/conn/rustls/config.rs b/crates/core/src/conn/rustls/config.rs index 942da2d0e..31c236208 100644 --- a/crates/core/src/conn/rustls/config.rs +++ b/crates/core/src/conn/rustls/config.rs @@ -8,9 +8,9 @@ use std::sync::Arc; use futures_util::stream::{once, Once, Stream}; use rustls_pki_types::{CertificateDer, PrivateKeyDer}; -use tokio_rustls::rustls::crypto::ring::sign::{any_ecdsa_type, any_supported_type}; -use tokio_rustls::rustls::server::{ClientHello, NoClientAuth, ResolvesServerCert, WebPkiClientVerifier}; -use tokio_rustls::rustls::sign::{self, CertifiedKey}; +use tokio_rustls::rustls::crypto::ring::sign::any_supported_type; +use tokio_rustls::rustls::server::{ClientHello, ResolvesServerCert, WebPkiClientVerifier}; +use tokio_rustls::rustls::sign::CertifiedKey; pub use tokio_rustls::rustls::server::ServerConfig; diff --git a/crates/otel/Cargo.toml b/crates/otel/Cargo.toml index 345d29252..05f41cc05 100644 --- a/crates/otel/Cargo.toml +++ b/crates/otel/Cargo.toml @@ -25,6 +25,7 @@ opentelemetry-http = { workspace = true } opentelemetry-semantic-conventions = { workspace = true } opentelemetry = { workspace = true, features = ["metrics"] } salvo_core = { workspace = true, default-features = false } +headers03 = {version = "0.3", package = "headers"} [dev-dependencies] salvo_core = { workspace = true, features = ["test"] } diff --git a/crates/otel/src/tracing.rs b/crates/otel/src/tracing.rs index 4c5848af5..3280db807 100644 --- a/crates/otel/src/tracing.rs +++ b/crates/otel/src/tracing.rs @@ -4,6 +4,7 @@ use opentelemetry_http::HeaderExtractor; use opentelemetry_semantic_conventions::{resource, trace}; use salvo_core::http::headers::{self, HeaderMapExt}; use salvo_core::prelude::*; +use headers03::{HeaderMap, HeaderName, HeaderValue}; /// Middleware for tracing with OpenTelemetry. pub struct Tracing { @@ -26,8 +27,16 @@ where async fn handle(&self, req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) { let remote_addr = req.remote_addr().to_string(); + //TODO: Will remove after opentelemetry_http updated + let mut headers = HeaderMap::with_capacity(req.headers().len()); + headers.extend(req.headers().into_iter().map(|(name, value)| { + let name = HeaderName::from_bytes(name.as_ref()).unwrap(); + let value = HeaderValue::from_bytes(value.as_ref()).unwrap(); + (name, value) + })); + let parent_cx = - global::get_text_map_propagator(|propagator| propagator.extract(&HeaderExtractor(req.headers()))); + global::get_text_map_propagator(|propagator| propagator.extract(&HeaderExtractor(&headers))); let mut attributes = Vec::new(); attributes.push(resource::TELEMETRY_SDK_NAME.string(env!("CARGO_CRATE_NAME"))); diff --git a/crates/proxy/src/clients.rs b/crates/proxy/src/clients.rs index ea1e2230b..6dbac218d 100644 --- a/crates/proxy/src/clients.rs +++ b/crates/proxy/src/clients.rs @@ -1,17 +1,15 @@ -use futures_util::TryStreamExt; use hyper::upgrade::OnUpgrade; use hyper_tls::HttpsConnector; use hyper_util::client::legacy::{connect::HttpConnector, Client as HyperUtilClient}; use hyper_util::rt::TokioExecutor; -use salvo_core::http::header::{HeaderMap, HeaderName, HeaderValue, CONNECTION, HOST, UPGRADE}; -use salvo_core::http::uri::{Scheme, Uri}; -use salvo_core::http::{response, ReqBody, ResBody, StatusCode}; +use salvo_core::http::{ReqBody, ResBody, StatusCode}; use salvo_core::rt::tokio::TokioIo; -use salvo_core::{async_trait, BoxedError, Depot, Error, FlowCtrl, Handler, Request, Response}; +use salvo_core::{async_trait, Error}; use tokio::io::copy_bidirectional; use super::{HyperRequest, HyperResponse}; +/// A [`Client`] implementation based on [`hyper_util::client::legacy::Client`]. pub struct HyperClient { inner: HyperUtilClient, ReqBody>, } @@ -23,6 +21,7 @@ impl Default for HyperClient { } } impl HyperClient { + /// Create a new `HyperClient` with the given `HyperClient`. pub fn new(inner: HyperUtilClient, ReqBody>) -> Self { Self { inner } } @@ -45,12 +44,10 @@ impl super::Client for HyperClient { let response_upgrade_type = crate::get_upgrade_type(response.headers()); if request_upgrade_type.as_deref() == response_upgrade_type { let response_upgraded = hyper::upgrade::on(&mut response).await.unwrap(); - println!("--------------------1"); if let Some(request_upgraded) = request_upgraded { tokio::spawn(async move { match request_upgraded.await { Ok(request_upgraded) => { - println!("--------------------2"); let mut request_upgraded = TokioIo::new(request_upgraded); let mut response_upgraded = TokioIo::new(response_upgraded); if let Err(e) = copy_bidirectional(&mut response_upgraded, &mut request_upgraded).await diff --git a/crates/proxy/src/lib.rs b/crates/proxy/src/lib.rs index e590647c5..d438182ef 100644 --- a/crates/proxy/src/lib.rs +++ b/crates/proxy/src/lib.rs @@ -12,7 +12,6 @@ use std::convert::{Infallible, TryFrom}; -use futures_util::TryStreamExt; use hyper::upgrade::OnUpgrade; use percent_encoding::{utf8_percent_encode, CONTROLS}; use salvo_core::http::header::{HeaderMap, HeaderName, HeaderValue, CONNECTION, HOST, UPGRADE};