tonic_tls/lib.rs
1//! A tls adaptor for `tonic`.
2//!
3//! Supports various tls backend:
4//! * [native_tls](tokio_native_tls::native_tls) in mod [native].
5//! * [openssl](tokio_openssl) in mod [openssl].
6//! * [rustls](tokio_rustls::rustls) in mod [rustls].
7//! * [schannel](tokio_schannel) in mod [schannel].
8//!
9//! For full examples see [examples](https://github.com/youyuanwu/tonic-tls/tree/main/tonic-tls-tests/examples).
10//! Server example for openssl:
11//! # Examples
12//! Server example:
13//! ```no_run
14//! # use tower::Service;
15//! # use hyper::{Request, Response};
16//! # use tonic::{body::Body, server::NamedService, transport::{Server, server::TcpIncoming}};
17//! # use core::convert::Infallible;
18//! # use std::error::Error;
19//! use openssl::ssl::SslAcceptor;
20//! use tonic_tls::openssl::TlsIncoming;
21//! # fn main() { } // Cannot have type parameters, hence instead define:
22//! # fn run<S>(some_service: S, acceptor: SslAcceptor) -> Result<(), Box<dyn Error + Send + Sync>>
23//! # where
24//! # S: Service<Request<Body>, Response = Response<Body>, Error = Infallible> + NamedService + Clone + Send + Sync + 'static,
25//! # S::Future: Send + 'static,
26//! # {
27//! let addr = "127.0.0.1:1322".parse().unwrap();
28//! let inc = TlsIncoming::new(TcpIncoming::bind(addr).unwrap(), acceptor);
29//! Server::builder()
30//! .add_service(some_service)
31//! .serve_with_incoming(inc);
32//! # Ok(())
33//! # }
34//! ```
35//! Client example:
36//! ```
37//! async fn connect_tonic_channel(
38//! endpoint: tonic::transport::Endpoint,
39//! ssl_conn: openssl::ssl::SslConnector
40//! ) -> tonic::transport::Channel {
41//! endpoint.connect_with_connector(tonic_tls::openssl::TlsConnector::new(
42//! &endpoint,
43//! ssl_conn,
44//! "localhost".to_string(),
45//! )).await.unwrap()
46//! }
47//! ```
48#![doc(html_root_url = "https://docs.rs/tonic-tls/latest/tonic_tls/")]
49
50mod client;
51pub use client::{TlsConnector, connector_inner};
52mod endpoint;
53mod server;
54pub use server::{TlsAcceptor, incoming_inner};
55
56#[cfg(feature = "native")]
57pub mod native;
58
59#[cfg(feature = "rustls")]
60pub mod rustls;
61
62#[cfg(feature = "openssl")]
63pub mod openssl;
64
65#[cfg(all(feature = "openssl-ktls", target_os = "linux"))]
66pub mod openssl_ktls;
67
68#[cfg(all(feature = "schannel", target_os = "windows"))]
69pub mod schannel;
70
71/// A const that contains the on the wire `h2` alpn value
72/// to pass to tls backends.
73pub const ALPN_H2: &[u8] = b"h2";
74
75/// Common boxed error.
76pub type Error = Box<dyn std::error::Error + Send + Sync + 'static>;