Sunbeam Studios

Our open-source projects, here for you!

hyper-http-proxy (1.1.0)

Published 2026-03-26 10:59:08 +00:00 by siennathesane

Installation

[registry]
default = "gitea"

[registries.gitea]
index = "sparse+" # Sparse index
# index = "" # Git

[net]
git-fetch-with-cli = true
cargo add hyper-http-proxy@1.1.0

About this package

A proxy connector for Hyper-based applications

hyper-http-proxy

Checks 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_http_proxy::{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-http-proxy exposes 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:

native-tls = ["dep:native-tls", "tokio-native-tls", "hyper-tls", "__tls"] native-tls-vendored = ["native-tls", "tokio-native-tls?/vendored"] rustls-tls-manual-roots = ["__rustls"] rustls-tls-webpki-roots = ["dep:webpki-roots", "__rustls"] rustls-tls-native-roots = ["dep:rustls-native-certs", "__rustls", "hyper-rustls/rustls-native-certs"]

  1. No TLS support (default-features = false)
  2. TLS support via native-tls to link against the operating system's native TLS implementation (default-features = false, features = ["native-tls"])
  3. TLS support via rustls using native certificates (default).
  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-tls-webpki-roots"])

Credits

This was forked from https://github.com/siketyan/hyper-http-proxy that originally forked from https://github.com/tafia/hyper-proxy

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

ID Version
bytes ^1.5
futures-util ^0.3
headers ^0.4
http ^1
hyper ^1
hyper-rustls ^0.27
hyper-tls ^0.6
hyper-util ^0.1
native-tls ^0.2
pin-project-lite ^0.2
rustls-native-certs ^0.7
tokio ^1.35
tokio-native-tls ^0.3
tokio-rustls ^0.26
tower-service ^0.3
webpki-roots ^0.26
futures ^0.3
http-body-util ^0.1
hyper ^1.0
hyper-util ^0.1
tokio ^1.35

Keywords

hyper proxy tokio ssl
Details
Cargo
2026-03-26 10:59:08 +00:00
0
MetalBear Tech LTD <hi@metalbear.co>
MIT
18 KiB
Assets (1)
Versions (1) View all
1.1.0 2026-03-26