Search in sources :

Example 21 with ProxySelector

use of java.net.ProxySelector in project platform_external_apache-http by android.

the class ProxySelectorRoutePlanner method determineProxy.

/**
 * Determines a proxy for the given target.
 *
 * @param target    the planned target, never <code>null</code>
 * @param request   the request to be sent, never <code>null</code>
 * @param context   the context, or <code>null</code>
 *
 * @return  the proxy to use, or <code>null</code> for a direct route
 *
 * @throws HttpException
 *         in case of system proxy settings that cannot be handled
 */
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
    // the proxy selector can be 'unset', so we better deal with null here
    ProxySelector psel = this.proxySelector;
    if (psel == null)
        psel = ProxySelector.getDefault();
    if (psel == null)
        return null;
    URI targetURI = null;
    try {
        targetURI = new URI(target.toURI());
    } catch (URISyntaxException usx) {
        throw new HttpException("Cannot convert host to URI: " + target, usx);
    }
    List<Proxy> proxies = psel.select(targetURI);
    Proxy p = chooseProxy(proxies, target, request, context);
    HttpHost result = null;
    if (p.type() == Proxy.Type.HTTP) {
        // convert the socket address to an HttpHost
        if (!(p.address() instanceof InetSocketAddress)) {
            throw new HttpException("Unable to handle non-Inet proxy address: " + p.address());
        }
        final InetSocketAddress isa = (InetSocketAddress) p.address();
        // assume default scheme (http)
        result = new HttpHost(getHost(isa), isa.getPort());
    }
    return result;
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) HttpHost(org.apache.http.HttpHost) InetSocketAddress(java.net.InetSocketAddress) HttpException(org.apache.http.HttpException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 22 with ProxySelector

use of java.net.ProxySelector in project graylog2-server by Graylog2.

the class OkHttpClientProvider method get.

@Override
public OkHttpClient get() {
    final OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder().retryOnConnectionFailure(true).connectTimeout(connectTimeout.getQuantity(), connectTimeout.getUnit()).writeTimeout(writeTimeout.getQuantity(), writeTimeout.getUnit()).readTimeout(readTimeout.getQuantity(), readTimeout.getUnit());
    if (httpProxyUri != null) {
        final ProxySelector proxySelector = new ProxySelector() {

            @Override
            public List<Proxy> select(URI uri) {
                final String host = uri.getHost();
                if (nonProxyHostsPattern != null && nonProxyHostsPattern.matches(host)) {
                    LOG.debug("Bypassing proxy server for {}", host);
                    return ImmutableList.of(Proxy.NO_PROXY);
                }
                try {
                    final InetAddress targetAddress = InetAddress.getByName(host);
                    if (targetAddress.isLoopbackAddress()) {
                        return ImmutableList.of(Proxy.NO_PROXY);
                    } else if (nonProxyHostsPattern != null && nonProxyHostsPattern.matches(targetAddress.getHostAddress())) {
                        LOG.debug("Bypassing proxy server for {}", targetAddress.getHostAddress());
                        return ImmutableList.of(Proxy.NO_PROXY);
                    }
                } catch (UnknownHostException e) {
                    LOG.debug("Unable to resolve host name for proxy selection: ", e);
                }
                final Proxy proxy = new Proxy(Proxy.Type.HTTP, getProxyAddress());
                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);
        if (!isNullOrEmpty(httpProxyUri.getUserInfo())) {
            final List<String> list = Splitter.on(":").limit(2).splitToList(httpProxyUri.getUserInfo());
            if (list.size() == 2) {
                clientBuilder.proxyAuthenticator(new ProxyAuthenticator(list.get(0), list.get(1)));
            }
        }
    }
    return clientBuilder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) URI(java.net.URI) ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 23 with ProxySelector

use of java.net.ProxySelector in project open-ecard by ecsec.

the class ProxySettingsLoader method load.

/**
 * Preload proxy settings according to the global options.
 * The load must be performed when the settings change while running.
 */
public void load() {
    synchronized (ProxySettingsLoader.class) {
        // load system proxy selector
        ProxySelector selector = new NoProxySelector();
        // get config values
        String scheme = OpenecardProperties.getProperty("proxy.scheme");
        // the empty string is no defined value, thus it means scheme not defined
        scheme = scheme != null ? scheme.toUpperCase() : "";
        String validate = OpenecardProperties.getProperty("proxy.validate_tls");
        String host = OpenecardProperties.getProperty("proxy.host");
        String port = OpenecardProperties.getProperty("proxy.port");
        String user = OpenecardProperties.getProperty("proxy.user");
        String pass = OpenecardProperties.getProperty("proxy.pass");
        String excl = OpenecardProperties.getProperty("proxy.excludes");
        // remove block of basic auth for http proxies
        setSystemProperty("jdk.http.auth.tunneling.disabledSchemes", "");
        clearSystemProperties();
        switch(scheme) {
            case "SOCKS":
                // try to load SOCKS proxy
                if (host != null && port != null) {
                    setSystemProperty("socksProxyHost", host);
                    setSystemProperty("socksProxyPort", port);
                    setSystemProperty("socksProxyVersion", "5");
                    setSystemProperty("java.net.socks.username", user);
                    setSystemProperty("java.net.socks.password", pass);
                    // search strategy fails for this case, see https://github.com/MarkusBernhardt/proxy-vole/issues/5
                    // furthermore there are issues with the protocol selection
                    // ProxySearch ps = new ProxySearch();
                    // ps.addStrategy(ProxySearch.Strategy.JAVA);
                    // selector = ps.getProxySelector();
                    ProtocolDispatchSelector ps = new ProtocolDispatchSelector();
                    ps.setFallbackSelector(new FixedSocksSelector(host, Integer.parseInt(port)));
                    selector = ps;
                    List<Pattern> exclusions = parseExclusionHosts(excl);
                    selector = new RegexProxySelector(selector, exclusions);
                }
                break;
            case "HTTP":
            case "HTTPS":
                // try to load HTTP proxy
                if (host != null && port != null) {
                    try {
                        if ("HTTP".equals(scheme)) {
                            setSystemProperty("http.proxyHost", host);
                            setSystemProperty("http.proxyPort", port);
                            setSystemProperty("https.proxyHost", host);
                            setSystemProperty("https.proxyPort", port);
                            if (user != null && pass != null) {
                                try {
                                    Authenticator.setDefault(createAuthenticator(user, pass));
                                } catch (SecurityException ignore) {
                                    LOG.error("Failed to set new Proxy Authenticator.");
                                }
                            }
                            ProxySearch ps = new ProxySearch();
                            ps.addStrategy(ProxySearch.Strategy.JAVA);
                            selector = ps.getProxySelector();
                        } else {
                            // use our own HTTP CONNECT Proxy
                            // the default is always validate
                            boolean valid = true;
                            if (validate != null) {
                                valid = Boolean.parseBoolean(validate);
                            }
                            // use our connect proxy with an empty parent selector
                            Proxy proxy = new HttpConnectProxy(scheme, valid, host, Integer.parseInt(port), user, pass);
                            selector = new SingleProxySelector(proxy);
                        }
                        List<Pattern> exclusions = parseExclusionHosts(excl);
                        selector = new RegexProxySelector(selector, exclusions);
                    } catch (NumberFormatException ex) {
                        LOG.error("Invalid port specified for HTTPS proxy, using system defaults.");
                    }
                }
                break;
            case "NO PROXY":
                selector = new NoProxySelector();
                break;
            default:
                // including "SYSTEM PROXY"
                if (!"SYSTEM PROXY".equals(scheme)) {
                    LOG.warn("Unsupported proxy scheme {} used.", scheme);
                }
                // get proxy for a common host and set system properties
                ProxySelector ps = ProxySearch.getDefaultProxySearch().getProxySelector();
                List<Proxy> proxies = ps != null ? ps.select(URI.create("https://google.com/")) : Collections.EMPTY_LIST;
                setSocksProperties(proxies);
                setHttpProperties(proxies);
                selector = new UpdatingProxySelector(new SelectorSupplier() {

                    @Override
                    public ProxySelector find() {
                        ProxySearch ps = ProxySearch.getDefaultProxySearch();
                        ProxySelector selector = ps.getProxySelector();
                        return selector;
                    }
                });
                break;
        }
        ProxySelector.setDefault(selector);
    }
}
Also used : ProxySearch(com.github.markusbernhardt.proxy.ProxySearch) Pattern(java.util.regex.Pattern) ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) FixedSocksSelector(com.github.markusbernhardt.proxy.selector.fixed.FixedSocksSelector) ProtocolDispatchSelector(com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector)

Example 24 with ProxySelector

use of java.net.ProxySelector in project hub-alert by blackducksoftware.

the class AzureBoardsProperties method createHttpTransport.

public HttpTransport createHttpTransport(ProxyInfo proxyInfo) {
    // Authenticated proxies aren't supported with the OAuth client library by default.
    // Need to use an Apache Http Client backed transport to support authenticated proxies.
    // Setup the client as the int-rest project does. That is known to setup a client that supports authenticated proxies.
    // https://github.com/googleapis/google-http-java-client/issues/190
    HttpClientBuilder httpClientBuilder = ApacheHttpTransport.newDefaultHttpClientBuilder();
    if (proxyInfo.shouldUseProxy()) {
        String proxyHost = proxyInfo.getHost().orElse(null);
        int proxyPort = proxyInfo.getPort();
        InetSocketAddress proxyAddress = InetSocketAddress.createUnresolved(proxyHost, proxyPort);
        ProxySelector proxySelector = ProxySelector.of(proxyAddress);
        httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(proxySelector));
        httpClientBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
        if (proxyInfo.hasAuthenticatedProxySettings()) {
            NTCredentials credentials = new NTCredentials(proxyInfo.getUsername().orElse(null), proxyInfo.getPassword().orElse(null), proxyInfo.getNtlmWorkstation().orElse(null), proxyInfo.getNtlmDomain().orElse(null));
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(proxyInfo.getHost().orElse(null), proxyInfo.getPort()), credentials);
            httpClientBuilder.setProxyAuthenticationStrategy(ProxyAuthenticationStrategy.INSTANCE);
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }
    }
    return new ApacheHttpTransport(httpClientBuilder.build());
}
Also used : ProxySelector(java.net.ProxySelector) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) InetSocketAddress(java.net.InetSocketAddress) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) ApacheHttpTransport(com.google.api.client.http.apache.v2.ApacheHttpTransport) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) NTCredentials(org.apache.http.auth.NTCredentials)

