Skip to content

Commit

Permalink
Follow proxy no proxy host configuration (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
lionelraeymaekers authored Dec 2, 2020
1 parent f12f0db commit 18613e3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/main/java/jenkins/plugins/slack/HttpClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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();
Expand All @@ -60,5 +60,4 @@ private static Credentials createCredentials(String userName, String password) {
return new UsernamePasswordCredentials(userName, password);
}
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 18613e3

Please sign in to comment.