All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.markusbernhardt.proxy.search.java.JavaProxySearchStrategy Maven / Gradle / Ivy

Go to download

Proxy Vole is a Java library to auto detect the platform network proxy settings.

The newest version!
package com.github.markusbernhardt.proxy.search.java;

import java.net.ProxySelector;

import com.github.markusbernhardt.proxy.ProxySearchStrategy;
import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector;
import com.github.markusbernhardt.proxy.selector.fixed.FixedSocksSelector;
import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector;
import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector;
import com.github.markusbernhardt.proxy.util.Logger;
import com.github.markusbernhardt.proxy.util.Logger.LogLevel;

/*****************************************************************************
 * Reads some java system properties and extracts the proxy settings from them.
 * The following variables are read:
 * 
    *
  • http.proxyHost (default: none)
  • *
  • http.proxyPort (default: 80 if http.proxyHost specified)
  • *
  • http.nonProxyHosts (default: none)
  • *
*
    *
  • https.proxyHost (default: none)
  • *
  • https.proxyPort (default: 443 if https.proxyHost specified)
  • *
*
    *
  • ftp.proxyHost (default: none)
  • *
  • ftp.proxyPort (default: 80 if ftp.proxyHost specified)
  • *
  • ftp.nonProxyHosts (default: none)
  • *
*
    *
  • socksProxyHost
  • *
  • socksProxyPort (default: 1080)
  • *
*

* This is based on information found here:
* http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html *

* If the "http.proxyHost" property is not set then the no proxy selector is * setup This property is used as marker to signal that the System settings * should be used. * * @author Markus Bernhardt, Copyright 2016 * @author Bernd Rosstauscher, Copyright 2009 ****************************************************************************/ public class JavaProxySearchStrategy implements ProxySearchStrategy { /************************************************************************* * Constructor Will use the default environment variables. ************************************************************************/ public JavaProxySearchStrategy() { super(); } /************************************************************************* * Loads the proxy settings from environment variables. * * @return a configured ProxySelector, null if none is found. ************************************************************************/ @Override public ProxySelector getProxySelector() { ProtocolDispatchSelector ps = new ProtocolDispatchSelector(); Logger.log(getClass(), LogLevel.TRACE, "Using settings from Java System Properties"); setupProxyForProtocol(ps, "http", 80); setupProxyForProtocol(ps, "https", 443); setupProxyForProtocol(ps, "ftp", 80); setupProxyForProtocol(ps, "ftps", 80); boolean socksAvailable = setupSocktProxy(ps); if (ps.size() == 0 && !socksAvailable) { return null; } return ps; } /************************************************************************* * Gets the printable name of the search strategy. * * @return the printable name of the search strategy ************************************************************************/ @Override public String getName() { return "java"; } /************************************************************************* * Parse SOCKS settings * * @param ps * @throws NumberFormatException ************************************************************************/ private boolean setupSocktProxy(ProtocolDispatchSelector ps) { String host = System.getProperty("socksProxyHost"); if (host == null || host.trim().length() == 0) { return false; } String port = System.getProperty("socksProxyPort", "1080"); Logger.log(getClass(), LogLevel.TRACE, "Socks proxy {0}:{1} found", host, port); ps.setFallbackSelector(new FixedSocksSelector(host, Integer.parseInt(port))); return true; } /************************************************************************* * Parse properties for the given protocol. * * @param ps * @param protocol * @throws NumberFormatException ************************************************************************/ private void setupProxyForProtocol(ProtocolDispatchSelector ps, String protocol, int defaultPort) { String host = System.getProperty(protocol + ".proxyHost"); if (host == null || host.trim().length() == 0) { return; } String port = System.getProperty(protocol + ".proxyPort", Integer.toString(defaultPort)); String whiteList = System.getProperty(protocol + ".nonProxyHosts", "").replace('|', ','); if ("https".equalsIgnoreCase(protocol)) { // This is dirty but https has // no own property for it. whiteList = System.getProperty("http.nonProxyHosts", "").replace('|', ','); } Logger.log(getClass(), LogLevel.TRACE, protocol.toUpperCase() + " proxy {0}:{1} found using whitelist: {2}", host, port, whiteList); ProxySelector protocolSelector = new FixedProxySelector(host, Integer.parseInt(port)); if (whiteList.trim().length() > 0) { protocolSelector = new ProxyBypassListSelector(whiteList, protocolSelector); } ps.setSelector(protocol, protocolSelector); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy