rama/http/client/proxy_connector/
mod.rs1mod 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#[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 #[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 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 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 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 #[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 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 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 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 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#[derive(Debug, Clone)]
207#[non_exhaustive]
208pub struct ProxyConnectorParts<Socks = (), Connect = (), ConnectUdp = (), Forward = ()> {
209 pub socks: Socks,
211 pub connect: Connect,
213 pub connect_udp: ConnectUdp,
215 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 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 pub fn forward_proxy_auth(mut self, enabled: bool) -> Self {
233 self.forward.set_proxy_auth(enabled);
234 self
235 }
236 }
237}