Search in sources :

Example 16 with ProxySelector

use of java.net.ProxySelector in project keystore-explorer by kaikramer.

the class DPreferences method storeProxyPreferences.

private boolean storeProxyPreferences() {
    // Store current proxy selector - compare with new one to see if default needs updated
    ProxySelector defaultProxySelector = ProxySelector.getDefault();
    if (jrbNoProxy.isSelected()) {
        NoProxySelector noProxySelector = new NoProxySelector();
        if (!noProxySelector.equals(defaultProxySelector)) {
            ProxySelector.setDefault(noProxySelector);
        }
    } else if (jrbSystemProxySettings.isSelected()) {
        SystemProxySelector systemProxySelector = new SystemProxySelector();
        if (!systemProxySelector.equals(defaultProxySelector)) {
            ProxySelector.setDefault(systemProxySelector);
        }
    } else if (jrbManualProxyConfig.isSelected()) {
        String httpHost = jtfHttpHost.getText().trim();
        String httpPortStr = jtfHttpPort.getText().trim();
        String httpsHost = jtfHttpsHost.getText().trim();
        String httpsPortStr = jtfHttpsPort.getText().trim();
        String socksHost = jtfSocksHost.getText().trim();
        String socksPortStr = jtfSocksPort.getText().trim();
        ProxyAddress httpProxyAddress = null;
        ProxyAddress httpsProxyAddress = null;
        ProxyAddress socksProxyAddress = null;
        // Require at least one of the HTTP host or HTTPS host or SOCKS host manual settings
        if ((httpHost.length() == 0) && (httpsHost.length() == 0) && (socksHost.length() == 0)) {
            jtpPreferences.setSelectedIndex(3);
            JOptionPane.showMessageDialog(this, res.getString("DPreferences.ManualConfigReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
            return false;
        } else {
            if (httpHost.length() > 0) {
                if (httpPortStr.length() == 0) {
                    jtpPreferences.setSelectedIndex(3);
                    JOptionPane.showMessageDialog(this, res.getString("DPreferences.PortReqHttp.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                    return false;
                } else {
                    try {
                        int httpPort = Integer.parseInt(httpPortStr);
                        if (httpPort < 1) {
                            throw new NumberFormatException();
                        }
                        httpProxyAddress = new ProxyAddress(httpHost, httpPort);
                    } catch (NumberFormatException ex) {
                        jtpPreferences.setSelectedIndex(3);
                        JOptionPane.showMessageDialog(this, res.getString("DPreferences.IntegerPortReqHttp.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                        return false;
                    }
                }
            }
            if (httpsHost.length() > 0) {
                if (httpsPortStr.length() == 0) {
                    jtpPreferences.setSelectedIndex(3);
                    JOptionPane.showMessageDialog(this, res.getString("DPreferences.PortReqHttps.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                    return false;
                } else {
                    try {
                        int httpsPort = Integer.parseInt(httpsPortStr);
                        if (httpsPort < 1) {
                            throw new NumberFormatException();
                        }
                        httpsProxyAddress = new ProxyAddress(httpsHost, httpsPort);
                    } catch (NumberFormatException ex) {
                        jtpPreferences.setSelectedIndex(3);
                        JOptionPane.showMessageDialog(this, res.getString("DPreferences.IntegerPortReqHttps.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                        return false;
                    }
                }
            }
            if (socksHost.length() > 0) {
                if (socksPortStr.length() == 0) {
                    jtpPreferences.setSelectedIndex(3);
                    JOptionPane.showMessageDialog(this, res.getString("DPreferences.PortReqSocks.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                    return false;
                } else {
                    try {
                        int socksPort = Integer.parseInt(socksPortStr);
                        if (socksPort < 1) {
                            throw new NumberFormatException();
                        }
                        socksProxyAddress = new ProxyAddress(socksHost, socksPort);
                    } catch (NumberFormatException ex) {
                        jtpPreferences.setSelectedIndex(3);
                        JOptionPane.showMessageDialog(this, res.getString("DPreferences.IntegerPortReqSocks.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
                        return false;
                    }
                }
            }
            ManualProxySelector manualProxySelector = new ManualProxySelector(httpProxyAddress, httpsProxyAddress, null, socksProxyAddress);
            if (!manualProxySelector.equals(defaultProxySelector)) {
                ProxySelector.setDefault(manualProxySelector);
            }
        }
    } else if (jrbAutomaticProxyConfig.isSelected()) {
        String pacUrl = jtfPacUrl.getText().trim();
        if (pacUrl.length() == 0) {
            jtpPreferences.setSelectedIndex(3);
            JOptionPane.showMessageDialog(this, res.getString("DPreferences.PacUrlReq.message"), getTitle(), JOptionPane.WARNING_MESSAGE);
            return false;
        }
        PacProxySelector pacProxySelector = new PacProxySelector(pacUrl);
        if (!pacProxySelector.equals(defaultProxySelector)) {
            ProxySelector.setDefault(pacProxySelector);
        }
    }
    return true;
}
Also used : ManualProxySelector(org.kse.utilities.net.ManualProxySelector) ProxySelector(java.net.ProxySelector) PacProxySelector(org.kse.utilities.net.PacProxySelector) SystemProxySelector(org.kse.utilities.net.SystemProxySelector) NoProxySelector(org.kse.utilities.net.NoProxySelector) PacProxySelector(org.kse.utilities.net.PacProxySelector) ProxyAddress(org.kse.utilities.net.ProxyAddress) ManualProxySelector(org.kse.utilities.net.ManualProxySelector) SystemProxySelector(org.kse.utilities.net.SystemProxySelector) NoProxySelector(org.kse.utilities.net.NoProxySelector)

Example 17 with ProxySelector

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

the class ProxyDiagnostics method toString.

/**
 * Get the diagnostics for proxy information.
 * @return the information.
 */
@Override
public String toString() {
    ProxySelector selector = ProxySelector.getDefault();
    StringBuilder result = new StringBuilder();
    for (Proxy proxy : selector.select(destURI)) {
        SocketAddress address = proxy.address();
        if (address == null) {
            result.append("Direct connection\n");
            continue;
        }
        result.append(proxy);
        if (address instanceof InetSocketAddress) {
            InetSocketAddress ina = (InetSocketAddress) address;
            result.append(' ');
            result.append(ina.getHostName());
            result.append(':');
            result.append(ina.getPort());
            if (ina.isUnresolved()) {
                result.append(" [unresolved]");
            } else {
                InetAddress addr = ina.getAddress();
                result.append(" [");
                result.append(addr.getHostAddress());
                result.append(']');
            }
        }
        result.append('\n');
    }
    return result.toString();
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 18 with ProxySelector

use of java.net.ProxySelector in project triplea by triplea-game.

the class HttpProxy method getSystemProxy.

private static Optional<InetSocketAddress> getSystemProxy() {
    // this property is temporarily needed to turn on proxying
    SystemProperties.setJavaNetUseSystemProxies("true");
    try {
        final ProxySelector def = ProxySelector.getDefault();
        if (def != null) {
            // TODO: if we switch to HTTPS, we will potentially need an https URL, proxies can very by protocol.
            final String anyUrlThatShouldAvailable = "http://sourceforge.net/";
            final List<Proxy> proxyList = def.select(new URI(anyUrlThatShouldAvailable));
            ProxySelector.setDefault(null);
            if (proxyList != null && !proxyList.isEmpty()) {
                final Proxy proxy = proxyList.get(0);
                final InetSocketAddress address = (InetSocketAddress) proxy.address();
                return Optional.ofNullable(address);
            }
        }
    } catch (final Exception e) {
        ClientLogger.logQuietly("Failed to get system HTTP proxy", e);
    } finally {
        SystemProperties.setJavaNetUseSystemProxies("false");
    }
    return Optional.empty();
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI)

Example 19 with ProxySelector

use of java.net.ProxySelector in project dropwizard by dropwizard.

the class JerseyClientBuilderTest method usesACustomHttpRoutePlanner.

@Test
void usesACustomHttpRoutePlanner() {
    final HttpRoutePlanner customHttpRoutePlanner = new SystemDefaultRoutePlanner(new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            return Collections.singletonList(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.53.12", 8080)));
        }

        @Override
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        }
    });
    builder.using(customHttpRoutePlanner);
    verify(apacheHttpClientBuilder).using(customHttpRoutePlanner);
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) InetSocketAddress(java.net.InetSocketAddress) List(java.util.List) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) Test(org.junit.jupiter.api.Test)

Example 20 with ProxySelector

use of java.net.ProxySelector in project grpc-java by grpc.

the class ProxyDetectorImpl method detectProxy.

private ProxiedSocketAddress detectProxy(InetSocketAddress targetAddr) throws IOException {
    URI uri;
    String host;
    try {
        host = GrpcUtil.getHost(targetAddr);
    } catch (Throwable t) {
        // Workaround for Android API levels < 19 if getHostName causes a NetworkOnMainThreadException
        log.log(Level.WARNING, "Failed to get host for proxy lookup, proceeding without proxy", t);
        return null;
    }
    try {
        uri = new URI(PROXY_SCHEME, null, /* userInfo */
        host, targetAddr.getPort(), null, /* path */
        null, /* query */
        null);
    } catch (final URISyntaxException e) {
        log.log(Level.WARNING, "Failed to construct URI for proxy lookup, proceeding without proxy", e);
        return null;
    }
    ProxySelector proxySelector = this.proxySelector.get();
    if (proxySelector == null) {
        log.log(Level.FINE, "proxy selector is null, so continuing without proxy lookup");
        return null;
    }
    List<Proxy> proxies = proxySelector.select(uri);
    if (proxies.size() > 1) {
        log.warning("More than 1 proxy detected, gRPC will select the first one");
    }
    Proxy proxy = proxies.get(0);
    if (proxy.type() == Proxy.Type.DIRECT) {
        return null;
    }
    InetSocketAddress proxyAddr = (InetSocketAddress) proxy.address();
    // The prompt string should be the realm as returned by the server.
    // We don't have it because we are avoiding the full handshake.
    String promptString = "";
    PasswordAuthentication auth = authenticationProvider.requestPasswordAuthentication(GrpcUtil.getHost(proxyAddr), proxyAddr.getAddress(), proxyAddr.getPort(), PROXY_SCHEME, promptString, null);
    final InetSocketAddress resolvedProxyAddr;
    if (proxyAddr.isUnresolved()) {
        InetAddress resolvedAddress = InetAddress.getByName(proxyAddr.getHostName());
        resolvedProxyAddr = new InetSocketAddress(resolvedAddress, proxyAddr.getPort());
    } else {
        resolvedProxyAddr = proxyAddr;
    }
    HttpConnectProxiedSocketAddress.Builder builder = HttpConnectProxiedSocketAddress.newBuilder().setTargetAddress(targetAddr).setProxyAddress(resolvedProxyAddr);
    if (auth == null) {
        return builder.build();
    }
    return builder.setUsername(auth.getUserName()).setPassword(auth.getPassword() == null ? null : new String(auth.getPassword())).build();
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) HttpConnectProxiedSocketAddress(io.grpc.HttpConnectProxiedSocketAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) InetAddress(java.net.InetAddress) PasswordAuthentication(java.net.PasswordAuthentication)

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