1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::error::Error;
use hyper::client::HttpConnector;
use hyper_proxy::{Proxy, ProxyConnector};
use hyper_tls::HttpsConnector;
use crate::proxy::PROXY_CONFIG;
pub type Connector = ProxyConnector<HttpsConnector<HttpConnector>>;
pub fn connector() -> Result<Connector, Box<dyn Error + Send + Sync>> {
let mut connector = ProxyConnector::new(HttpsConnector::new())?;
if let Some(http_proxy) = PROXY_CONFIG.http_proxy() {
let matches = move |scheme: Option<&str>, host: Option<&str>, port| {
scheme == Some("http") && !PROXY_CONFIG.exclude(scheme, host, port)
};
connector.add_proxy(Proxy::new(matches, http_proxy.clone()));
}
if let Some(https_proxy) = PROXY_CONFIG.https_proxy() {
let matches = move |scheme: Option<&str>, host: Option<&str>, port| {
scheme == Some("https") && !PROXY_CONFIG.exclude(scheme, host, port)
};
connector.add_proxy(Proxy::new(matches, https_proxy.clone()));
}
if let Some(all_proxy) = PROXY_CONFIG.all_proxy() {
let matches = move |scheme: Option<&str>, host: Option<&str>, port| {
!PROXY_CONFIG.exclude(scheme, host, port)
};
connector.add_proxy(Proxy::new(matches, all_proxy.clone()));
}
Ok(connector)
}