【问题标题】:How to reach an HTTPS site via proxy with Hyper?如何通过 Hyper 代理访问 HTTPS 站点?
【发布时间】:2017-05-26 12:13:57
【问题描述】:

以下是通过代理访问 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);
}

输出:

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

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

【问题讨论】:

  • 难道所有这些错误都是由于您尝试在调试模式下打印response 而它没有实现Debug 而导致的吗?
  • 即使我不打印任何东西也会出现同样的错误
  • hyper_native_tls 上的所有结构似乎都没有实现 Debug,因此在该代码上看到的行为是正确的。请确保您没有执行任何fmt() 并根据您的发现更新问题。
  • @E_net4:应该是一个答案。我检查了ProxyConfigClient 并且都不需要Debug,所以看起来OP 正在向自己开枪……而且错误信息不是很好。或者他没有使用最新版本的 hyper (0.10.0),因此我咨询的文档不好。
  • @MatthieuM。在某种程度上,这是真的。另一方面,这个问题质量很差,根本没有提到在其措辞中使用Debug-formatted 输出。我宁愿让它改进或删除。我也想知道是否有合适的副本。

标签: rust http-proxy hyper


【解决方案1】:

在箱子hyper_native_tlsnative_tls 周围存在一些未经测试的冲突。

目前,对NativeTlsClientSslClient 的实现有一个限制,需要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,其中S: Debug,并且根据Error trait,错误类型必须始终实现Debug

解决此特定问题的方法是将Debug 提供给TlsStream

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

第一个代码sn -p 还是编译不了,因为ssl 移动后正在使用,这里不允许复制。第二个 sn-p 通过克隆对象来工作,不幸的是,NativeTlsClient 没有实现。我们也无法派生实现,因为native_tls::TlsConnector 也没有实现Clone。就这个兔子洞而言,它可能应该在这成为调试报告之前就结束了。

我不完全确定这里可以做什么(除了根本不使用本机 TLS),但我目前的建议是在 hyper_native_tls_client 中提出问题,解释它不适用于 hyper 的客户端代理(编辑:it's done and fixed!)。

【讨论】:

猜你喜欢
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2020-06-18
  • 2016-06-08
  • 1970-01-01
  • 2011-06-12
相关资源
最近更新 更多