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

hyper-proxy2

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

1 个不稳定版本

0.1.0 2024 年 1 月 14 日

#303HTTP 客户端

Download history 24/week @ 2024-04-23 30/week @ 2024-04-30 2/week @ 2024-05-07 21/week @ 2024-05-14 120/week @ 2024-05-21 33/week @ 2024-05-28 107/week @ 2024-06-04 166/week @ 2024-06-11 153/week @ 2024-06-18 148/week @ 2024-06-25 203/week @ 2024-07-02 144/week @ 2024-07-09 347/week @ 2024-07-16 353/week @ 2024-07-23 407/week @ 2024-07-30 421/week @ 2024-08-06

1,541 每月下载量
用于 slack-morphism

MIT 许可证

41KB
795

hyper-proxy2

Travis Build Status 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_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(())
}

功能

hyper-proxy2 提供了三个主要 Cargo 功能,以配置它连接到代理时使用的 TLS 实现。它还可以在不支持 TLS 的情况下配置,通过完全不编译默认功能来实现。支持配置列表如下

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

致谢

代码的大部分来自 reqwest。核心部分刚刚提取并略有增强。

主要变化包括

  • 支持身份验证
  • 添加非安全隧道
  • 添加在连接到代理时添加额外头部的可能性

依赖项

~6–19MB
~269K SLoC