-
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.
chore(transport): Clean up server and channel (#174)
* chore(transport): Clean up server and channel * Fix tls feature compilation
- Loading branch information
1 parent
0847b67
commit 1626c2e
Showing
6 changed files
with
244 additions
and
289 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
13 changes: 9 additions & 4 deletions
13
tonic/src/transport/channel.rs → tonic/src/transport/channel/mod.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
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,93 @@ | ||
use crate::transport::{ | ||
service::TlsConnector, | ||
tls::{Certificate, Identity}, | ||
}; | ||
use http::Uri; | ||
use std::fmt; | ||
|
||
/// Configures TLS settings for endpoints. | ||
#[cfg(feature = "tls")] | ||
#[derive(Clone)] | ||
pub struct ClientTlsConfig { | ||
domain: Option<String>, | ||
cert: Option<Certificate>, | ||
identity: Option<Identity>, | ||
rustls_raw: Option<tokio_rustls::rustls::ClientConfig>, | ||
} | ||
|
||
#[cfg(feature = "tls")] | ||
impl fmt::Debug for ClientTlsConfig { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("ClientTlsConfig") | ||
.field("domain", &self.domain) | ||
.field("cert", &self.cert) | ||
.field("identity", &self.identity) | ||
.finish() | ||
} | ||
} | ||
|
||
#[cfg(feature = "tls")] | ||
impl ClientTlsConfig { | ||
/// Creates a new `ClientTlsConfig` using Rustls. | ||
pub fn with_rustls() -> Self { | ||
ClientTlsConfig { | ||
domain: None, | ||
cert: None, | ||
identity: None, | ||
rustls_raw: None, | ||
} | ||
} | ||
|
||
/// Sets the domain name against which to verify the server's TLS certificate. | ||
/// | ||
/// This has no effect if `rustls_client_config` is used to configure Rustls. | ||
pub fn domain_name(self, domain_name: impl Into<String>) -> Self { | ||
ClientTlsConfig { | ||
domain: Some(domain_name.into()), | ||
..self | ||
} | ||
} | ||
|
||
/// Sets the CA Certificate against which to verify the server's TLS certificate. | ||
/// | ||
/// This has no effect if `rustls_client_config` is used to configure Rustls. | ||
pub fn ca_certificate(self, ca_certificate: Certificate) -> Self { | ||
ClientTlsConfig { | ||
cert: Some(ca_certificate), | ||
..self | ||
} | ||
} | ||
|
||
/// Sets the client identity to present to the server. | ||
/// | ||
/// This has no effect if `rustls_client_config` is used to configure Rustls. | ||
pub fn identity(self, identity: Identity) -> Self { | ||
ClientTlsConfig { | ||
identity: Some(identity), | ||
..self | ||
} | ||
} | ||
|
||
/// Use options specified by the given `ClientConfig` to configure TLS. | ||
/// | ||
/// This overrides all other TLS options set via other means. | ||
pub fn rustls_client_config(self, config: tokio_rustls::rustls::ClientConfig) -> Self { | ||
ClientTlsConfig { | ||
rustls_raw: Some(config), | ||
..self | ||
} | ||
} | ||
|
||
pub(crate) fn tls_connector(&self, uri: Uri) -> Result<TlsConnector, crate::Error> { | ||
let domain = match &self.domain { | ||
None => uri.to_string(), | ||
Some(domain) => domain.clone(), | ||
}; | ||
match &self.rustls_raw { | ||
None => { | ||
TlsConnector::new_with_rustls_cert(self.cert.clone(), self.identity.clone(), domain) | ||
} | ||
Some(c) => TlsConnector::new_with_rustls_raw(c.clone(), domain), | ||
} | ||
} | ||
} |
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.