committed by
Sean McArthur
parent
81e0f1ff2a
commit
cf8944a0f0
123
src/proxy.rs
123
src/proxy.rs
@@ -1,16 +1,16 @@
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
#[cfg(feature = "socks")]
|
||||
use std::net::{SocketAddr, ToSocketAddrs};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{IntoUrl, Url};
|
||||
use http::{header::HeaderValue, Uri};
|
||||
use hyper::client::connect::Destination;
|
||||
use url::percent_encoding::percent_decode;
|
||||
use crate::{IntoUrl, Url};
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
#[cfg(target_os = "windows")]
|
||||
use std::error::Error;
|
||||
use url::percent_encoding::percent_decode;
|
||||
#[cfg(target_os = "windows")]
|
||||
use winreg::enums::HKEY_CURRENT_USER;
|
||||
#[cfg(target_os = "windows")]
|
||||
@@ -98,7 +98,7 @@ impl Proxy {
|
||||
/// ```
|
||||
pub fn http<U: IntoProxyScheme>(proxy_scheme: U) -> crate::Result<Proxy> {
|
||||
Ok(Proxy::new(Intercept::Http(
|
||||
proxy_scheme.into_proxy_scheme()?
|
||||
proxy_scheme.into_proxy_scheme()?,
|
||||
)))
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ impl Proxy {
|
||||
/// ```
|
||||
pub fn https<U: IntoProxyScheme>(proxy_scheme: U) -> crate::Result<Proxy> {
|
||||
Ok(Proxy::new(Intercept::Https(
|
||||
proxy_scheme.into_proxy_scheme()?
|
||||
proxy_scheme.into_proxy_scheme()?,
|
||||
)))
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ impl Proxy {
|
||||
/// ```
|
||||
pub fn all<U: IntoProxyScheme>(proxy_scheme: U) -> crate::Result<Proxy> {
|
||||
Ok(Proxy::new(Intercept::All(
|
||||
proxy_scheme.into_proxy_scheme()?
|
||||
proxy_scheme.into_proxy_scheme()?,
|
||||
)))
|
||||
}
|
||||
|
||||
@@ -163,12 +163,12 @@ impl Proxy {
|
||||
/// # }
|
||||
/// # fn main() {}
|
||||
pub fn custom<F, U: IntoProxyScheme>(fun: F) -> Proxy
|
||||
where F: Fn(&Url) -> Option<U> + Send + Sync + 'static {
|
||||
where
|
||||
F: Fn(&Url) -> Option<U> + Send + Sync + 'static,
|
||||
{
|
||||
Proxy::new(Intercept::Custom(Custom {
|
||||
auth: None,
|
||||
func: Arc::new(move |url| {
|
||||
fun(url).map(IntoProxyScheme::into_proxy_scheme)
|
||||
}),
|
||||
func: Arc::new(move |url| fun(url).map(IntoProxyScheme::into_proxy_scheme)),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -179,9 +179,7 @@ impl Proxy {
|
||||
*/
|
||||
|
||||
fn new(intercept: Intercept) -> Proxy {
|
||||
Proxy {
|
||||
intercept,
|
||||
}
|
||||
Proxy { intercept }
|
||||
}
|
||||
|
||||
/// Set the `Proxy-Authorization` header using Basic auth.
|
||||
@@ -214,15 +212,13 @@ impl Proxy {
|
||||
|
||||
pub(crate) fn http_basic_auth<D: Dst>(&self, uri: &D) -> Option<HeaderValue> {
|
||||
match self.intercept {
|
||||
Intercept::All(ProxyScheme::Http { ref auth, .. }) |
|
||||
Intercept::Http(ProxyScheme::Http { ref auth, .. }) => auth.clone(),
|
||||
Intercept::Custom(ref custom) => {
|
||||
custom.call(uri).and_then(|scheme| match scheme {
|
||||
ProxyScheme::Http { auth, .. } => auth,
|
||||
#[cfg(feature = "socks")]
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
Intercept::All(ProxyScheme::Http { ref auth, .. })
|
||||
| Intercept::Http(ProxyScheme::Http { ref auth, .. }) => auth.clone(),
|
||||
Intercept::Custom(ref custom) => custom.call(uri).and_then(|scheme| match scheme {
|
||||
ProxyScheme::Http { auth, .. } => auth,
|
||||
#[cfg(feature = "socks")]
|
||||
_ => None,
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -236,14 +232,14 @@ impl Proxy {
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
Intercept::Https(ref u) => {
|
||||
if uri.scheme() == "https" {
|
||||
Some(u.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
Intercept::Custom(ref custom) => custom.call(uri),
|
||||
}
|
||||
}
|
||||
@@ -251,12 +247,8 @@ impl Proxy {
|
||||
pub(crate) fn is_match<D: Dst>(&self, uri: &D) -> bool {
|
||||
match self.intercept {
|
||||
Intercept::All(_) => true,
|
||||
Intercept::Http(_) => {
|
||||
uri.scheme() == "http"
|
||||
},
|
||||
Intercept::Https(_) => {
|
||||
uri.scheme() == "https"
|
||||
},
|
||||
Intercept::Http(_) => uri.scheme() == "http",
|
||||
Intercept::Https(_) => uri.scheme() == "https",
|
||||
Intercept::Custom(ref custom) => custom.call(uri).is_some(),
|
||||
}
|
||||
}
|
||||
@@ -304,7 +296,11 @@ impl ProxyScheme {
|
||||
}
|
||||
|
||||
/// Use a username and password when connecting to the proxy server
|
||||
fn with_basic_auth<T: Into<String>, U: Into<String>>(mut self, username: T, password: U) -> Self {
|
||||
fn with_basic_auth<T: Into<String>, U: Into<String>>(
|
||||
mut self,
|
||||
username: T,
|
||||
password: U,
|
||||
) -> Self {
|
||||
self.set_basic_auth(username, password);
|
||||
self
|
||||
}
|
||||
@@ -314,7 +310,7 @@ impl ProxyScheme {
|
||||
ProxyScheme::Http { ref mut auth, .. } => {
|
||||
let header = encode_basic_auth(&username.into(), &password.into());
|
||||
*auth = Some(header);
|
||||
},
|
||||
}
|
||||
#[cfg(feature = "socks")]
|
||||
ProxyScheme::Socks5 { ref mut auth, .. } => {
|
||||
*auth = Some((username.into(), password.into()));
|
||||
@@ -332,12 +328,10 @@ impl ProxyScheme {
|
||||
let to_addr = || {
|
||||
let host_and_port = try_!(url.with_default_port(|url| match url.scheme() {
|
||||
"socks5" | "socks5h" => Ok(1080),
|
||||
_ => Err(())
|
||||
_ => Err(()),
|
||||
}));
|
||||
let mut addr = try_!(host_and_port.to_socket_addrs());
|
||||
addr
|
||||
.next()
|
||||
.ok_or_else(crate::error::unknown_proxy_scheme)
|
||||
addr.next().ok_or_else(crate::error::unknown_proxy_scheme)
|
||||
};
|
||||
|
||||
let mut scheme = match url.scheme() {
|
||||
@@ -346,7 +340,7 @@ impl ProxyScheme {
|
||||
"socks5" => Self::socks5(to_addr()?)?,
|
||||
#[cfg(feature = "socks")]
|
||||
"socks5h" => Self::socks5h(to_addr()?)?,
|
||||
_ => return Err(crate::error::unknown_proxy_scheme())
|
||||
_ => return Err(crate::error::unknown_proxy_scheme()),
|
||||
};
|
||||
|
||||
if let Some(pwd) = url.password() {
|
||||
@@ -359,8 +353,6 @@ impl ProxyScheme {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Intercept {
|
||||
All(ProxyScheme),
|
||||
@@ -372,9 +364,9 @@ enum Intercept {
|
||||
impl Intercept {
|
||||
fn set_basic_auth(&mut self, username: &str, password: &str) {
|
||||
match self {
|
||||
Intercept::All(ref mut s) |
|
||||
Intercept::Http(ref mut s) |
|
||||
Intercept::Https(ref mut s) => s.set_basic_auth(username, password),
|
||||
Intercept::All(ref mut s)
|
||||
| Intercept::Http(ref mut s)
|
||||
| Intercept::Https(ref mut s) => s.set_basic_auth(username, password),
|
||||
Intercept::Custom(ref mut custom) => {
|
||||
let header = encode_basic_auth(username, password);
|
||||
custom.auth = Some(header);
|
||||
@@ -397,9 +389,10 @@ impl Custom {
|
||||
uri.scheme(),
|
||||
uri.host(),
|
||||
uri.port().map(|_| ":").unwrap_or(""),
|
||||
uri.port().map(|p| p.to_string()).unwrap_or_default())
|
||||
.parse()
|
||||
.expect("should be valid Url");
|
||||
uri.port().map(|p| p.to_string()).unwrap_or_default()
|
||||
)
|
||||
.parse()
|
||||
.expect("should be valid Url");
|
||||
|
||||
(self.func)(&url)
|
||||
.and_then(|result| result.ok())
|
||||
@@ -413,7 +406,7 @@ impl Custom {
|
||||
uri,
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
#[cfg(feature = "socks")]
|
||||
socks => socks,
|
||||
})
|
||||
@@ -467,8 +460,7 @@ impl Dst for Uri {
|
||||
}
|
||||
|
||||
fn host(&self) -> &str {
|
||||
Uri::host(self)
|
||||
.expect("<Uri as Dst>::host should have a str")
|
||||
Uri::host(self).expect("<Uri as Dst>::host should have a str")
|
||||
}
|
||||
|
||||
fn port(&self) -> Option<u16> {
|
||||
@@ -499,8 +491,7 @@ pub fn get_proxies() -> HashMap<String, Url> {
|
||||
proxies
|
||||
}
|
||||
|
||||
fn insert_proxy(proxies: &mut HashMap<String, Url>, schema: String, addr: String)
|
||||
{
|
||||
fn insert_proxy(proxies: &mut HashMap<String, Url>, schema: String, addr: String) {
|
||||
if let Ok(valid_addr) = Url::parse(&addr) {
|
||||
proxies.insert(schema, valid_addr);
|
||||
}
|
||||
@@ -522,7 +513,6 @@ fn get_from_environment() -> HashMap<String, Url> {
|
||||
proxies
|
||||
}
|
||||
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
fn get_from_registry_impl() -> Result<HashMap<String, Url>, Box<dyn Error>> {
|
||||
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
||||
@@ -543,7 +533,11 @@ fn get_from_registry_impl() -> Result<HashMap<String, Url>, Box<dyn Error>> {
|
||||
let protocol_parts: Vec<&str> = p.split("=").collect();
|
||||
match protocol_parts.as_slice() {
|
||||
[protocol, address] => {
|
||||
insert_proxy(&mut proxies, String::from(*protocol), String::from(*address));
|
||||
insert_proxy(
|
||||
&mut proxies,
|
||||
String::from(*protocol),
|
||||
String::from(*address),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
// Contains invalid protocol setting, just break the loop
|
||||
@@ -558,9 +552,21 @@ fn get_from_registry_impl() -> Result<HashMap<String, Url>, Box<dyn Error>> {
|
||||
if proxy_server.starts_with("http:") {
|
||||
insert_proxy(&mut proxies, String::from("http"), proxy_server);
|
||||
} else {
|
||||
insert_proxy(&mut proxies, String::from("http"), format!("http://{}", proxy_server));
|
||||
insert_proxy(&mut proxies, String::from("https"), format!("https://{}", proxy_server));
|
||||
insert_proxy(&mut proxies, String::from("ftp"), format!("https://{}", proxy_server));
|
||||
insert_proxy(
|
||||
&mut proxies,
|
||||
String::from("http"),
|
||||
format!("http://{}", proxy_server),
|
||||
);
|
||||
insert_proxy(
|
||||
&mut proxies,
|
||||
String::from("https"),
|
||||
format!("https://{}", proxy_server),
|
||||
);
|
||||
insert_proxy(
|
||||
&mut proxies,
|
||||
String::from("ftp"),
|
||||
format!("https://{}", proxy_server),
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(proxies)
|
||||
@@ -581,8 +587,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn host(&self) -> &str {
|
||||
Url::host_str(self)
|
||||
.expect("<Url as Dst>::host should have a str")
|
||||
Url::host_str(self).expect("<Url as Dst>::host should have a str")
|
||||
}
|
||||
|
||||
fn port(&self) -> Option<u16> {
|
||||
@@ -594,7 +599,6 @@ mod tests {
|
||||
s.parse().unwrap()
|
||||
}
|
||||
|
||||
|
||||
fn intercepted_uri(p: &Proxy, s: &str) -> Uri {
|
||||
match p.intercept(&url(s)).unwrap() {
|
||||
ProxyScheme::Http { uri, .. } => uri,
|
||||
@@ -641,7 +645,6 @@ mod tests {
|
||||
assert_eq!(intercepted_uri(&p, other), target);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_custom() {
|
||||
let target1 = "http://example.domain/";
|
||||
@@ -687,7 +690,7 @@ mod tests {
|
||||
// reset user setting.
|
||||
match system_proxy {
|
||||
Err(_) => env::remove_var("http_proxy"),
|
||||
Ok(proxy) => env::set_var("http_proxy", proxy)
|
||||
Ok(proxy) => env::set_var("http_proxy", proxy),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user