Skip to main content

rama/http/client/proxy_connector/
mod.rs

1mod builder;
2use builder::connect_udp_not_enabled;
3pub(crate) use builder::{
4    DefaultProxyConnectMethod, DefaultProxyConnectUdpMethod, DefaultProxyForwardMethod,
5    DefaultProxyHttpConnector, DefaultProxySocksMethod,
6};
7#[doc(inline)]
8pub use builder::{
9    DefaultProxyConnector, ProxyAltSvcConnectionLayer, ProxyConnectUdpMethod,
10    ProxyConnectUdpNotEnabled, ProxyConnectorBuilder, ProxyHttpStage, ProxyMethodBuilder,
11    ProxyServiceConnector, ProxySocksMethod,
12};
13
14mod forward;
15#[doc(inline)]
16pub use forward::{MaybeForwardedConnection, MaybeForwardedConnector};
17
18mod not_enabled;
19#[doc(inline)]
20pub use not_enabled::{NoProxyConnection, ProxyMethodNotEnabled};
21
22mod stream;
23#[doc(inline)]
24pub use stream::{ProxyStream, ProxyStreamConnector};
25
26use crate::{
27    error::BoxError,
28    graceful::WeakScope,
29    http::{StreamingBody, client::proxy::HttpForwardProxyConnector},
30    net::client::ProxyDialer,
31    tcp::client::service::TcpConnector,
32};
33use rama_utils::macros::generate_set_and_with;
34
35#[cfg(any(feature = "rustls", feature = "boring"))]
36use rama_tls::client::TlsClientConfig;
37
38/// The proxy methods a client reaches origins with, each over the connections towards
39/// proxies they were built on: see [`ProxyConnector::builder`].
40///
41/// Every method has a slot of its own, and one that is not enabled refuses the routes
42/// asking for it rather than quietly serving them another way. A client's builder takes the
43/// parts out and places each of them where it belongs, in `with_proxy_support`.
44#[derive(Debug, Clone)]
45pub struct ProxyConnector<Socks = (), Connect = (), ConnectUdp = (), Forward = ()> {
46    socks: Socks,
47    connect: Connect,
48    connect_udp: ConnectUdp,
49    forward: Forward,
50}
51
52impl ProxyConnector {
53    /// Build the connections towards proxies of a client and the methods it speaks on them.
54    #[must_use]
55    pub fn builder(scope: impl Into<WeakScope>) -> ProxyConnectorBuilder {
56        ProxyConnectorBuilder::new(scope)
57    }
58
59    core::cfg_select! {
60        any(feature = "boring", feature = "rustls") => {
61            /// Every proxy method this build of rama has, over rama's default connections
62            /// towards proxies: TCP, QUIC for an `h3://` one, the global DNS resolver, and TLS
63            /// for a secure one.
64            ///
65            /// `Body` is the body type sent to a proxy, the one the client serves origins with.
66            pub fn default_with_scope<Body>(
67                scope: impl Into<WeakScope>,
68            ) -> DefaultProxyConnector<TcpConnector, Body>
69            where
70                Body: StreamingBody<Data: Send + 'static, Error: Into<BoxError>>
71                    + Unpin
72                    + Send
73                    + 'static,
74            {
75                Self::default_using_tls(scope, TlsClientConfig::default_http())
76            }
77
78            /// [`Self::default_with_scope`], with `tls` towards the proxies.
79            pub fn default_using_tls<Body>(
80                scope: impl Into<WeakScope>,
81                tls: TlsClientConfig,
82            ) -> DefaultProxyConnector<TcpConnector, Body>
83            where
84                Body: StreamingBody<Data: Send + 'static, Error: Into<BoxError>>
85                    + Unpin
86                    + Send
87                    + 'static,
88            {
89                Self::builder(scope)
90                    .with_default_transport_connector()
91                    .with_default_dns_connector()
92                    .with_default_tls_support(tls)
93                    .with_default_http_connector()
94                    .with_default_connection_pool()
95                    .with_all_proxy_protocols()
96                    .build()
97            }
98        }
99        _ => {
100            /// Every proxy method this build of rama has, over rama's default connections
101            /// towards proxies, which have no TLS to reach an `https://` proxy with.
102            ///
103            /// `Body` is the body type sent to a proxy, the one the client serves origins with.
104            pub fn default_with_scope<Body>(
105                scope: impl Into<WeakScope>,
106            ) -> DefaultProxyConnector<TcpConnector, Body>
107            where
108                Body: StreamingBody<Data: Send + 'static, Error: Into<BoxError>>
109                    + Unpin
110                    + Send
111                    + 'static,
112            {
113                Self::builder(scope)
114                    .with_default_transport_connector()
115                    .with_default_dns_connector()
116                    .without_tls_support()
117                    .with_default_http_connector()
118                    .with_default_connection_pool()
119                    .with_all_proxy_protocols()
120                    .build()
121            }
122        }
123    }
124}
125
126impl
127    ProxyConnector<
128        ProxyMethodNotEnabled,
129        ProxyMethodNotEnabled,
130        ProxyConnectUdpNotEnabled<()>,
131        ProxyMethodNotEnabled,
132    >
133{
134    /// A [`ProxyConnector`] with no method yet: each one refuses its routes until it is
135    /// given with `with_*`, as its own stack of layers.
136    #[must_use]
137    pub fn new() -> Self {
138        Self::from_methods(
139            ProxyMethodNotEnabled::SOCKS5,
140            ProxyMethodNotEnabled::HTTP_CONNECT,
141            connect_udp_not_enabled(&WeakScope::detached()),
142            ProxyMethodNotEnabled::HTTP_FORWARD,
143        )
144    }
145}
146
147impl Default
148    for ProxyConnector<
149        ProxyMethodNotEnabled,
150        ProxyMethodNotEnabled,
151        ProxyConnectUdpNotEnabled<()>,
152        ProxyMethodNotEnabled,
153    >
154{
155    fn default() -> Self {
156        Self::new()
157    }
158}
159
160impl<Socks, Connect, ConnectUdp, Forward> ProxyConnector<Socks, Connect, ConnectUdp, Forward> {
161    /// Open a tunnel to the origin over a SOCKS5 proxy with `socks`.
162    pub fn with_socks<S>(self, socks: S) -> ProxyConnector<S, Connect, ConnectUdp, Forward> {
163        ProxyConnector::from_methods(socks, self.connect, self.connect_udp, self.forward)
164    }
165
166    /// Open a tunnel to the origin over an HTTP proxy with CONNECT, with `connect`.
167    pub fn with_connect<C>(self, connect: C) -> ProxyConnector<Socks, C, ConnectUdp, Forward> {
168        ProxyConnector::from_methods(self.socks, connect, self.connect_udp, self.forward)
169    }
170
171    /// Open a datagram tunnel to the origin with CONNECT-UDP, with `connect_udp`.
172    pub fn with_connect_udp<U>(self, connect_udp: U) -> ProxyConnector<Socks, Connect, U, Forward> {
173        ProxyConnector::from_methods(self.socks, self.connect, connect_udp, self.forward)
174    }
175
176    /// Have an HTTP proxy forward a plaintext request, with `forward`.
177    pub fn with_forward<F>(self, forward: F) -> ProxyConnector<Socks, Connect, ConnectUdp, F> {
178        ProxyConnector::from_methods(self.socks, self.connect, self.connect_udp, forward)
179    }
180
181    pub(in crate::http::client) const fn from_methods(
182        socks: Socks,
183        connect: Connect,
184        connect_udp: ConnectUdp,
185        forward: Forward,
186    ) -> Self {
187        Self {
188            socks,
189            connect,
190            connect_udp,
191            forward,
192        }
193    }
194
195    pub fn into_parts(self) -> ProxyConnectorParts<Socks, Connect, ConnectUdp, Forward> {
196        ProxyConnectorParts {
197            socks: self.socks,
198            connect: self.connect,
199            connect_udp: self.connect_udp,
200            forward: self.forward,
201        }
202    }
203}
204
205/// The proxy methods of a [`ProxyConnector`], taken apart with [`ProxyConnector::into_parts`].
206#[derive(Debug, Clone)]
207#[non_exhaustive]
208pub struct ProxyConnectorParts<Socks = (), Connect = (), ConnectUdp = (), Forward = ()> {
209    /// The connector which opens a tunnel to the origin over a SOCKS5 proxy.
210    pub socks: Socks,
211    /// The connector which opens a tunnel to the origin over an HTTP proxy with CONNECT.
212    pub connect: Connect,
213    /// The connector which opens a datagram tunnel to the origin with CONNECT-UDP.
214    pub connect_udp: ConnectUdp,
215    /// The connector for requests an HTTP proxy forwards.
216    pub forward: Forward,
217}
218
219impl<Socks, Connect, ConnectUdp, C>
220    ProxyConnector<Socks, Connect, ConnectUdp, HttpForwardProxyConnector<ProxyDialer<C>>>
221{
222    generate_set_and_with! {
223        /// See [`HttpForwardProxyConnector::with_isolate_auth_error`]; disabled by default.
224        pub fn isolate_forward_proxy_auth_error(mut self, enabled: bool) -> Self {
225            self.forward.set_isolate_auth_error(enabled);
226            self
227        }
228    }
229
230    generate_set_and_with! {
231        /// See [`HttpForwardProxyConnector::with_proxy_auth`]; enabled by default.
232        pub fn forward_proxy_auth(mut self, enabled: bool) -> Self {
233            self.forward.set_proxy_auth(enabled);
234            self
235        }
236    }
237}