Search in sources :

Example 61 with Proxy

use of java.net.Proxy in project jdk8u_jdk by JetBrains.

the class DefaultProxySelector method select.

/**
     * select() method. Where all the hard work is done.
     * Build a list of proxies depending on URI.
     * Since we're only providing compatibility with the system properties
     * from previous releases (see list above), that list will always
     * contain 1 single proxy, default being NO_PROXY.
     */
public java.util.List<Proxy> select(URI uri) {
    if (uri == null) {
        throw new IllegalArgumentException("URI can't be null.");
    }
    String protocol = uri.getScheme();
    String host = uri.getHost();
    if (host == null) {
        // This is a hack to ensure backward compatibility in two
        // cases: 1. hostnames contain non-ascii characters,
        // internationalized domain names. in which case, URI will
        // return null, see BugID 4957669; 2. Some hostnames can
        // contain '_' chars even though it's not supposed to be
        // legal, in which case URI will return null for getHost,
        // but not for getAuthority() See BugID 4913253
        String auth = uri.getAuthority();
        if (auth != null) {
            int i;
            i = auth.indexOf('@');
            if (i >= 0) {
                auth = auth.substring(i + 1);
            }
            i = auth.lastIndexOf(':');
            if (i >= 0) {
                auth = auth.substring(0, i);
            }
            host = auth;
        }
    }
    if (protocol == null || host == null) {
        throw new IllegalArgumentException("protocol = " + protocol + " host = " + host);
    }
    List<Proxy> proxyl = new ArrayList<Proxy>(1);
    NonProxyInfo pinfo = null;
    if ("http".equalsIgnoreCase(protocol)) {
        pinfo = NonProxyInfo.httpNonProxyInfo;
    } else if ("https".equalsIgnoreCase(protocol)) {
        // HTTPS uses the same property as HTTP, for backward
        // compatibility
        pinfo = NonProxyInfo.httpNonProxyInfo;
    } else if ("ftp".equalsIgnoreCase(protocol)) {
        pinfo = NonProxyInfo.ftpNonProxyInfo;
    } else if ("socket".equalsIgnoreCase(protocol)) {
        pinfo = NonProxyInfo.socksNonProxyInfo;
    }
    /**
         * Let's check the System properties for that protocol
         */
    final String proto = protocol;
    final NonProxyInfo nprop = pinfo;
    final String urlhost = host.toLowerCase();
    /**
         * This is one big doPrivileged call, but we're trying to optimize
         * the code as much as possible. Since we're checking quite a few
         * System properties it does help having only 1 call to doPrivileged.
         * Be mindful what you do in here though!
         */
    Proxy p = AccessController.doPrivileged(new PrivilegedAction<Proxy>() {

        public Proxy run() {
            int i, j;
            String phost = null;
            int pport = 0;
            String nphosts = null;
            InetSocketAddress saddr = null;
            // Then let's walk the list of protocols in our array
            for (i = 0; i < props.length; i++) {
                if (props[i][0].equalsIgnoreCase(proto)) {
                    for (j = 1; j < props[i].length; j++) {
                        /* System.getProp() will give us an empty
                                 * String, "" for a defined but "empty"
                                 * property.
                                 */
                        phost = NetProperties.get(props[i][j] + "Host");
                        if (phost != null && phost.length() != 0)
                            break;
                    }
                    if (phost == null || phost.length() == 0) {
                        /**
                                 * No system property defined for that
                                 * protocol. Let's check System Proxy
                                 * settings (Gnome & Windows) if we were
                                 * instructed to.
                                 */
                        if (hasSystemProxies) {
                            String sproto;
                            if (proto.equalsIgnoreCase("socket"))
                                sproto = "socks";
                            else
                                sproto = proto;
                            Proxy sproxy = getSystemProxy(sproto, urlhost);
                            if (sproxy != null) {
                                return sproxy;
                            }
                        }
                        return Proxy.NO_PROXY;
                    }
                    // Let's get the NonProxyHosts property
                    if (nprop != null) {
                        nphosts = NetProperties.get(nprop.property);
                        synchronized (nprop) {
                            if (nphosts == null) {
                                if (nprop.defaultVal != null) {
                                    nphosts = nprop.defaultVal;
                                } else {
                                    nprop.hostsSource = null;
                                    nprop.pattern = null;
                                }
                            } else if (nphosts.length() != 0) {
                                // add the required default patterns
                                // but only if property no set. If it
                                // is empty, leave empty.
                                nphosts += "|" + NonProxyInfo.defStringVal;
                            }
                            if (nphosts != null) {
                                if (!nphosts.equals(nprop.hostsSource)) {
                                    nprop.pattern = toPattern(nphosts);
                                    nprop.hostsSource = nphosts;
                                }
                            }
                            if (shouldNotUseProxyFor(nprop.pattern, urlhost)) {
                                return Proxy.NO_PROXY;
                            }
                        }
                    }
                    // We got a host, let's check for port
                    pport = NetProperties.getInteger(props[i][j] + "Port", 0).intValue();
                    if (pport == 0 && j < (props[i].length - 1)) {
                        // Let's try the other prefixes for that proto
                        for (int k = 1; k < (props[i].length - 1); k++) {
                            if ((k != j) && (pport == 0))
                                pport = NetProperties.getInteger(props[i][k] + "Port", 0).intValue();
                        }
                    }
                    // Still couldn't find a port, let's use default
                    if (pport == 0) {
                        if (// SOCKS
                        j == (props[i].length - 1))
                            pport = defaultPort("socket");
                        else
                            pport = defaultPort(proto);
                    }
                    // We did find a proxy definition.
                    // Let's create the address, but don't resolve it
                    // as this will be done at connection time
                    saddr = InetSocketAddress.createUnresolved(phost, pport);
                    // Socks is *always* the last on the list.
                    if (j == (props[i].length - 1)) {
                        int version = NetProperties.getInteger(SOCKS_PROXY_VERSION, 5).intValue();
                        return SocksProxy.create(saddr, version);
                    } else {
                        return new Proxy(Proxy.Type.HTTP, saddr);
                    }
                }
            }
            return Proxy.NO_PROXY;
        }
    });
    proxyl.add(p);
    /*
         * If no specific property was set for that URI, we should be
         * returning an iterator to an empty List.
         */
    return proxyl;
}
Also used : SocksProxy(sun.net.SocksProxy) Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList)

