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

Rust 如何使用Hyper通过代理访问HTTPS站点?,rust,http-proxy,hyper,Rust,Http Proxy,Hyper,以下是通过代理访问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();

以下是通过代理访问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]:未满足特性绑定的'hyper\u native\u tls::TlsStream:std::fmt::Debug'
-->src/main.rs:13:9
|
13 | ProxyConfig::新建(
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^未为“hyper_native_tls::TlsStream”实现特性“std::fmt::Debug”`
|
=注意:`hyper_native_tls::TlsStream`不能使用“:?”进行格式化;如果它是在您的板条箱中定义的,请添加“#[派生(调试)]”或手动实现它
=注意:由于对'hyper::net::HttpsStream'的'std::fmt::Debug'impl的要求,因此需要`
=注意:由于对'hyper_native_tls::NativeTlsClient'的'hyper::net::SslClient'impl的要求,因此需要此选项`
=注意:`::new所需`
错误[E0277]:未满足特性绑定的'hyper_native_tls::TlsStream:std::fmt::Debug'
-->src/main.rs:13:9
|
13 | ProxyConfig::新建(
|从这里开始。。。
14 | | |“http”、“本地主机”、3128、连接器、ssl
15 | |         )
|| _________________________`
|
=注意:`hyper_native_tls::TlsStream`不能使用“:?”进行格式化;如果它是在您的板条箱中定义的,请添加“#[派生(调试)]”或手动实现它
=注意:由于对'hyper::net::HttpsStream'的'std::fmt::Debug'impl的要求,因此需要`
=注意:由于对'hyper_native_tls::NativeTlsClient'的'hyper::net::SslClient'impl的要求,因此需要此选项`
=注意:`hyper::client::ProxyConfig'需要`
错误[E0277]:未满足特性绑定的'hyper_native_tls::TlsStream:std::fmt::Debug'
-->src/main.rs:12:18
|
12 |让client=client::with_proxy_config(
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^未为` hyper _native_tls::tlsstre`
|
=注意:`hyper_native_tls::TlsStream`不能使用“:?”进行格式化;如果它是在您的板条箱中定义的,请添加“#[派生(调试)]”或手动实现它
=注意:由于对'hyper::net::HttpsStream'的'std::fmt::Debug'impl的要求,因此需要`
=注意:由于对'hyper_native_tls::NativeTlsClient'的'hyper::net::SslClient'impl的要求,因此需要此选项`
=注意:“hyper::Client::with_proxy_config”需要`
以下是货物依赖项:

[依赖项]
hyper=“0.10”
超本机tls=“0.2”
使用这些依赖项会更好:

[依赖项]
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);
}
输出:

响应{
状态:好,
headers:headers{Server:nginx,Date:Thu,2017年1月12日15:05:13 GMT,内容类型:text/html;charset=utf-8,内容长度:12150,连接:保持活动,访问控制允许来源:*,访问控制允许凭据:true,},
版本:Http11,
url:“https://httpbin.org/",
原始状态:原始状态(
200,
“好的”
),
消息:http11消息{
是否代理:false,
方法:无,
流:包装器{
obj:一些(
阅读(
SizedReader(剩余=12150)
)
)
}
}
}

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

板条箱周围有一些未经测试的冲突
hyper\u native\u tls
native\u tls

目前,对需要
T:Debug
()的
NativeTlsClient
SslClient
的实现有一个限制。问题中的代码没有编译,因为
TlsStream
没有实现Debug,无论其参数类型如何

首先可以考虑删除前面提到的约束,但是在HyLuthNaveTys:

中触发了其他几个错误。
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`
第一个代码段仍然无法编译,因为移动后正在使用
ssl
,此处不允许复制。第二个代码段通过克隆对象来工作,不幸的是没有为
NativeTlsClient
实现。我们也无法派生实现,因为
native_tls::TlsConnector
没有t实现
Clone
。就这个兔子洞而言,在它成为调试报告之前,它应该在这里结束


我不完全确定在这里可以做什么(除了完全不使用本机TLS之外),但我目前的建议是在
hyper\u National\u TLS\u client
中提交一个问题,解释它不适用于hyper的客户端代理(编辑:).

所有这些错误都不是由于您试图在调试模式下打印
响应而导致的,而它没有实现
debug
?即使我没有打印任何内容,我也会收到相同的错误。上的任何结构似乎都没有执行
debug
,因此在该代码上看到的行为是正确的。请确保您没有执行任何
fmt()
,并用您的发现更新问题。@E_net4:可能是答案。我检查了,并且都不需要
Debug
,因此OP似乎在自责……错误消息不是很好。或者他没有使用最新版本的hyper(0.10.0)因此,我正在查阅的文档并不好。@MatthieuM。从某种程度上说,这是真的。另一方面,这个问题的质量很差,根本不涉及使用
Debug
-格式的输出。我宁愿让它改进或删除。我还想知道是否有合适的副本。@Tshepang干得好。;)
#[derive(Debug, Clone)]
pub struct TlsStream<S>(Arc<Mutex<native_tls::TlsStream<S>>>);