Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend proxy support for agents #3

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,44 @@
import com.azure.core.http.ProxyOptions;
import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
import hudson.ProxyConfiguration;
import hudson.Util;
import jenkins.model.Jenkins;
import jenkins.util.JenkinsJVM;

import java.net.InetSocketAddress;

public class HttpClientRetriever {

/**
* Jenkins class loader prevents the built in auto-detection from working
* need to pass an explicit http client.
*
* If this is running on an agent then use {@link #get(ProxyConfiguration)}
*/
public static HttpClient get() {
// Jenkins class loader prevents the built in auto-detection from working
// need to pass an explicit http client
ProxyOptions proxyOptions = null;
if (JenkinsJVM.isJenkinsJVM()) {
ProxyConfiguration proxy = Jenkins.get().proxy;
if (proxy != null) {
proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(proxy.name, proxy.port));
if (proxy.getSecretPassword() != null && !proxy.getSecretPassword().getPlainText().equals("")) {
proxyOptions.setCredentials(proxy.getUserName(), proxy.getSecretPassword().getPlainText());
}
return get(proxy);
}
}
return new NettyAsyncHttpClientBuilder().build();
}

public static HttpClient get(ProxyConfiguration proxy) {
ProxyOptions proxyOptions = null;

if (proxy != null) {
proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(proxy.name, proxy.port));
if (proxy.getSecretPassword() != null && !proxy.getSecretPassword().getPlainText().equals("")) {
proxyOptions.setCredentials(proxy.getUserName(), proxy.getSecretPassword().getPlainText());
}
String noProxyHost = Util.fixEmpty(proxy.getNoProxyHost());
if (noProxyHost != null) {
proxyOptions.setNonProxyHosts(noProxyHost);
}
}

return new NettyAsyncHttpClientBuilder().proxy(proxyOptions).build();
}
}