Example 62 with Proxy

use of java.net.Proxy in project jdk8u_jdk by JetBrains.

the class ResolverDirectHTTP method openConnection.

private URLConnection openConnection(URL url) throws IOException {
    String proxyHostProp = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpProxyHost]);
    String proxyPortProp = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpProxyPort]);
    String proxyUser = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpProxyUser]);
    String proxyPass = engineGetProperty(ResolverDirectHTTP.properties[ResolverDirectHTTP.HttpProxyPass]);
    Proxy proxy = null;
    if ((proxyHostProp != null) && (proxyPortProp != null)) {
        int port = Integer.parseInt(proxyPortProp);
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHostProp, port));
    }
    URLConnection urlConnection;
    if (proxy != null) {
        urlConnection = url.openConnection(proxy);
        if ((proxyUser != null) && (proxyPass != null)) {
            String password = proxyUser + ":" + proxyPass;
            String authString = "Basic " + Base64.encode(password.getBytes("ISO-8859-1"));
            urlConnection.setRequestProperty("Proxy-Authorization", authString);
        }
    } else {
        urlConnection = url.openConnection();
    }
    return urlConnection;
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) URLConnection(java.net.URLConnection)

Example 63 with Proxy

use of java.net.Proxy in project bnd by bndtools.

the class ConnectionSettings method createProxyHandler.

/**
	 * Create Proxy Handler from ProxyDTO
	 */
