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

hyper-http-proxy

基于Hyper的应用程序代理连接器

1个稳定版本

1.0.0 2024年5月26日

#119HTTP客户端

Download history 287/week @ 2024-05-23 124/week @ 2024-05-30 538/week @ 2024-06-06 2198/week @ 2024-06-13 6783/week @ 2024-06-20 7256/week @ 2024-06-27 7907/week @ 2024-07-04 13137/week @ 2024-07-11 14275/week @ 2024-07-18 12896/week @ 2024-07-25 18930/week @ 2024-08-01

61,243 每月下载次数
5 个crate中使用 (via kube-client)

MIT 许可证

40KB
755 代码行

hyper-http-proxy

Checks MIT licensed crates.io

基于hyper的应用的代理连接器。

文档

示例

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(())
}

功能

hyper-http-proxy暴露Cargo功能,以配置它连接到代理时使用的TLS实现。它也可以通过完全不编译默认功能来配置,从而不提供TLS支持。支持配置列表如下

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. 不支持TLS (default-features = false)
  2. 通过native-tls支持TLS,以链接到操作系统的原生TLS实现 (default-features = false, features = ["native-tls"])
  3. 通过rustls使用原生证书支持TLS(默认)。
  4. 通过 rustls 支持TLS,使用静态编译的CA证书集来绕过操作系统的默认存储(default-features = false, features = ["rustls-tls-webpki-roots"]

致谢

本项目是从 https://github.com/siketyan/hyper-http-proxy 分支出来的,而该分支最初是从 https://github.com/tafia/hyper-proxy 分支出来的

代码的大部分来自 reqwest。核心部分已经被提取并略作增强。主要变更包括:

  • 支持身份验证
  • 增加非加密隧道
  • 在连接到代理时可以添加额外的头部信息

依赖项

~6–19MB
~278K SLoC