1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::net::{Shutdown, SocketAddr};
use std::{io, time};

use actix_net::ssl; //::RustlsAcceptor;
use rustls::{ClientSession, ServerConfig, ServerSession};
use tokio_io::{AsyncRead, AsyncWrite};
use tokio_rustls::TlsStream;

use server::{IoStream, ServerFlags};

/// Support `SSL` connections via rustls package
///
/// `rust-tls` feature enables `RustlsAcceptor` type
pub struct RustlsAcceptor<T> {
    _t: ssl::RustlsAcceptor<T>,
}

impl<T: AsyncRead + AsyncWrite> RustlsAcceptor<T> {
    /// Create `RustlsAcceptor` with custom server flags.
    pub fn with_flags(
        mut config: ServerConfig, flags: ServerFlags,
    ) -> ssl::RustlsAcceptor<T> {
        let mut protos = Vec::new();
        if flags.contains(ServerFlags::HTTP2) {
            protos.push("h2".to_string());
        }
        if flags.contains(ServerFlags::HTTP1) {
            protos.push("http/1.1".to_string());
        }
        if !protos.is_empty() {
            config.set_protocols(&protos);
        }

        ssl::RustlsAcceptor::new(config)
    }
}

impl<Io: IoStream> IoStream for TlsStream<Io, ClientSession> {
    #[inline]
    fn shutdown(&mut self, _how: Shutdown) -> io::Result<()> {
        let _ = <Self as AsyncWrite>::shutdown(self);
        Ok(())
    }

    #[inline]
    fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
        self.get_mut().0.set_nodelay(nodelay)
    }

    #[inline]
    fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
        self.get_mut().0.set_linger(dur)
    }

    #[inline]
    fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
        self.get_mut().0.set_keepalive(dur)
    }
}

impl<Io: IoStream> IoStream for TlsStream<Io, ServerSession> {
    #[inline]
    fn shutdown(&mut self, _how: Shutdown) -> io::Result<()> {
        let _ = <Self as AsyncWrite>::shutdown(self);
        Ok(())
    }

    #[inline]
    fn peer_addr(&self) -> Option<SocketAddr> {
        self.get_ref().0.peer_addr()
    }

    #[inline]
    fn set_nodelay(&mut self, nodelay: bool) -> io::Result<()> {
        self.get_mut().0.set_nodelay(nodelay)
    }

    #[inline]
    fn set_linger(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
        self.get_mut().0.set_linger(dur)
    }

    #[inline]
    fn set_keepalive(&mut self, dur: Option<time::Duration>) -> io::Result<()> {
        self.get_mut().0.set_keepalive(dur)
    }
}