2017-01-12 9 views
3

Следующая попытка достичь сайт HTTPS через прокси-сервер:Как добраться до HTTPS-сайта через прокси-сервер с помощью Hyper?

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` 

Вот являются грузовые зависимости:

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

Нет сборки неудачи там, но он не идет через прокси-сервер.

+0

Не все эти ошибки вызваны тем фактом, что вы пытаетесь напечатать «ответ» в режиме отладки, в то время как он не реализует «Debug»? – ljedrz

+0

Я получаю ту же ошибку, даже если я ничего не печатаю – Tshepang

+1

Ни одна из структур на ['hyper_native_tls'] (https://docs.rs/hyper-native-tls/0.2.0/hyper_native_tls/) не выглядит 'Debug', поэтому поведение, наблюдаемое на этом коде, будет правильным. Убедитесь, что вы не выполняете 'fmt()' и обновляете вопрос своими выводами. –

ответ

4

Были некоторые непроверенные конфликты вокруг ящиков hyper_native_tls и native_tls.

В настоящее время существует ограничение на реализацию SslClient для 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, где S: Debug, и в соответствии с признаком Error типы ошибок должны всегда реализовывать Debug.

Исправление к этому конкретному вопросу заключается в предоставлении Debug в 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, объясняя, что он не работает с прокси-сервером гиперссылки (Редактировать: it's done and fixed!).

+0

https://github.com/sfackler/hyper-native-tls/commit/30e9d1574774aa9267ca8d4770f9af21f04edb13 – Tshepang

+0

@Tshepang Хорошая работа. ;) –

 Смежные вопросы

  • Нет связанных вопросов^_^