From 18613e3fe1d90074c5c141047c402622302f9c87 Mon Sep 17 00:00:00 2001 From: Raeymaekers Lionel <52168082+lionelraeymaekers@users.noreply.github.com> Date: Wed, 2 Dec 2020 09:28:20 +0100 Subject: [PATCH] Follow proxy no proxy host configuration (#745) --- .../jenkins/plugins/slack/HttpClient.java | 5 +- .../slack/NoProxyHostCheckerRoutePlanner.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/main/java/jenkins/plugins/slack/NoProxyHostCheckerRoutePlanner.java diff --git a/src/main/java/jenkins/plugins/slack/HttpClient.java b/src/main/java/jenkins/plugins/slack/HttpClient.java index 25080878..2dece14a 100644 --- a/src/main/java/jenkins/plugins/slack/HttpClient.java +++ b/src/main/java/jenkins/plugins/slack/HttpClient.java @@ -1,6 +1,7 @@ package jenkins.plugins.slack; import hudson.ProxyConfiguration; +import jenkins.plugins.slack.NoProxyHostCheckerRoutePlanner; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; @@ -13,7 +14,6 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; -import org.apache.http.impl.conn.DefaultProxyRoutePlanner; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -37,7 +37,7 @@ public static CloseableHttpClient getCloseableHttpClient(ProxyConfiguration prox if (proxy != null) { final HttpHost proxyHost = new HttpHost(proxy.name, proxy.port); - final HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost); + final HttpRoutePlanner routePlanner = new NoProxyHostCheckerRoutePlanner(proxy.getNoProxyHost(), proxyHost); clientBuilder.setRoutePlanner(routePlanner); String username = proxy.getUserName(); @@ -60,5 +60,4 @@ private static Credentials createCredentials(String userName, String password) { return new UsernamePasswordCredentials(userName, password); } } - } diff --git a/src/main/java/jenkins/plugins/slack/NoProxyHostCheckerRoutePlanner.java b/src/main/java/jenkins/plugins/slack/NoProxyHostCheckerRoutePlanner.java new file mode 100644 index 00000000..d3bcb064 --- /dev/null +++ b/src/main/java/jenkins/plugins/slack/NoProxyHostCheckerRoutePlanner.java @@ -0,0 +1,47 @@ +package jenkins.plugins.slack; + +import hudson.ProxyConfiguration; +import java.util.regex.Pattern; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.conn.routing.HttpRoute; +import org.apache.http.conn.routing.HttpRoutePlanner; +import org.apache.http.impl.conn.DefaultProxyRoutePlanner; +import org.apache.http.impl.conn.DefaultRoutePlanner; +import org.apache.http.impl.conn.DefaultSchemePortResolver; +import org.apache.http.protocol.HttpContext; + +public class NoProxyHostCheckerRoutePlanner implements HttpRoutePlanner { + + private DefaultProxyRoutePlanner defaultProxyRoutePlanner = null; + private DefaultRoutePlanner defaultRoutePlanner = null; + private String noProxyHost = null; + + public NoProxyHostCheckerRoutePlanner(String noProxyHost, HttpHost host){ + defaultProxyRoutePlanner = new DefaultProxyRoutePlanner(host); + defaultRoutePlanner = new DefaultRoutePlanner(new DefaultSchemePortResolver()); + this.noProxyHost = noProxyHost; + } + + public void setProxy(HttpHost host){ + defaultProxyRoutePlanner = new DefaultProxyRoutePlanner(host); + } + + public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws org.apache.http.HttpException { + final String targetHostUri = target.toURI(); + if(isNoProxyHost(targetHostUri)) + return defaultRoutePlanner.determineRoute(target,request,context); + return defaultProxyRoutePlanner.determineRoute(target,request,context); + } + + private boolean isNoProxyHost(String host) { + if (host!=null && noProxyHost!=null) { + for (Pattern p : ProxyConfiguration.getNoProxyHostPatterns(noProxyHost)) { + if (p.matcher(host).matches()) { + return true; + } + } + } + return false; + } +}