-
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(transport): Make transport server and channel independent
- Loading branch information
Showing
15 changed files
with
136 additions
and
110 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pub(crate) mod io; | ||
pub(crate) use self::io::ServerIo; | ||
|
||
mod router; | ||
pub use self::router::{Routes, RoutesBuilder}; | ||
|
||
#[cfg(feature = "tls")] | ||
pub(crate) mod tls; |
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,65 @@ | ||
use std::io::Cursor; | ||
use std::{fmt, sync::Arc}; | ||
|
||
use tokio::io::{AsyncRead, AsyncWrite}; | ||
use tokio_rustls::{ | ||
rustls::{server::WebPkiClientVerifier, RootCertStore, ServerConfig}, | ||
TlsAcceptor as RustlsAcceptor, | ||
}; | ||
|
||
use crate::transport::server::Connected; | ||
use crate::transport::server::TlsStream; | ||
use crate::transport::service::tls::{add_certs_from_pem, load_identity, ALPN_H2}; | ||
use crate::transport::tls::{Certificate, Identity}; | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct TlsAcceptor { | ||
inner: Arc<ServerConfig>, | ||
} | ||
|
||
impl TlsAcceptor { | ||
pub(crate) fn new( | ||
identity: Identity, | ||
client_ca_root: Option<Certificate>, | ||
client_auth_optional: bool, | ||
) -> Result<Self, crate::Error> { | ||
let builder = ServerConfig::builder(); | ||
|
||
let builder = match client_ca_root { | ||
None => builder.with_no_client_auth(), | ||
Some(cert) => { | ||
let mut roots = RootCertStore::empty(); | ||
add_certs_from_pem(&mut Cursor::new(cert), &mut roots)?; | ||
let verifier = if client_auth_optional { | ||
WebPkiClientVerifier::builder(roots.into()).allow_unauthenticated() | ||
} else { | ||
WebPkiClientVerifier::builder(roots.into()) | ||
} | ||
.build()?; | ||
builder.with_client_cert_verifier(verifier) | ||
} | ||
}; | ||
|
||
let (cert, key) = load_identity(identity)?; | ||
let mut config = builder.with_single_cert(cert, key)?; | ||
|
||
config.alpn_protocols.push(ALPN_H2.into()); | ||
Ok(Self { | ||
inner: Arc::new(config), | ||
}) | ||
} | ||
|
||
pub(crate) async fn accept<IO>(&self, io: IO) -> Result<TlsStream<IO>, crate::Error> | ||
where | ||
IO: AsyncRead + AsyncWrite + Connected + Unpin + Send + 'static, | ||
{ | ||
let acceptor = RustlsAcceptor::from(self.inner.clone()); | ||
acceptor.accept(io).await.map_err(Into::into) | ||
} | ||
} | ||
|
||
impl fmt::Debug for TlsAcceptor { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("TlsAcceptor").finish() | ||
} | ||
} |
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.