Search in sources :

Example 1 with FixedSocksSelector

use of com.github.markusbernhardt.proxy.selector.fixed.FixedSocksSelector 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)

Aggregations

ProxySearch (com.github.markusbernhardt.proxy.ProxySearch)1 FixedSocksSelector (com.github.markusbernhardt.proxy.selector.fixed.FixedSocksSelector)1 ProtocolDispatchSelector (com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector)1 Proxy (java.net.Proxy)1 ProxySelector (java.net.ProxySelector)1 Pattern (java.util.regex.Pattern)1