hyper_http_proxy/
lib.rs

1//! A Proxy Connector crate for Hyper based applications
2//!
3//! # Example
4//! ```rust,no_run
5//! use hyper::{Request, Uri, body::Body};
6//! use hyper_util::client::legacy::Client;
7//! use hyper_util::client::legacy::connect::HttpConnector;
8//! use hyper_util::rt::TokioExecutor;
9//! use bytes::Bytes;
10//! use futures_util::{TryFutureExt, TryStreamExt};
11//! use http_body_util::{BodyExt, Empty};
12//! use hyper_http_proxy::{Proxy, ProxyConnector, Intercept};
13//! use headers::Authorization;
14//! use std::error::Error;
15//! use tokio::io::{stdout, AsyncWriteExt as _};
16//!
17//! #[tokio::main]
18//! async fn main() -> Result<(), Box<dyn Error>> {
19//! let proxy = {
20//!         let proxy_uri = "http://my-proxy:8080".parse().unwrap();
21//!         let mut proxy = Proxy::new(Intercept::All, proxy_uri);
22//!         proxy.set_authorization(Authorization::basic("John Doe", "Agent1234"));
23//!         let connector = HttpConnector::new();
24//!         # #[cfg(not(any(feature = "tls", feature = "rustls-base", feature = "openssl-tls")))]
25//!         # let proxy_connector = ProxyConnector::from_proxy_unsecured(connector, proxy);
26//!         # #[cfg(any(feature = "tls", feature = "rustls-base", feature = "openssl"))]
27//!         let proxy_connector = ProxyConnector::from_proxy(connector, proxy).unwrap();
28//!         proxy_connector
29//!     };
30//!
31//!     // Connecting to http will trigger regular GETs and POSTs.
32//!     // We need to manually append the relevant headers to the request
33//!     let uri: Uri = "http://my-remote-website.com".parse().unwrap();
34//!     let mut req = Request::get(uri.clone()).body(Empty::<Bytes>::new()).unwrap();
35//!
36//!     if let Some(headers) = proxy.http_headers(&uri) {
37//!         req.headers_mut().extend(headers.clone().into_iter());
38//!     }
39//!
40//!     let client = Client::builder(TokioExecutor::new()).build(proxy);
41//!     let mut resp = client.request(req).await?;
42//!     println!("Response: {}", resp.status());
43//!     while let Some(chunk) = resp.body_mut().collect().await.ok().map(|c| c.to_bytes()) {
44//!         stdout().write_all(&chunk).await?;
45//!     }
46//!
47//!     // Connecting to an https uri is straightforward (uses 'CONNECT' method underneath)
48//!     let uri = "https://my-remote-websitei-secured.com".parse().unwrap();
49//!     let mut resp = client.get(uri).await?;
50//!     println!("Response: {}", resp.status());
51//!     while let Some(chunk) = resp.body_mut().collect().await.ok().map(|c| c.to_bytes()) {
52//!         stdout().write_all(&chunk).await?;
53//!     }
54//!
55//!     Ok(())
56//! }
57//! ```
58
59#![allow(missing_docs)]
60
61mod rt;
62mod stream;
63mod tunnel;
64
65use std::{fmt, io, sync::Arc};
66use std::{
67    future::Future,
68    pin::Pin,
69    task::{Context, Poll},
70};
71
72use futures_util::future::TryFutureExt;
73use headers::{authorization::Credentials, Authorization, HeaderMapExt, ProxyAuthorization};
74use http::header::{HeaderMap, HeaderName, HeaderValue};
75use hyper::rt::{Read, Write};
76use hyper::Uri;
77use tower_service::Service;
78
79pub use stream::ProxyStream;
80
81#[cfg(all(not(feature = "__rustls"), feature = "native-tls"))]
82use native_tls::TlsConnector as NativeTlsConnector;
83
84#[cfg(all(not(feature = "__rustls"), feature = "native-tls"))]
85use tokio_native_tls::TlsConnector;
86
87#[cfg(feature = "__rustls")]
88use hyper_rustls::ConfigBuilderExt;
89
90#[cfg(feature = "__rustls")]
91use tokio_rustls::TlsConnector;
92
93#[cfg(feature = "__rustls")]
94use tokio_rustls::rustls::pki_types::ServerName;
95
96type BoxError = Box<dyn std::error::Error + Send + Sync>;
97
98/// The Intercept enum to filter connections
99#[derive(Debug, Clone)]
100pub enum Intercept {
101    /// All incoming connection will go through proxy
102    All,
103    /// Only http connections will go through proxy
104    Http,
105    /// Only https connections will go through proxy
106    Https,
107    /// No connection will go through this proxy
108    None,
109    /// A custom intercept
110    Custom(Custom),
111}
112
113/// A trait for matching between Destination and Uri
114pub trait Dst {
115    /// Returns the connection scheme, e.g. "http" or "https"
116    fn scheme(&self) -> Option<&str>;
117    /// Returns the host of the connection
118    fn host(&self) -> Option<&str>;
119    /// Returns the port for the connection
120    fn port(&self) -> Option<u16>;
121}
122
123impl Dst for Uri {
124    fn scheme(&self) -> Option<&str> {
125        self.scheme_str()
126    }
127
128    fn host(&self) -> Option<&str> {
129        self.host()
130    }
131
132    fn port(&self) -> Option<u16> {
133        self.port_u16()
134    }
135}
136
137#[inline]
138pub(crate) fn io_err<E: Into<Box<dyn std::error::Error + Send + Sync>>>(e: E) -> io::Error {
139    io::Error::new(io::ErrorKind::Other, e)
140}
141
142pub type CustomProxyCallback =
143    dyn Fn(Option<&str>, Option<&str>, Option<u16>) -> bool + Send + Sync;
144
145/// A Custom struct to proxy custom uris
146#[derive(Clone)]
147pub struct Custom(Arc<CustomProxyCallback>);
148
149impl fmt::Debug for Custom {
150    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
151        write!(f, "_")
152    }
153}
154
155impl<F: Fn(Option<&str>, Option<&str>, Option<u16>) -> bool + Send + Sync + 'static> From<F>
156    for Custom
157{
158    fn from(f: F) -> Custom {
159        Custom(Arc::new(f))
160    }
161}
162
163impl Intercept {
164    /// A function to check if given `Uri` is proxied
165    pub fn matches<D: Dst>(&self, uri: &D) -> bool {
166        match (self, uri.scheme()) {
167            (&Intercept::All, _)
168            | (&Intercept::Http, Some("http"))
169            | (&Intercept::Https, Some("https")) => true,
170            (&Intercept::Custom(Custom(ref f)), _) => f(uri.scheme(), uri.host(), uri.port()),
171            _ => false,
172        }
173    }
174}
175
176impl<F: Fn(Option<&str>, Option<&str>, Option<u16>) -> bool + Send + Sync + 'static> From<F>
177    for Intercept
178{
179    fn from(f: F) -> Intercept {
180        Intercept::Custom(f.into())
181    }
182}
183
184/// A Proxy struct
185#[derive(Clone, Debug)]
186pub struct Proxy {
187    intercept: Intercept,
188    force_connect: bool,
189    headers: HeaderMap,
190    uri: Uri,
191}
192
193impl Proxy {
194    /// Create a new `Proxy`
195    pub fn new<I: Into<Intercept>>(intercept: I, uri: Uri) -> Proxy {
196        let mut proxy = Proxy {
197            intercept: intercept.into(),
198            uri: uri.clone(),
199            headers: HeaderMap::new(),
200            force_connect: false,
201        };
202
203        if let Some((user, pass)) = extract_user_pass(&uri) {
204            proxy.set_authorization(Authorization::basic(user, pass));
205        }
206
207        proxy
208    }
209
210    /// Set `Proxy` authorization
211    pub fn set_authorization<C: Credentials + Clone>(&mut self, credentials: Authorization<C>) {
212        match self.intercept {
213            Intercept::Http => {
214                self.headers.typed_insert(Authorization(credentials.0));
215            }
216            Intercept::Https => {
217                self.headers.typed_insert(ProxyAuthorization(credentials.0));
218            }
219            _ => {
220                self.headers
221                    .typed_insert(Authorization(credentials.0.clone()));
222                self.headers.typed_insert(ProxyAuthorization(credentials.0));
223            }
224        }
225    }
226
227    /// Forces the use of the CONNECT method.
228    pub fn force_connect(&mut self) {
229        self.force_connect = true;
230    }
231
232    /// Set a custom header
233    pub fn set_header(&mut self, name: HeaderName, value: HeaderValue) {
234        self.headers.insert(name, value);
235    }
236
237    /// Get current intercept
238    pub fn intercept(&self) -> &Intercept {
239        &self.intercept
240    }
241
242    /// Get current `Headers` which must be sent to proxy
243    pub fn headers(&self) -> &HeaderMap {
244        &self.headers
245    }
246
247    /// Get proxy uri
248    pub fn uri(&self) -> &Uri {
249        &self.uri
250    }
251}
252
253/// A wrapper around `Proxy`s with a connector.
254#[derive(Clone)]
255pub struct ProxyConnector<C> {
256    proxies: Vec<Proxy>,
257    connector: C,
258
259    #[cfg(all(not(feature = "__rustls"), feature = "native-tls"))]
260    tls: Option<NativeTlsConnector>,
261
262    #[cfg(feature = "__rustls")]
263    tls: Option<TlsConnector>,
264
265    #[cfg(not(feature = "__tls"))]
266    tls: Option<()>,
267}
268
269impl<C: fmt::Debug> fmt::Debug for ProxyConnector<C> {
270    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
271        write!(
272            f,
273            "ProxyConnector {}{{ proxies: {:?}, connector: {:?} }}",
274            if self.tls.is_some() {
275                ""
276            } else {
277                "(unsecured)"
278            },
279            self.proxies,
280            self.connector
281        )
282    }
283}
284
285impl<C> ProxyConnector<C> {
286    /// Create a new secured Proxies
287    #[cfg(all(not(feature = "__rustls"), feature = "native-tls"))]
288    pub fn new(connector: C) -> Result<Self, io::Error> {
289        let tls = NativeTlsConnector::builder()
290            .build()
291            .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
292
293        Ok(ProxyConnector {
294            proxies: Vec::new(),
295            connector: connector,
296            tls: Some(tls),
297        })
298    }
299
300    /// Create a new secured Proxies
301    #[cfg(feature = "__rustls")]
302    pub fn new(connector: C) -> Result<Self, io::Error> {
303        let config = tokio_rustls::rustls::ClientConfig::builder();
304
305        #[cfg(feature = "rustls-tls-native-roots")]
306        let config = config.with_native_roots()?;
307
308        #[cfg(feature = "rustls-tls-webpki-roots")]
309        let config = config.with_webpki_roots();
310
311        let cfg = Arc::new(config.with_no_client_auth());
312        let tls = TlsConnector::from(cfg);
313
314        Ok(ProxyConnector {
315            proxies: Vec::new(),
316            connector,
317            tls: Some(tls),
318        })
319    }
320
321    /// Create a new unsecured Proxy
322    pub fn unsecured(connector: C) -> Self {
323        ProxyConnector {
324            proxies: Vec::new(),
325            connector,
326            tls: None,
327        }
328    }
329
330    /// Create a proxy connector and attach a particular proxy
331    #[cfg(feature = "__tls")]
332    pub fn from_proxy(connector: C, proxy: Proxy) -> Result<Self, io::Error> {
333        let mut c = ProxyConnector::new(connector)?;
334        c.proxies.push(proxy);
335        Ok(c)
336    }
337
338    /// Create a proxy connector and attach a particular proxy
339    pub fn from_proxy_unsecured(connector: C, proxy: Proxy) -> Self {
340        let mut c = ProxyConnector::unsecured(connector);
341        c.proxies.push(proxy);
342        c
343    }
344
345    /// Change proxy connector
346    pub fn with_connector<CC>(self, connector: CC) -> ProxyConnector<CC> {
347        ProxyConnector {
348            connector,
349            proxies: self.proxies,
350            tls: self.tls,
351        }
352    }
353
354    /// Set or unset tls when tunneling
355    #[cfg(all(not(feature = "__rustls"), feature = "native-tls"))]
356    pub fn set_tls(&mut self, tls: Option<NativeTlsConnector>) {
357        self.tls = tls;
358    }
359
360    /// Set or unset tls when tunneling
361    #[cfg(feature = "__rustls")]
362    pub fn set_tls(&mut self, tls: Option<TlsConnector>) {
363        self.tls = tls;
364    }
365
366    /// Get the current proxies
367    pub fn proxies(&self) -> &[Proxy] {
368        &self.proxies
369    }
370
371    /// Add a new additional proxy
372    pub fn add_proxy(&mut self, proxy: Proxy) {
373        self.proxies.push(proxy);
374    }
375
376    /// Extend the list of proxies
377    pub fn extend_proxies<I: IntoIterator<Item = Proxy>>(&mut self, proxies: I) {
378        self.proxies.extend(proxies)
379    }
380
381    /// Get http headers for a matching uri
382    ///
383    /// These headers must be appended to the hyper Request for the proxy to work properly.
384    /// This is needed only for http requests.
385    pub fn http_headers(&self, uri: &Uri) -> Option<&HeaderMap> {
386        if uri.scheme_str().map_or(true, |s| s != "http") {
387            return None;
388        }
389
390        self.match_proxy(uri).map(|p| &p.headers)
391    }
392
393    fn match_proxy<D: Dst>(&self, uri: &D) -> Option<&Proxy> {
394        self.proxies.iter().find(|p| p.intercept.matches(uri))
395    }
396}
397
398macro_rules! mtry {
399    ($e:expr) => {
400        match $e {
401            Ok(v) => v,
402            Err(e) => break Err(e.into()),
403        }
404    };
405}
406
407impl<C> Service<Uri> for ProxyConnector<C>
408where
409    C: Service<Uri>,
410    C::Response: Read + Write + Send + Unpin + 'static,
411    C::Future: Send + 'static,
412    C::Error: Into<BoxError>,
413{
414    type Response = ProxyStream<C::Response>;
415    type Error = io::Error;
416    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
417
418    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
419        match self.connector.poll_ready(cx) {
420            Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
421            Poll::Ready(Err(e)) => Poll::Ready(Err(io_err(e.into()))),
422            Poll::Pending => Poll::Pending,
423        }
424    }
425
426    fn call(&mut self, uri: Uri) -> Self::Future {
427        if let (Some(p), Some(host)) = (self.match_proxy(&uri), uri.host()) {
428            if uri.scheme() == Some(&http::uri::Scheme::HTTPS) || p.force_connect {
429                let host = host.to_owned();
430                let port =
431                    uri.port_u16()
432                        .unwrap_or(if uri.scheme() == Some(&http::uri::Scheme::HTTP) {
433                            80
434                        } else {
435                            443
436                        });
437
438                let tunnel = tunnel::new(&host, port, &p.headers);
439                let connection =
440                    proxy_dst(&uri, &p.uri).map(|proxy_url| self.connector.call(proxy_url));
441                let tls = if uri.scheme() == Some(&http::uri::Scheme::HTTPS) {
442                    self.tls.clone()
443                } else {
444                    None
445                };
446
447                Box::pin(async move {
448                    // this hack will gone once `try_blocks` will eventually stabilized
449                    #[allow(clippy::never_loop)]
450                    loop {
451                        let proxy_stream = mtry!(mtry!(connection).await.map_err(io_err));
452                        let tunnel_stream = mtry!(tunnel.with_stream(proxy_stream).await);
453
454                        break match tls {
455                            #[cfg(all(not(feature = "__rustls"), feature = "native-tls"))]
456                            Some(tls) => {
457                                use hyper_util::rt::TokioIo;
458                                let tls = TlsConnector::from(tls);
459                                let secure_stream = mtry!(tls
460                                    .connect(&host, TokioIo::new(tunnel_stream))
461                                    .await
462                                    .map_err(io_err));
463
464                                Ok(ProxyStream::Secured(Box::new(TokioIo::new(secure_stream))))
465                            }
466
467                            #[cfg(feature = "__rustls")]
468                            Some(tls) => {
469                                use hyper_util::rt::TokioIo;
470                                let server_name =
471                                    mtry!(ServerName::try_from(host.to_string()).map_err(io_err));
472                                let secure_stream = mtry!(tls
473                                    .connect(server_name, TokioIo::new(tunnel_stream))
474                                    .await
475                                    .map_err(io_err));
476
477                                Ok(ProxyStream::Secured(Box::new(TokioIo::new(secure_stream))))
478                            }
479
480                            #[cfg(not(feature = "__tls",))]
481                            Some(_) => panic!("hyper-proxy was not built with TLS support"),
482
483                            None => Ok(ProxyStream::Regular(tunnel_stream)),
484                        };
485                    }
486                })
487            } else {
488                match proxy_dst(&uri, &p.uri) {
489                    Ok(proxy_uri) => Box::pin(
490                        self.connector
491                            .call(proxy_uri)
492                            .map_ok(ProxyStream::Regular)
493                            .map_err(|err| io_err(err.into())),
494                    ),
495                    Err(err) => Box::pin(futures_util::future::err(io_err(err))),
496                }
497            }
498        } else {
499            Box::pin(
500                self.connector
501                    .call(uri)
502                    .map_ok(ProxyStream::NoProxy)
503                    .map_err(|err| io_err(err.into())),
504            )
505        }
506    }
507}
508
509fn proxy_dst(dst: &Uri, proxy: &Uri) -> io::Result<Uri> {
510    Uri::builder()
511        .scheme(
512            proxy
513                .scheme_str()
514                .ok_or_else(|| io_err(format!("proxy uri missing scheme: {}", proxy)))?,
515        )
516        .authority(
517            proxy
518                .authority()
519                .ok_or_else(|| io_err(format!("proxy uri missing host: {}", proxy)))?
520                .clone(),
521        )
522        .path_and_query(dst.path_and_query().unwrap().clone())
523        .build()
524        .map_err(|err| io_err(format!("other error: {}", err)))
525}
526
527/// Extracts the username and password from the URI
528fn extract_user_pass(uri: &Uri) -> Option<(&str, &str)> {
529    let authority = uri.authority()?.as_str();
530    let (userinfo, _) = authority.rsplit_once('@')?;
531    let mut parts = userinfo.splitn(2, ':');
532    let username = parts.next()?;
533    let password = parts.next()?;
534    Some((username, password))
535}
536
537#[cfg(test)]
538mod tests {
539    use http::Uri;
540
541    use crate::{Intercept, Proxy};
542
543    #[test]
544    fn test_new_proxy_with_authorization() {
545        let proxy = Proxy::new(
546            Intercept::All,
547            Uri::from_static("https://bob:secret@my-proxy:8080"),
548        );
549
550        assert_eq!(
551            proxy
552                .headers()
553                .get("authorization")
554                .unwrap()
555                .to_str()
556                .unwrap(),
557            "Basic Ym9iOnNlY3JldA=="
558        );
559    }
560
561    #[test]
562    fn test_new_proxy_without_authorization() {
563        let proxy = Proxy::new(Intercept::All, Uri::from_static("https://my-proxy:8080"));
564
565        assert_eq!(proxy.headers().get("authorization"), None);
566    }
567}