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
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
#[cfg(feature = "rustls-base")]
use tokio_rustls::client::TlsStream as RustlsStream;
#[cfg(feature = "tls")]
use tokio_native_tls::TlsStream;
#[cfg(feature = "openssl-tls")]
use tokio_openssl::SslStream as OpenSslStream;
use hyper::client::connect::{Connected, Connection};
#[cfg(feature = "rustls-base")]
pub type TlsStream<R> = RustlsStream<R>;
#[cfg(feature = "openssl-tls")]
pub type TlsStream<R> = OpenSslStream<R>;
pub enum ProxyStream<R> {
NoProxy(R),
Regular(R),
#[cfg(any(feature = "tls", feature = "rustls-base", feature = "openssl-tls"))]
Secured(TlsStream<R>),
}
macro_rules! match_fn_pinned {
($self:expr, $fn:ident, $ctx:expr, $buf:expr) => {
match $self.get_mut() {
ProxyStream::NoProxy(s) => Pin::new(s).$fn($ctx, $buf),
ProxyStream::Regular(s) => Pin::new(s).$fn($ctx, $buf),
#[cfg(any(feature = "tls", feature = "rustls-base", feature = "openssl-tls"))]
ProxyStream::Secured(s) => Pin::new(s).$fn($ctx, $buf),
}
};
($self:expr, $fn:ident, $ctx:expr) => {
match $self.get_mut() {
ProxyStream::NoProxy(s) => Pin::new(s).$fn($ctx),
ProxyStream::Regular(s) => Pin::new(s).$fn($ctx),
#[cfg(any(feature = "tls", feature = "rustls-base", feature = "openssl-tls"))]
ProxyStream::Secured(s) => Pin::new(s).$fn($ctx),
}
};
}
impl<R: AsyncRead + AsyncWrite + Unpin> AsyncRead for ProxyStream<R> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match_fn_pinned!(self, poll_read, cx, buf)
}
}
impl<R: AsyncRead + AsyncWrite + Unpin> AsyncWrite for ProxyStream<R> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
match_fn_pinned!(self, poll_write, cx, buf)
}
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[io::IoSlice<'_>],
) -> Poll<Result<usize, io::Error>> {
match_fn_pinned!(self, poll_write_vectored, cx, bufs)
}
fn is_write_vectored(&self) -> bool {
match self {
ProxyStream::NoProxy(s) => s.is_write_vectored(),
ProxyStream::Regular(s) => s.is_write_vectored(),
#[cfg(any(feature = "tls", feature = "rustls-base", feature = "openssl-tls"))]
ProxyStream::Secured(s) => s.is_write_vectored(),
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match_fn_pinned!(self, poll_flush, cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
match_fn_pinned!(self, poll_shutdown, cx)
}
}
impl<R: AsyncRead + AsyncWrite + Connection + Unpin> Connection for ProxyStream<R> {
fn connected(&self) -> Connected {
match self {
ProxyStream::NoProxy(s) => s.connected(),
ProxyStream::Regular(s) => s.connected().proxy(true),
#[cfg(feature = "tls")]
ProxyStream::Secured(s) => s.get_ref().get_ref().get_ref().connected().proxy(true),
#[cfg(feature = "rustls-base")]
ProxyStream::Secured(s) => s.get_ref().0.connected().proxy(true),
#[cfg(feature = "openssl-tls")]
ProxyStream::Secured(s) => s.get_ref().connected().proxy(true),
}
}
}