public static ProxyHandler createProxyHandler(final ProxyDTO proxyDTO) {
    return new ProxyHandler() {

        Glob[] globs;

        private ProxySetup proxySetup;

        @Override
        public ProxySetup forURL(URL url) throws Exception {
            switch(proxyDTO.protocol) {
                case DIRECT:
                    break;
                case HTTP:
                    String scheme = url.getProtocol();
                    if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) {
                    // ok
                    } else
                        return null;
                    break;
                case SOCKS:
                    break;
                default:
                    break;
            }
            String host = url.getHost();
            if (host != null) {
                if (isNonProxyHost(host))
                    return null;
            }
            if (proxySetup == null) {
                proxySetup = new ProxySetup();
                if (proxyDTO.username != null && proxyDTO.password != null)
                    proxySetup.authentication = new PasswordAuthentication(proxyDTO.username, proxyDTO.password.toCharArray());
                SocketAddress socketAddress;
                if (proxyDTO.host != null)
                    socketAddress = new InetSocketAddress(proxyDTO.host, proxyDTO.port);
                else
                    socketAddress = new InetSocketAddress(proxyDTO.port);
                proxySetup.proxy = new Proxy(proxyDTO.protocol, socketAddress);
            }
            return proxySetup;
        }

        public boolean isNonProxyHost(String host) {
            Glob[] globs = getNonProxyHosts(proxyDTO);
            for (Glob glob : globs) {
                if (glob.matcher(host).matches())
                    return true;
            }
            return false;
        }

        public Glob[] getNonProxyHosts(final ProxyDTO proxyDTO) {
            // not synchronized because conflicts only do some double work
            if (globs == null) {
                if (proxyDTO.nonProxyHosts == null)
                    globs = new Glob[0];
                else {
                    String[] parts = proxyDTO.nonProxyHosts.split("\\s*\\|\\s*");
                    globs = new Glob[parts.length];
                    for (int i = 0; i < parts.length; i++) globs[i] = new Glob(parts[i]);
                }
            }
            return globs;
        }
    };
}
Also used : ProxyHandler(aQute.bnd.service.url.ProxyHandler) InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL) Proxy(java.net.Proxy) Glob(aQute.libg.glob.Glob) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Example 64 with Proxy

use of java.net.Proxy in project bitsquare by bitsquare.

the class SocksConnectionSocketFactory method createSocket.

/**
     * creates an unconnected Socks Proxy socket
     */
@Override
public Socket createSocket(final HttpContext context) throws IOException {
    InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socks.address");
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
    return new Socket(proxy);
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket)

Example 65 with Proxy

use of java.net.Proxy in project android_frameworks_base by AOSPA.

the class PacProxySelector method parseResponse.

private static List<Proxy> parseResponse(String response) {
    String[] split = response.split(";");
    List<Proxy> ret = Lists.newArrayList();
    for (String s : split) {
        String trimmed = s.trim();
        if (trimmed.equals("DIRECT")) {
            ret.add(java.net.Proxy.NO_PROXY);
        } else if (trimmed.startsWith(PROXY)) {
            Proxy proxy = proxyFromHostPort(Type.HTTP, trimmed.substring(PROXY.length()));
            if (proxy != null) {
                ret.add(proxy);
            }
        } else if (trimmed.startsWith(SOCKS)) {
            Proxy proxy = proxyFromHostPort(Type.SOCKS, trimmed.substring(SOCKS.length()));
            if (proxy != null) {
                ret.add(proxy);
            }
        }
    }
    if (ret.size() == 0) {
        ret.add(java.net.Proxy.NO_PROXY);
    }
    return ret;
}
Also used : Proxy(java.net.Proxy)

Aggregations

Proxy (java.net.Proxy)146 InetSocketAddress (java.net.InetSocketAddress)78 URL (java.net.URL)51 IOException (java.io.IOException)38 URI (java.net.URI)23 Test (org.junit.Test)22 HttpURLConnection (java.net.HttpURLConnection)21 ProxySelector (java.net.ProxySelector)21 SocketAddress (java.net.SocketAddress)20 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)14 URISyntaxException (java.net.URISyntaxException)12 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PasswordAuthentication (java.net.PasswordAuthentication)8 URLConnection (java.net.URLConnection)7 HttpHost (org.apache.http.HttpHost)7 InterruptedIOException (java.io.InterruptedIOException)6 SSLServerSocket (javax.net.ssl.SSLServerSocket)6 InputStream (java.io.InputStream)5 List (java.util.List)5