Skip to content

Commit

Permalink
Merge pull request #536 from navikt/noproxy
Browse files Browse the repository at this point in the history
respect no_proxy setting when opening connection
  • Loading branch information
jan-olaveide authored May 12, 2022
2 parents ff6e142 + d21316b commit e154aa7
Showing 1 changed file with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.io.IOException;
import java.net.*;
import java.util.Arrays;
import java.util.Optional;

import static java.net.Proxy.Type.HTTP;

Expand All @@ -16,6 +18,7 @@ public class ProxyAwareResourceRetriever extends DefaultResourceRetriever {
public static final int DEFAULT_HTTP_SIZE_LIMIT = 50 * 1024;
private static final Logger LOG = LoggerFactory.getLogger(ProxyAwareResourceRetriever.class);
private final boolean usePlainTextForHttps;
private final URL proxyURL;

public ProxyAwareResourceRetriever() {
this(null);
Expand All @@ -32,13 +35,7 @@ public ProxyAwareResourceRetriever(URL proxyUrl, boolean usePlainTextForHttps) {
ProxyAwareResourceRetriever(URL proxyUrl, boolean usePlainTextForHttps, int connectTimeout, int readTimeout, int sizeLimit) {
super(connectTimeout, readTimeout, sizeLimit);
this.usePlainTextForHttps = usePlainTextForHttps;
if (proxyUrl != null) {
LOG.info("Using proxy URL {}",proxyUrl);
setProxy(new Proxy(HTTP, new InetSocketAddress(proxyUrl.getHost(), proxyUrl.getPort())));
}
else {
LOG.info("No proxying");
}
this.proxyURL = proxyUrl;
}

URL urlWithPlainTextForHttps(URL url) throws IOException {
Expand All @@ -60,6 +57,30 @@ URL urlWithPlainTextForHttps(URL url) throws IOException {
@Override
protected HttpURLConnection openConnection(URL url) throws IOException {
URL urlToOpen = usePlainTextForHttps ? urlWithPlainTextForHttps(url) : url;
return super.openConnection(urlToOpen);
if (shouldProxy(url)) {
return (HttpURLConnection)urlToOpen.openConnection(proxy());
}
return (HttpURLConnection)urlToOpen.openConnection();
}

private boolean shouldProxy(URL url) {
return proxyURL != null && !isNoProxy(url);
}

private boolean isNoProxy(URL url) {
return Optional.ofNullable(System.getenv("NO_PROXY"))
.map(s -> Arrays.stream(s.split(","))
.anyMatch(url.getHost()::contains))
.isPresent();
}

@Override
public Proxy getProxy() {
return proxyURL != null ? proxy() : null;
}


private Proxy proxy() {
return new Proxy(HTTP, new InetSocketAddress(proxyURL.getHost(), proxyURL.getPort()));
}
}

0 comments on commit e154aa7

Please sign in to comment.