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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use rustls::ClientConnection;
use rustls::ServerConnection;
use rustls::StreamOwned;
use std::fmt::Arguments;
use std::io;
use std::io::IoSlice;
use std::io::IoSliceMut;
use std::io::Read;
use std::io::Write;

pub enum RustlsSessionRef<'a> {
    Client(&'a ClientConnection),
    Server(&'a ServerConnection),
}

/// Merge client and server stream into single interface
pub(crate) enum RustlsStream<S: Read + Write> {
    Server(StreamOwned<ServerConnection, S>),
    Client(StreamOwned<ClientConnection, S>),
}

impl<S: Read + Write> RustlsStream<S> {
    pub fn session(&self) -> RustlsSessionRef {
        match self {
            RustlsStream::Server(s) => RustlsSessionRef::Server(&s.conn),
            RustlsStream::Client(s) => RustlsSessionRef::Client(&s.conn),
        }
    }
}

impl<S: Read + Write> RustlsStream<S> {
    pub fn get_socket_mut(&mut self) -> &mut S {
        match self {
            RustlsStream::Server(s) => s.get_mut(),
            RustlsStream::Client(s) => s.get_mut(),
        }
    }

    pub fn get_socket_ref(&self) -> &S {
        match self {
            RustlsStream::Server(s) => s.get_ref(),
            RustlsStream::Client(s) => s.get_ref(),
        }
    }

    pub fn is_handshaking(&self) -> bool {
        match self {
            RustlsStream::Server(s) => s.conn.is_handshaking(),
            RustlsStream::Client(s) => s.conn.is_handshaking(),
        }
    }

    pub fn complete_io(&mut self) -> io::Result<(usize, usize)> {
        match self {
            RustlsStream::Server(s) => s.conn.complete_io(&mut s.sock),
            RustlsStream::Client(s) => s.conn.complete_io(&mut s.sock),
        }
    }

    pub fn get_alpn_protocol(&self) -> Option<&[u8]> {
        match self {
            RustlsStream::Server(s) => s.conn.alpn_protocol(),
            RustlsStream::Client(s) => s.conn.alpn_protocol(),
        }
    }
}

impl<S: Read + Write> Write for RustlsStream<S> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        match self {
            RustlsStream::Server(s) => s.write(buf),
            RustlsStream::Client(s) => s.write(buf),
        }
    }

    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
        match self {
            RustlsStream::Server(s) => s.write_vectored(bufs),
            RustlsStream::Client(s) => s.write_vectored(bufs),
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        match self {
            RustlsStream::Server(s) => s.flush(),
            RustlsStream::Client(s) => s.flush(),
        }
    }

    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        match self {
            RustlsStream::Server(s) => s.write_all(buf),
            RustlsStream::Client(s) => s.write_all(buf),
        }
    }

    fn write_fmt(&mut self, fmt: Arguments<'_>) -> io::Result<()> {
        match self {
            RustlsStream::Server(s) => s.write_fmt(fmt),
            RustlsStream::Client(s) => s.write_fmt(fmt),
        }
    }
}

impl<S: Read + Write> Read for RustlsStream<S> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        match self {
            RustlsStream::Server(s) => s.read(buf),
            RustlsStream::Client(s) => s.read(buf),
        }
    }

    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
        match self {
            RustlsStream::Server(s) => s.read_vectored(bufs),
            RustlsStream::Client(s) => s.read_vectored(bufs),
        }
    }

    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
        match self {
            RustlsStream::Server(s) => s.read_to_end(buf),
            RustlsStream::Client(s) => s.read_to_end(buf),
        }
    }

    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
        match self {
            RustlsStream::Server(s) => s.read_to_string(buf),
            RustlsStream::Client(s) => s.read_to_string(buf),
        }
    }

    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
        match self {
            RustlsStream::Server(s) => s.read_exact(buf),
            RustlsStream::Client(s) => s.read_exact(buf),
        }
    }
}