pub struct RustlsConfig { /* private fields */ }
Available on crate feature tls-rustls only.
Expand description

Rustls configuration.

Implementations§

source§

impl RustlsConfig

source

pub fn from_config(config: Arc<ServerConfig>) -> Self

Create config from Arc<ServerConfig>.

NOTE: You need to set ALPN protocols (like http/1.1 or h2) manually.

source

pub async fn from_der(cert: Vec<Vec<u8>>, key: Vec<u8>) -> Result<Self>

Create config from DER-encoded data.

The certificate must be DER-encoded X.509.

The private key must be DER-encoded ASN.1 in either PKCS#8 or PKCS#1 format.

source

pub async fn from_pem(cert: Vec<u8>, key: Vec<u8>) -> Result<Self>

Create config from PEM formatted data.

Certificate and private key must be in PEM format.

source

pub async fn from_pem_file( cert: impl AsRef<Path>, key: impl AsRef<Path> ) -> Result<Self>

Create config from PEM formatted files.

Contents of certificate file and private key file must be in PEM format.

Examples found in repository?
examples/rustls_server.rs (lines 13-16)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, world!" }));

    let config = RustlsConfig::from_pem_file(
        "examples/self-signed-certs/cert.pem",
        "examples/self-signed-certs/key.pem",
    )
    .await
    .unwrap();

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    println!("listening on {}", addr);
    axum_server::tls_rustls::bind_rustls(addr, config)
        .serve(app.into_make_service())
        .await
        .unwrap();
}
More examples
Hide additional examples
examples/http_and_https.rs (lines 39-42)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
async fn https_server() {
    let app = Router::new().route("/", get(|| async { "Hello, world!" }));

    let config = RustlsConfig::from_pem_file(
        "examples/self-signed-certs/cert.pem",
        "examples/self-signed-certs/key.pem",
    )
    .await
    .unwrap();

    let addr = SocketAddr::from(([127, 0, 0, 1], 3443));
    println!("https listening on {}", addr);
    axum_server::bind_rustls(addr, config)
        .serve(app.into_make_service())
        .await
        .unwrap();
}
examples/rustls_session.rs (lines 20-23)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
async fn main() {
    let app = Router::new().route("/", get(handler));

    let config = RustlsConfig::from_pem_file(
        "examples/self-signed-certs/cert.pem",
        "examples/self-signed-certs/key.pem",
    )
    .await
    .unwrap();

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    println!("listening on {}", addr);

    let acceptor = CustomAcceptor::new(RustlsAcceptor::new(config));
    let server = axum_server::bind(addr).acceptor(acceptor);

    server.serve(app.into_make_service()).await.unwrap();
}
examples/from_std_listener_rustls.rs (lines 14-17)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, world!" }));

    let config = RustlsConfig::from_pem_file(
        "examples/self-signed-certs/cert.pem",
        "examples/self-signed-certs/key.pem",
    )
    .await
    .unwrap();

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    let listener = TcpListener::bind(addr).unwrap();
    println!("listening on {}", addr);
    axum_server::from_tcp_rustls(listener, config)
        .serve(app.into_make_service())
        .await
        .unwrap();
}
examples/rustls_reload.rs (lines 18-21)
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, world!" }));

    let config = RustlsConfig::from_pem_file(
        "examples/self-signed-certs/cert.pem",
        "examples/self-signed-certs/key.pem",
    )
    .await
    .unwrap();

    // Spawn a task to reload tls.
    tokio::spawn(reload(config.clone()));

    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    println!("listening on {}", addr);
    axum_server::bind_rustls(addr, config)
        .serve(app.into_make_service())
        .await
        .unwrap();
}
source

pub fn get_inner(&self) -> Arc<ServerConfig>

Get inner Arc<ServerConfig>.

source

pub fn reload_from_config(&self, config: Arc<ServerConfig>)

Reload config from Arc<ServerConfig>.

source

pub async fn reload_from_der( &self, cert: Vec<Vec<u8>>, key: Vec<u8> ) -> Result<()>

Reload config from DER-encoded data.

The certificate must be DER-encoded X.509.

The private key must be DER-encoded ASN.1 in either PKCS#8 or PKCS#1 format.

source

pub async fn from_pem_chain_file( chain: impl AsRef<Path>, key: impl AsRef<Path> ) -> Result<Self>

This helper will establish a TLS server based on strong cipher suites from a PEM-formatted certificate chain and key.

source

pub async fn reload_from_pem(&self, cert: Vec<u8>, key: Vec<u8>) -> Result<()>

Reload config from PEM formatted data.

Certificate and private key must be in PEM format.

source

pub async fn reload_from_pem_file( &self, cert: impl AsRef<Path>, key: impl AsRef<Path> ) -> Result<()>

Reload config from PEM formatted files.

Contents of certificate file and private key file must be in PEM format.

Examples found in repository?
examples/rustls_reload.rs (lines 44-47)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
async fn reload(config: RustlsConfig) {
    // Wait for 20 seconds.
    sleep(Duration::from_secs(20)).await;

    println!("reloading rustls configuration");

    // Reload rustls configuration from new files.
    config
        .reload_from_pem_file(
            "examples/self-signed-certs/reload/cert.pem",
            "examples/self-signed-certs/reload/key.pem",
        )
        .await
        .unwrap();

    println!("rustls configuration reloaded");
}

Trait Implementations§

source§

impl Clone for RustlsConfig

source§

fn clone(&self) -> RustlsConfig

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for RustlsConfig

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more