From c30ce343b77978d33fdcc8fe14d21cf3c27009ab Mon Sep 17 00:00:00 2001 From: Aviram Hassan Date: Fri, 17 May 2024 22:56:38 +0300 Subject: [PATCH] Added support for HTTP proxy with hyper-proxy2 (#1496) Signed-off-by: Aviram Hassan --- kube-client/Cargo.toml | 4 +++- kube-client/src/client/builder.rs | 32 ++++++++++++++++++++----------- kube/Cargo.toml | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/kube-client/Cargo.toml b/kube-client/Cargo.toml index 1b280f78b..d3719a1c2 100644 --- a/kube-client/Cargo.toml +++ b/kube-client/Cargo.toml @@ -25,13 +25,14 @@ jsonpatch = ["kube-core/jsonpatch"] admission = ["kube-core/admission"] config = ["__non_core", "pem", "home"] socks5 = ["hyper-socks2"] +http_proxy = ["hyper-proxy2"] unstable-client = [] # private feature sets; do not use __non_core = ["tracing", "serde_yaml", "base64"] [package.metadata.docs.rs] -features = ["client", "rustls-tls", "openssl-tls", "ws", "oauth", "oidc", "jsonpatch", "admission", "k8s-openapi/latest", "socks5", "unstable-client"] +features = ["client", "rustls-tls", "openssl-tls", "ws", "oauth", "oidc", "jsonpatch", "admission", "k8s-openapi/latest", "socks5", "unstable-client", "http-proxy"] # Define the configuration attribute `docsrs`. Used to enable `doc_cfg` feature. rustdoc-args = ["--cfg", "docsrs"] @@ -61,6 +62,7 @@ kube-core = { path = "../kube-core", version = "=0.91.0" } jsonpath-rust = { workspace = true, optional = true } tokio-util = { workspace = true, features = ["io", "codec"], optional = true } hyper = { workspace = true, features = ["client", "http1"], optional = true } +hyper-proxy2 = {version = "0.1", optional = true} hyper-util = { workspace = true, features = ["client", "client-legacy", "http1", "tokio"], optional = true } hyper-rustls = { workspace = true, features = ["http1", "logging", "native-tokio", "ring", "tls12"], optional = true } hyper-socks2 = { workspace = true, optional = true } diff --git a/kube-client/src/client/builder.rs b/kube-client/src/client/builder.rs index f6e832513..81289ec72 100644 --- a/kube-client/src/client/builder.rs +++ b/kube-client/src/client/builder.rs @@ -82,18 +82,28 @@ impl TryFrom for ClientBuilder { let mut connector = HttpConnector::new(); connector.enforce_http(false); - #[cfg(feature = "socks5")] - if let Some(proxy_addr) = config.proxy_url.clone() { - let connector = hyper_socks2::SocksConnector { - proxy_addr, - auth: None, - connector, - }; - - return make_generic_builder(connector, config); + match config.proxy_url.as_ref() { + #[cfg(feature = "socks5")] + Some(proxy_url) if proxy_url.scheme_str() == Some("socks5") => { + let connector = hyper_socks2::SocksConnector { + proxy_addr: proxy_url.clone(), + auth: None, + connector, + }; + + make_generic_builder(connector, config) + } + + #[cfg(feature = "http-proxy")] + Some(proxy_url) if proxy_url.scheme_str() == Some("http") => { + let proxy = hyper_proxy2::Proxy::new(hyper_proxy2::Intercept::All, proxy_url.clone()); + let connector = hyper_proxy2::ProxyConnector::from_proxy_unsecured(connector, proxy); + + make_generic_builder(connector, config) + } + + _ => make_generic_builder(connector, config), } - - make_generic_builder(connector, config) } } diff --git a/kube/Cargo.toml b/kube/Cargo.toml index 28f2ea70a..f1004d7ca 100644 --- a/kube/Cargo.toml +++ b/kube/Cargo.toml @@ -37,7 +37,7 @@ unstable-client = ["kube-client/unstable-client"] socks5 = ["kube-client/socks5"] [package.metadata.docs.rs] -features = ["client", "rustls-tls", "openssl-tls", "derive", "ws", "oauth", "jsonpatch", "admission", "runtime", "k8s-openapi/latest", "unstable-runtime", "socks5"] +features = ["client", "rustls-tls", "openssl-tls", "derive", "ws", "oauth", "jsonpatch", "admission", "runtime", "k8s-openapi/latest", "unstable-runtime", "socks5", "http-proxy"] # Define the configuration attribute `docsrs`. Used to enable `doc_cfg` feature. rustdoc-args = ["--cfg", "docsrs"]