如何使用 Hyper 通过代理访问 HTTPS 站点?

2023-11-29

以下是通过代理访问 HTTPS 站点的尝试:

extern crate hyper;
extern crate hyper_native_tls;

use hyper::net::HttpsConnector;
use hyper::client::{Client, ProxyConfig};
use hyper_native_tls::NativeTlsClient;

fn main() {
    let ssl = NativeTlsClient::new().unwrap();
    let connector = HttpsConnector::new(ssl);

    let client = Client::with_proxy_config(
        ProxyConfig::new(
            "http", "localhost", 3128, connector, ssl
        )
    );

    let response = client.get("https://httpbin.org").send().unwrap();
    println!("{}", response.headers);
}

我收到此错误:

error[E0277]: the trait bound `hyper_native_tls::TlsStream<hyper::net::HttpStream>: std::fmt::Debug` is not satisfied
  --> src/main.rs:13:9
   |
13 |         ProxyConfig::new(
   |         ^^^^^^^^^^^^^^^^ the trait `std::fmt::Debug` is not implemented for `hyper_native_tls::TlsStream<hyper::net::HttpStream>`
   |
   = note: `hyper_native_tls::TlsStream<hyper::net::HttpStream>` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `hyper::net::HttpsStream<hyper_native_tls::TlsStream<hyper::net::HttpStream>>`
   = note: required because of the requirements on the impl of `hyper::net::SslClient<hyper::net::HttpsStream<hyper_native_tls::TlsStream<hyper::net::HttpStream>>>` for `hyper_native_tls::NativeTlsClient`
   = note: required by `<hyper::client::ProxyConfig<C, S>>::new`

error[E0277]: the trait bound `hyper_native_tls::TlsStream<hyper::net::HttpStream>: std::fmt::Debug` is not satisfied
  --> src/main.rs:13:9
   |
13 |           ProxyConfig::new(
   |  _________^ starting here...
14 | |             "http", "localhost", 3128, connector, ssl
15 | |         )
   | |_________^ ...ending here: the trait `std::fmt::Debug` is not implemented for `hyper_native_tls::TlsStream<hyper::net::HttpStream>`
   |
   = note: `hyper_native_tls::TlsStream<hyper::net::HttpStream>` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `hyper::net::HttpsStream<hyper_native_tls::TlsStream<hyper::net::HttpStream>>`
   = note: required because of the requirements on the impl of `hyper::net::SslClient<hyper::net::HttpsStream<hyper_native_tls::TlsStream<hyper::net::HttpStream>>>` for `hyper_native_tls::NativeTlsClient`
   = note: required by `hyper::client::ProxyConfig`

error[E0277]: the trait bound `hyper_native_tls::TlsStream<hyper::net::HttpStream>: std::fmt::Debug` is not satisfied
  --> src/main.rs:12:18
   |
12 |     let client = Client::with_proxy_config(
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::fmt::Debug` is not implemented for `hyper_native_tls::TlsStream<hyper::net::HttpStream>`
   |
   = note: `hyper_native_tls::TlsStream<hyper::net::HttpStream>` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `hyper::net::HttpsStream<hyper_native_tls::TlsStream<hyper::net::HttpStream>>`
   = note: required because of the requirements on the impl of `hyper::net::SslClient<hyper::net::HttpsStream<hyper_native_tls::TlsStream<hyper::net::HttpStream>>>` for `hyper_native_tls::NativeTlsClient`
   = note: required by `hyper::Client::with_proxy_config`

以下是 Cargo 依赖项:

[dependencies]
hyper = "0.10"
hyper-native-tls = "0.2"

使用这些依赖关系会更好:

[dependencies]
hyper = "0.10"
hyper-openssl = "0.2"

这段代码:

extern crate hyper;
extern crate hyper_openssl;

use hyper::net::HttpsConnector;
use hyper::client::{Client, ProxyConfig};
use hyper_openssl::OpensslClient as TlsClient;

fn main() {
    let ssl = TlsClient::new().unwrap();
    let connector = HttpsConnector::new(ssl.clone());

    let client = Client::with_proxy_config(
        ProxyConfig::new(
            "http", "localhost", 3128, connector, ssl
        )
    );

    let response = client.get("https://httpbin.org").send().unwrap();
    println!("{:#?}", response);
}

Output:

Response {
    status: Ok,
    headers: Headers { Server: nginx, Date: Thu, 12 Jan 2017 15:05:13 GMT, Content-Type: text/html; charset=utf-8, Content-Length: 12150, Connection: keep-alive, Access-Control-Allow-Origin: *, Access-Control-Allow-Credentials: true, },
    version: Http11,
    url: "https://httpbin.org/",
    status_raw: RawStatus(
        200,
        "OK"
    ),
    message: Http11Message {
        is_proxied: false,
        method: None,
        stream: Wrapper {
            obj: Some(
                Reading(
                    SizedReader(remaining=12150)
                )
            )
        }
    }
}

那里没有构建失败,但它不通过代理。


板条箱周围存在一些未经测试的冲突hyper_native_tls and native_tls.

目前,实施有限制SslClient for NativeTlsClient这需要T: Debug (code)。问题中的代码无法编译,因为TlsStream无论其参数类型如何,都不会实现 Debug。

首先可以考虑消除上述限制。但这会引发一些其他错误hyper_native_tls:

error[E0277]: the trait bound `T: std::fmt::Debug` is not satisfied
   --> src/lib.rs:129:45
    |
129 |             Err(e) => Err(hyper::Error::Ssl(Box::new(e))),
    |                                             ^^^^^^^^^^^ the trait `std::fmt::Debug` is not implemented for `T`
    |
    = help: consider adding a `where T: std::fmt::Debug` bound
    = note: required because of the requirements on the impl of `std::error::Error` for `native_tls::HandshakeError<T>`
    = note: required for the cast to the object type `std::error::Error + std::marker::Sync + std::marker::Send + 'static`

深入兔子洞,我们发现native_tls::HandshakeError保存参数类型S被中断的流(如果发生此特定错误)。这成为另一个问题,因为该类型只实现Debug where S: Debug,并根据Error特征,错误类型必须始终实现Debug.

解决此特定问题的方法是提供Debug to TlsStream:

#[derive(Debug, Clone)]
pub struct TlsStream<S>(Arc<Mutex<native_tls::TlsStream<S>>>);

第一个代码片段仍然无法编译,因为ssl是搬家后使用的,这里不允许复制。第二个片段通过克隆对象来工作,不幸的是,它没有实现NativeTlsClient。我们也无法推导出实现,因为native_tls::TlsConnector不实施Clone任何一个。就这个兔子洞而言,在成为调试报告之前它可能应该在这里结束。

我不完全确定在这里可以做什么(除了根本不使用本机 TLS),但我当前的建议是在hyper_native_tls_client,解释说它不适用于 hyper 的客户端代理(编辑:已经完成并修复了!).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Hyper 通过代理访问 HTTPS 站点? 的相关文章

随机推荐

  • 来自 bash shell 的 XMLA/SOAP 命令

    我需要从 bash shell 中调用 icCube 的管理 API 发送 SOAP 命令的最简单方法是什么
  • url 检查器 VBA,重定向时显示重定向的 url

    我对 EXCEL VBA 很陌生 我有点困于寻找一种方法来创建一个宏来显示 url 是否仍然处于活动状态 200 ok 或者可能会被重定向 如果是这样 我想知道到什么 URL 当它根本不起作用时 则返回正确的代码以及 URL 不起作用的原因
  • 使用 VBA 删除数字周围的文本

    我需要从单元格内的数字中删除所有文本 然后将两个数字拆分到两个单元格中 并将格式设置为数字 而不是文本 该单元格包含以下格式的文本 数字 between 150 000 and 159 999 per annum between 60 an
  • 如何在SQL Server中将XML数据转换为行列数据

    我有一个要求 数据库中有 XML 数据 我需要从数据库中以行和列的形式提取这些数据 我的 XML 就像
  • Hibernate Native SQL 映射到带有选择示例的实体

    我尝试创建本机 SQL 查询并使用参数映射到实体类 但失败了 return public List
  • iPhone 中的 HttpBasicAuthentication

    在我的 iPhone 应用程序中 我试图在 iPhone 上显示来自我的服务器的图像 这需要授权 我正在尝试使用 NSURLConnection 来获取图像 但它并没有要求我提供用户凭据 即它根本不会使用 didReceiveAuthent
  • 用于在 R 中访问 Cassandra 数据库的包

    我努力了R卡桑德拉 and RJDBC但不幸的是 这些绑定似乎仅适用于旧的 Cassandra 1 x Cassandra 2 x 中是否有任何绑定R语言 这不是真的 当前版本RJDBC与 Cassandra 2 X 配合使用 下载具有 C
  • 如何将用户输入限制为 DataGridView 中的几个值

    我需要想出一种方法来限制用户输入除1 or 2在我的 dgv 的专栏中 我有它 因此用户只能输入数字 但我需要进一步的限制来满足FOREIGN KEY限制 有任何想法吗 当前验证到位 Private Sub dgvCategories Ed
  • 在 R Shiny 的数据表中添加工具提示

    我正在尝试使用 JS 脚本添加工具提示attr函数 一些编写的脚本没有给出所需的结果 为数据表的第一列添加工具提示 由于我是新的 JS 脚本 因此无法调试错误 任何人都可以建议我为什么下面的代码没有给出我正确的结果 这是一段代码 libra
  • 如何用手指移动 OpenGL 方块?

    其实我有一个申请安卓1 5使用 GLSurfaceView 类在屏幕上显示一个简单的正方形 我想学习如何添加新功能 即移动用手指触摸的方块的功能 我的意思是 当用户触摸方块并移动手指时 方块应该粘在手指上 直到手指释放屏幕 任何教程 代码示
  • Angular 2 验证与子组件一起

    在父组件内我有一个子组件 两者都有其必填字段 最后 仅当两个组件都有效时 我才需要启用提交按钮 在这种情况下 所有必填字段均已填写 我怎样才能做到这一点 特别是使用模板驱动的验证 种子代码 父组件 Component selector pa
  • Internet Explorer 11 RGBA 拒绝工作

    我正在使用 IE 11 它拒绝使 rgba 在我的页面上工作 而 rgba 在某些网站上工作 我不明白为什么 My page note the rgba underlined with red 一些互联网页面 rgba 工作正常 显然 IE
  • 如何在 iOS 上的 Swift 中发布 CGDataProvider?

    我在 Swift 中有以下代码 func cropToAlpha image CGImage gt CGImage let pixelDataProvider CGDataProvider CGImageGetDataProvider im
  • 如何将服务器端 Jax-rs 调用与没有前缀的本机文件混合?

    我们目前正在使用 Jersey JAX RS 实现来处理 REST 请求 服务器端 Jersey 我们的 web xml 文件已配置为所有 rest 请求均由 Jersey 处理 没关系 我们的服务器目前是Tomcat6 并使用Java6
  • 在catalog.xml中为类别视图设置“列表/网格”默认视图模式

    我正在尝试让我的类别视图默认以列表或网格模式显示产品
  • 如何在android中的对话框中加载webview

    我正在尝试将网络视图加载到对话框中 我正在使用以下代码 final TextView seeMonthlyBill TextView parentLayout findViewById R id my ac my service timew
  • 如何从特定的IP地址发送电子邮件?

    我正在使用 cPanel CentOS 运行 vps 我想要动态地从 php 代码中选择要发送电子邮件的 IP 地址 我对任何奇怪的方式持开放态度 有什么办法可以做到这一点吗 我真的很感激一些清晰的想法 因为我不太擅长进出口和其他东西 附
  • Cckeditor 如何将键盘快捷键应用于特定样式

    是否可以将键盘快捷键应用于 CkEditor 中样式下拉列表中的特定样式 我搜索了他们的文档 但找不到适合我的案例的解决方案 这是屏幕截图 在这里 我希望能够将键盘快捷键应用到 样式 下拉列表中的黄色标记 任何帮助将不胜感激 谢谢 干得好
  • Azure Active Directory 和 WCF 身份验证

    我有 WCF 服务 我需要使用 Azure Active Directory 来保护它 我已经阅读了此处和social msdn 上的所有相关问题 但仍然无法让我的示例正常工作 我希望身份验证以以下方式工作 当用户从客户端调用 WCF 服务
  • 如何使用 Hyper 通过代理访问 HTTPS 站点?

    以下是通过代理访问 HTTPS 站点的尝试 extern crate hyper extern crate hyper native tls use hyper net HttpsConnector use hyper client Cli