-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(channel): Make channel feature additive
- Loading branch information
Showing
20 changed files
with
254 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
3 changes: 2 additions & 1 deletion
3
tonic/src/transport/service/connection.rs → ...c/transport/channel/service/connection.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
tonic/src/transport/service/connector.rs → ...rc/transport/channel/service/connector.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::io::{self, IoSlice}; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
use hyper::client::connect::{Connected as HyperConnected, Connection}; | ||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; | ||
|
||
pub(super) trait Io: AsyncRead + AsyncWrite + Send + 'static {} | ||
|
||
impl<T> Io for T where T: AsyncRead + AsyncWrite + Send + 'static {} | ||
|
||
pub(crate) struct BoxedIo(Pin<Box<dyn Io>>); | ||
|
||
impl BoxedIo { | ||
pub(super) fn new<I: Io>(io: I) -> Self { | ||
BoxedIo(Box::pin(io)) | ||
} | ||
} | ||
|
||
impl Connection for BoxedIo { | ||
fn connected(&self) -> HyperConnected { | ||
HyperConnected::new() | ||
} | ||
} | ||
|
||
impl AsyncRead for BoxedIo { | ||
fn poll_read( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut ReadBuf<'_>, | ||
) -> Poll<io::Result<()>> { | ||
Pin::new(&mut self.0).poll_read(cx, buf) | ||
} | ||
} | ||
|
||
impl AsyncWrite for BoxedIo { | ||
fn poll_write( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<io::Result<usize>> { | ||
Pin::new(&mut self.0).poll_write(cx, buf) | ||
} | ||
|
||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
Pin::new(&mut self.0).poll_flush(cx) | ||
} | ||
|
||
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
Pin::new(&mut self.0).poll_shutdown(cx) | ||
} | ||
|
||
fn poll_write_vectored( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
bufs: &[IoSlice<'_>], | ||
) -> Poll<Result<usize, io::Error>> { | ||
Pin::new(&mut self.0).poll_write_vectored(cx, bufs) | ||
} | ||
|
||
fn is_write_vectored(&self) -> bool { | ||
self.0.is_write_vectored() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
mod add_origin; | ||
pub(crate) use self::add_origin::AddOrigin; | ||
|
||
mod connector; | ||
pub(crate) use self::connector::Connector; | ||
|
||
mod connection; | ||
pub(crate) use self::connection::Connection; | ||
|
||
mod discover; | ||
pub(crate) use self::discover::DynamicServiceStream; | ||
|
||
pub(crate) mod executor; | ||
pub(crate) use self::executor::{Executor, SharedExec}; | ||
|
||
pub(crate) mod io; | ||
|
||
mod reconnect; | ||
|
||
mod user_agent; | ||
pub(crate) use self::user_agent::UserAgent; | ||
|
||
#[cfg(feature = "tls")] | ||
mod tls; | ||
#[cfg(feature = "tls")] | ||
pub(crate) use self::tls::TlsConnector; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use std::fmt; | ||
use std::io::Cursor; | ||
use std::sync::Arc; | ||
|
||
use tokio::io::{AsyncRead, AsyncWrite}; | ||
use tokio_rustls::{ | ||
rustls::{pki_types::ServerName, ClientConfig, RootCertStore}, | ||
TlsConnector as RustlsConnector, | ||
}; | ||
|
||
use super::io::BoxedIo; | ||
use crate::transport::service::tls::{add_certs_from_pem, load_identity, ALPN_H2}; | ||
use crate::transport::tls::{Certificate, Identity}; | ||
|
||
#[derive(Debug)] | ||
enum TlsError { | ||
H2NotNegotiated, | ||
} | ||
|
||
impl fmt::Display for TlsError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
TlsError::H2NotNegotiated => write!(f, "HTTP/2 was not negotiated."), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for TlsError {} | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct TlsConnector { | ||
config: Arc<ClientConfig>, | ||
domain: Arc<ServerName<'static>>, | ||
assume_http2: bool, | ||
} | ||
|
||
impl TlsConnector { | ||
pub(crate) fn new( | ||
ca_cert: Option<Certificate>, | ||
identity: Option<Identity>, | ||
domain: &str, | ||
assume_http2: bool, | ||
) -> Result<Self, crate::Error> { | ||
let builder = ClientConfig::builder(); | ||
let mut roots = RootCertStore::empty(); | ||
|
||
#[cfg(feature = "tls-roots")] | ||
roots.add_parsable_certificates(rustls_native_certs::load_native_certs()?); | ||
|
||
#[cfg(feature = "tls-webpki-roots")] | ||
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); | ||
|
||
if let Some(cert) = ca_cert { | ||
add_certs_from_pem(&mut Cursor::new(cert), &mut roots)?; | ||
} | ||
|
||
let builder = builder.with_root_certificates(roots); | ||
let mut config = match identity { | ||
Some(identity) => { | ||
let (client_cert, client_key) = load_identity(identity)?; | ||
builder.with_client_auth_cert(client_cert, client_key)? | ||
} | ||
None => builder.with_no_client_auth(), | ||
}; | ||
|
||
config.alpn_protocols.push(ALPN_H2.into()); | ||
Ok(Self { | ||
config: Arc::new(config), | ||
domain: Arc::new(ServerName::try_from(domain)?.to_owned()), | ||
assume_http2, | ||
}) | ||
} | ||
|
||
pub(crate) async fn connect<I>(&self, io: I) -> Result<BoxedIo, crate::Error> | ||
where | ||
I: AsyncRead + AsyncWrite + Send + Unpin + 'static, | ||
{ | ||
let io = RustlsConnector::from(self.config.clone()) | ||
.connect(self.domain.as_ref().to_owned(), io) | ||
.await?; | ||
|
||
// Generally we require ALPN to be negotiated, but if the user has | ||
// explicitly set `assume_http2` to true, we'll allow it to be missing. | ||
let (_, session) = io.get_ref(); | ||
let alpn_protocol = session.alpn_protocol(); | ||
if alpn_protocol != Some(ALPN_H2) { | ||
if alpn_protocol.is_some() || !self.assume_http2 { | ||
return Err(TlsError::H2NotNegotiated.into()); | ||
} | ||
} | ||
|
||
Ok(BoxedIo::new(io)) | ||
} | ||
} | ||
|
||
impl fmt::Debug for TlsConnector { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("TlsConnector").finish() | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.