Example 25 with ProxySelector

use of java.net.ProxySelector in project nutch by apache.

the class OkHttp method setConf.

@Override
public void setConf(Configuration conf) {
    super.setConf(conf);
    // protocols in order of preference
    List<okhttp3.Protocol> protocols = new ArrayList<>();
    if (useHttp2) {
        protocols.add(okhttp3.Protocol.HTTP_2);
    }
    protocols.add(okhttp3.Protocol.HTTP_1_1);
    okhttp3.OkHttpClient.Builder builder = new OkHttpClient.Builder().protocols(// 
    protocols).retryOnConnectionFailure(// 
    true).followRedirects(// 
    false).connectTimeout(timeout, TimeUnit.MILLISECONDS).writeTimeout(timeout, TimeUnit.MILLISECONDS).readTimeout(timeout, TimeUnit.MILLISECONDS);
    if (!tlsCheckCertificate) {
        builder.sslSocketFactory(trustAllSslSocketFactory, (X509TrustManager) trustAllCerts[0]);
        builder.hostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    }
    if (!accept.isEmpty()) {
        getCustomRequestHeaders().add(new String[] { "Accept", accept });
    }
    if (!acceptLanguage.isEmpty()) {
        getCustomRequestHeaders().add(new String[] { "Accept-Language", acceptLanguage });
    }
    if (!acceptCharset.isEmpty()) {
        getCustomRequestHeaders().add(new String[] { "Accept-Charset", acceptCharset });
    }
    if (useProxy) {
        Proxy proxy = new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort));
        String proxyUsername = conf.get("http.proxy.username");
        if (proxyUsername == null) {
            ProxySelector selector = new ProxySelector() {

                @SuppressWarnings("serial")
                private final List<Proxy> noProxyList = new ArrayList<Proxy>() {

                    {
                        add(Proxy.NO_PROXY);
                    }
                };

                @SuppressWarnings("serial")
                private final List<Proxy> proxyList = new ArrayList<Proxy>() {

                    {
                        add(proxy);
                    }
                };

                @Override
                public List<Proxy> select(URI uri) {
                    if (useProxy(uri)) {
                        return proxyList;
                    }
                    return noProxyList;
                }

                @Override
                public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
                    LOG.error("Connection to proxy failed for {}: {}", uri, ioe);
                }
            };
            builder.proxySelector(selector);
        } else {
            /*
         * NOTE: the proxy exceptions list does NOT work with proxy
         * username/password because an okhttp3 bug
         * (https://github.com/square/okhttp/issues/3995) when using the
         * ProxySelector class with proxy auth. If a proxy username is present,
         * the configured proxy will be used for ALL requests.
         */
            if (proxyException.size() > 0) {
                LOG.warn("protocol-okhttp does not respect 'http.proxy.exception.list' setting when " + "'http.proxy.username' is set. This is a limitation of the current okhttp3 " + "implementation, see NUTCH-2636");
            }
            builder.proxy(proxy);
            String proxyPassword = conf.get("http.proxy.password");
            Authenticator proxyAuthenticator = new Authenticator() {

                @Override
                public Request authenticate(okhttp3.Route route, okhttp3.Response response) throws IOException {
                    String credential = okhttp3.Credentials.basic(proxyUsername, proxyPassword);
                    return response.request().newBuilder().header("Proxy-Authorization", credential).build();
                }
            };
            builder.proxyAuthenticator(proxyAuthenticator);
        }
    }
    if (storeIPAddress || storeHttpHeaders || storeHttpRequest) {
        builder.addNetworkInterceptor(new HTTPHeadersInterceptor());
    }
    // enable support for Brotli compression (Content-Encoding)
    builder.addInterceptor(BrotliInterceptor.INSTANCE);
    client = builder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) ProxySelector(java.net.ProxySelector) Response(org.apache.nutch.net.protocols.Response) Proxy(java.net.Proxy) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Protocol(okhttp3.Protocol) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Authenticator(okhttp3.Authenticator)

Aggregations

ProxySelector (java.net.ProxySelector)53 URI (java.net.URI)33 Proxy (java.net.Proxy)32 InetSocketAddress (java.net.InetSocketAddress)21 IOException (java.io.IOException)19 SocketAddress (java.net.SocketAddress)13 List (java.util.List)7 InetAddress (java.net.InetAddress)6 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 HttpURLConnection (java.net.HttpURLConnection)3 URISyntaxException (java.net.URISyntaxException)3 HttpHost (org.apache.http.HttpHost)3 Test (org.junit.Test)3 ManualProxySelector (org.kse.utilities.net.ManualProxySelector)3 NoProxySelector (org.kse.utilities.net.NoProxySelector)3 PacProxySelector (org.kse.utilities.net.PacProxySelector)3 ProxyAddress (org.kse.utilities.net.ProxyAddress)3 SystemProxySelector (org.kse.utilities.net.SystemProxySelector)3 InterruptedIOException (java.io.InterruptedIOException)2