Skip to content

Commit

Permalink
Disabling a configured proxy for requests to localhost/127.0.0.1/::1. (
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisoelkers authored and joschi committed May 30, 2016
1 parent 7438192 commit 86d998d
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@
package org.graylog2.shared.bindings.providers;

import com.github.joschi.jadconfig.util.Duration;
import com.google.common.collect.ImmutableList;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.List;

import static java.util.Objects.requireNonNull;

Expand All @@ -40,6 +49,7 @@
*/
@Singleton
public class OkHttpClientProvider implements Provider<OkHttpClient> {
private static final Logger LOG = LoggerFactory.getLogger(OkHttpClientProvider.class);
protected final Duration connectTimeout;
protected final Duration readTimeout;
protected final Duration writeTimeout;
Expand All @@ -66,7 +76,27 @@ public OkHttpClient get() {

if (httpProxyUri != null) {
final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxyUri.getHost(), httpProxyUri.getPort()));
clientBuilder.proxy(proxy);
final ProxySelector proxySelector = new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
try {
final InetAddress targetAddress = InetAddress.getByName(uri.getHost());
if (targetAddress.isLoopbackAddress()) {
return ImmutableList.of(Proxy.NO_PROXY);
}
} catch (UnknownHostException e) {
LOG.debug("Unable to resolve host name for proxy selection: ", e);
}
return ImmutableList.of(proxy);
}

@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
LOG.warn("Unable to connect to proxy: ", ioe);
}
};

clientBuilder.proxySelector(proxySelector);
}

return clientBuilder.build();
Expand Down

0 comments on commit 86d998d

Please sign in to comment.