1use std::{
2 net::SocketAddr,
3 pin::Pin,
4 task::{Context, Poll},
5};
6
7use bytes::Bytes;
8use tokio::{
9 io::{AsyncRead, AsyncWrite},
10 net::TcpStream,
11};
12
13use super::util;
14use crate::ExportKeyingMaterial;
15
16#[derive(Debug)]
17#[allow(clippy::large_enum_variant)]
18pub(crate) enum ProxyStream {
19 Raw(TcpStream),
20 Proxied(util::Chain<std::io::Cursor<Bytes>, MaybeTlsStream<TcpStream>>),
21}
22
23impl AsyncRead for ProxyStream {
24 fn poll_read(
25 mut self: Pin<&mut Self>,
26 cx: &mut Context<'_>,
27 buf: &mut tokio::io::ReadBuf<'_>,
28 ) -> Poll<std::io::Result<()>> {
29 match &mut *self {
30 Self::Raw(stream) => Pin::new(stream).poll_read(cx, buf),
31 Self::Proxied(stream) => Pin::new(stream).poll_read(cx, buf),
32 }
33 }
34}
35
36impl AsyncWrite for ProxyStream {
37 fn poll_write(
38 mut self: Pin<&mut Self>,
39 cx: &mut Context<'_>,
40 buf: &[u8],
41 ) -> Poll<Result<usize, std::io::Error>> {
42 match &mut *self {
43 Self::Raw(stream) => Pin::new(stream).poll_write(cx, buf),
44 Self::Proxied(stream) => Pin::new(stream.get_mut().1).poll_write(cx, buf),
45 }
46 }
47
48 fn poll_flush(
49 mut self: Pin<&mut Self>,
50 cx: &mut Context<'_>,
51 ) -> Poll<Result<(), std::io::Error>> {
52 match &mut *self {
53 Self::Raw(stream) => Pin::new(stream).poll_flush(cx),
54 Self::Proxied(stream) => Pin::new(stream.get_mut().1).poll_flush(cx),
55 }
56 }
57
58 fn poll_shutdown(
59 mut self: Pin<&mut Self>,
60 cx: &mut Context<'_>,
61 ) -> Poll<Result<(), std::io::Error>> {
62 match &mut *self {
63 Self::Raw(stream) => Pin::new(stream).poll_shutdown(cx),
64 Self::Proxied(stream) => Pin::new(stream.get_mut().1).poll_shutdown(cx),
65 }
66 }
67
68 fn poll_write_vectored(
69 mut self: Pin<&mut Self>,
70 cx: &mut Context<'_>,
71 bufs: &[std::io::IoSlice<'_>],
72 ) -> Poll<Result<usize, std::io::Error>> {
73 match &mut *self {
74 Self::Raw(stream) => Pin::new(stream).poll_write_vectored(cx, bufs),
75 Self::Proxied(stream) => Pin::new(stream.get_mut().1).poll_write_vectored(cx, bufs),
76 }
77 }
78
79 fn is_write_vectored(&self) -> bool {
80 match self {
81 ProxyStream::Raw(stream) => stream.is_write_vectored(),
82 ProxyStream::Proxied(stream) => stream.get_ref().1.is_write_vectored(),
83 }
84 }
85}
86
87impl ProxyStream {
88 pub(super) fn local_addr(&self) -> std::io::Result<SocketAddr> {
89 match self {
90 Self::Raw(s) => s.local_addr(),
91 Self::Proxied(s) => s.get_ref().1.as_ref().local_addr(),
92 }
93 }
94
95 pub(super) fn peer_addr(&self) -> std::io::Result<SocketAddr> {
96 match self {
97 Self::Raw(s) => s.peer_addr(),
98 Self::Proxied(s) => s.get_ref().1.as_ref().peer_addr(),
99 }
100 }
101}
102
103#[derive(Debug)]
104#[allow(clippy::large_enum_variant)]
105pub(crate) enum MaybeTlsStream<IO> {
106 Raw(IO),
107 Tls(tokio_rustls::client::TlsStream<IO>),
108 #[cfg(all(test, feature = "server"))]
109 Test(tokio::io::DuplexStream),
110}
111
112impl<IO> ExportKeyingMaterial for MaybeTlsStream<IO> {
113 fn export_keying_material<T: AsMut<[u8]>>(
114 &self,
115 output: T,
116 label: &[u8],
117 context: Option<&[u8]>,
118 ) -> Option<T> {
119 let Self::Tls(tls) = self else {
120 return None;
121 };
122 tls.get_ref()
123 .1
124 .export_keying_material(output, label, context)
125 .ok()
126 }
127}
128
129impl<IO: AsyncRead + AsyncWrite + Unpin> AsyncRead for MaybeTlsStream<IO> {
130 fn poll_read(
131 mut self: Pin<&mut Self>,
132 cx: &mut Context<'_>,
133 buf: &mut tokio::io::ReadBuf<'_>,
134 ) -> Poll<std::io::Result<()>> {
135 match &mut *self {
136 Self::Raw(stream) => Pin::new(stream).poll_read(cx, buf),
137 Self::Tls(stream) => Pin::new(stream).poll_read(cx, buf),
138 #[cfg(all(test, feature = "server"))]
139 Self::Test(stream) => Pin::new(stream).poll_read(cx, buf),
140 }
141 }
142}
143
144impl<IO: AsyncRead + AsyncWrite + Unpin> AsyncWrite for MaybeTlsStream<IO> {
145 fn poll_write(
146 mut self: Pin<&mut Self>,
147 cx: &mut Context<'_>,
148 buf: &[u8],
149 ) -> Poll<Result<usize, std::io::Error>> {
150 match &mut *self {
151 Self::Raw(stream) => Pin::new(stream).poll_write(cx, buf),
152 Self::Tls(stream) => Pin::new(stream).poll_write(cx, buf),
153 #[cfg(all(test, feature = "server"))]
154 Self::Test(stream) => Pin::new(stream).poll_write(cx, buf),
155 }
156 }
157
158 fn poll_flush(
159 mut self: Pin<&mut Self>,
160 cx: &mut Context<'_>,
161 ) -> Poll<Result<(), std::io::Error>> {
162 match &mut *self {
163 Self::Raw(stream) => Pin::new(stream).poll_flush(cx),
164 Self::Tls(stream) => Pin::new(stream).poll_flush(cx),
165 #[cfg(all(test, feature = "server"))]
166 Self::Test(stream) => Pin::new(stream).poll_flush(cx),
167 }
168 }
169
170 fn poll_shutdown(
171 mut self: Pin<&mut Self>,
172 cx: &mut Context<'_>,
173 ) -> Poll<Result<(), std::io::Error>> {
174 match &mut *self {
175 Self::Raw(stream) => Pin::new(stream).poll_shutdown(cx),
176 Self::Tls(stream) => Pin::new(stream).poll_shutdown(cx),
177 #[cfg(all(test, feature = "server"))]
178 Self::Test(stream) => Pin::new(stream).poll_shutdown(cx),
179 }
180 }
181
182 fn poll_write_vectored(
183 mut self: Pin<&mut Self>,
184 cx: &mut Context<'_>,
185 bufs: &[std::io::IoSlice<'_>],
186 ) -> Poll<Result<usize, std::io::Error>> {
187 match &mut *self {
188 Self::Raw(stream) => Pin::new(stream).poll_write_vectored(cx, bufs),
189 Self::Tls(stream) => Pin::new(stream).poll_write_vectored(cx, bufs),
190 #[cfg(all(test, feature = "server"))]
191 Self::Test(stream) => Pin::new(stream).poll_write_vectored(cx, bufs),
192 }
193 }
194
195 fn is_write_vectored(&self) -> bool {
196 match self {
197 Self::Raw(stream) => stream.is_write_vectored(),
198 Self::Tls(stream) => stream.is_write_vectored(),
199 #[cfg(all(test, feature = "server"))]
200 Self::Test(stream) => stream.is_write_vectored(),
201 }
202 }
203}
204
205impl<IO> AsRef<IO> for MaybeTlsStream<IO> {
206 fn as_ref(&self) -> &IO {
207 match self {
208 Self::Raw(s) => s,
209 Self::Tls(s) => s.get_ref().0,
210 #[cfg(all(test, feature = "server"))]
211 Self::Test(_) => unimplemented!("can't grab underlying IO in MaybeTlsStream::Test"),
212 }
213 }
214}