#hyper #proxy #http-proxy #tokio #ssl

hyper-proxy2

A proxy connector for Hyper-based applications

1 unstable release

0.1.0 Jan 14, 2024

#488 in HTTP client

Download history 358/week @ 2024-07-19 370/week @ 2024-07-26 505/week @ 2024-08-02 338/week @ 2024-08-09 788/week @ 2024-08-16 951/week @ 2024-08-23 1241/week @ 2024-08-30 901/week @ 2024-09-06 1001/week @ 2024-09-13 1257/week @ 2024-09-20 1277/week @ 2024-09-27 1623/week @ 2024-10-04 2906/week @ 2024-10-11 3480/week @ 2024-10-18 5360/week @ 2024-10-25 4536/week @ 2024-11-01

16,661 downloads per month
Used in 12 crates (3 directly)

MIT license

41KB
795 lines

hyper-proxy2

Travis Build Status MIT licensed crates.io

A proxy connector for hyper based applications.

Documentation

Example

use std::error::Error;

use bytes::Bytes;
use headers::Authorization;
use http_body_util::{BodyExt, Empty};
use hyper::{Request, Uri};
use hyper_proxy2::{Proxy, ProxyConnector, Intercept};
use hyper_util::client::legacy::Client;
use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::rt::TokioExecutor;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
   let proxy = {
      let proxy_uri = "http://my-proxy:8080".parse().unwrap();
      let mut proxy = Proxy::new(Intercept::All, proxy_uri);
      proxy.set_authorization(Authorization::basic("John Doe", "Agent1234"));
      let connector = HttpConnector::new();
      let proxy_connector = ProxyConnector::from_proxy(connector, proxy).unwrap();
      proxy_connector
   };

   // Connecting to http will trigger regular GETs and POSTs.
   // We need to manually append the relevant headers to the request
   let uri: Uri = "http://my-remote-website.com".parse().unwrap();
   let mut req = Request::get(uri.clone()).body(Empty::<Bytes>::new()).unwrap();

   if let Some(headers) = proxy.http_headers(&uri) {
      req.headers_mut().extend(headers.clone().into_iter());
   }

   let client = Client::builder(TokioExecutor::new()).build(proxy);
   let fut_http = async {
      let res = client.request(req).await?;
      let body = res.into_body().collect().await?.to_bytes();

      Ok::<_, Box<dyn Error>>(String::from_utf8(body.to_vec()).unwrap())
   };

   // Connecting to an https uri is straightforward (uses 'CONNECT' method underneath)
   let uri = "https://my-remote-websitei-secured.com".parse().unwrap();
   let fut_https = async {
      let res = client.get(uri).await?;
      let body = res.into_body().collect().await?.to_bytes();

      Ok::<_, Box<dyn Error>>(String::from_utf8(body.to_vec()).unwrap())
   };

   let (http_res, https_res) = futures::future::join(fut_http, fut_https).await;
   let (_, _) = (http_res?, https_res?);

   Ok(())
}

Features

hyper-proxy2 exposes three main Cargo features, to configure which TLS implementation it uses to connect to a proxy. It can also be configured without TLS support, by compiling without default features entirely. The supported list of configurations is:

  1. No TLS support (default-features = false)
  2. TLS support via native-tls to link against the operating system's native TLS implementation (default)
  3. TLS support via rustls (default-features = false, features = ["rustls"])
  4. TLS support via rustls, using a statically-compiled set of CA certificates to bypass the operating system's default store (default-features = false, features = ["rustls-webpki"])

Credits

Large part of the code comes from reqwest. The core part as just been extracted and slightly enhanced.

Main changes are:

  • support for authentication
  • add non secured tunneling
  • add the possibility to add additional headers when connecting to the proxy

Dependencies

~6–18MB
~259K SLoC