Skip to content

Commit

Permalink
Added support for HTTP proxy with hyper-proxy2 (#1496)
Browse files Browse the repository at this point in the history
Signed-off-by: Aviram Hassan <aviramyhassan@gmail.com>
  • Loading branch information
aviramha committed May 17, 2024
1 parent a6f4337 commit c30ce34
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
4 changes: 3 additions & 1 deletion kube-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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 }
Expand Down
32 changes: 21 additions & 11 deletions kube-client/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,28 @@ impl TryFrom<Config> for ClientBuilder<GenericService> {
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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion kube/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down

0 comments on commit c30ce34

Please sign in to comment.