summaryrefslogblamecommitdiffstats
path: root/src/main/java/com/btr/proxy/search/env/EnvProxySearchStrategy.java
blob: f7b6483e6601884520bf45e0d8f9d9625641c9fe (plain) (tree)
1
2
3
4
5
6
7
8
9
10





                                                
                                                       


                                                                
                                          
                                    






















































































                                                                                                          
                                                                             
 



                                                                                                    
                 
 




                                                                                                      
 
                                                                                       



                                                                                                  




                                                                    











                                                                                                             
package com.btr.proxy.search.env;

import java.net.ProxySelector;
import java.util.Properties;

import com.btr.proxy.search.ProxySearchStrategy;
import com.btr.proxy.selector.fixed.FixedProxySelector;
import com.btr.proxy.selector.misc.ProtocolDispatchSelector;
import com.btr.proxy.selector.whitelist.ProxyBypassListSelector;
import com.btr.proxy.util.Logger;
import com.btr.proxy.util.Logger.LogLevel;
import com.btr.proxy.util.ProxyUtil;

/*****************************************************************************
 * Reads some environment variables and extracts the proxy settings from them.
 * These variables are mainly set on linux / unix environments.
 * The following variables are read per default:
 * <ul>
 * <li><i>http_proxy</i>   -> This will be used for http / https</li>
 * <li><i>https_proxy</i>  -> Will be used for https, if not set then http_proxy is used instead.</li>
 * <li><i>ftp_proxy</i>    -> Used for FTP.</li>
 * <li><i>no_proxy</i>     -> a no proxy white list.</li>
 * </ul>
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class EnvProxySearchStrategy implements ProxySearchStrategy {
	
	private String httpEnv;
	private String httpsEnv;
	private String ftpEnv;
	private String noProxyEnv;
	
	private String httpProxy;
	private String httpsProxy;
	private String ftpProxy;
	private String noProxy;
	
	/*************************************************************************
	 * Constructor
	 * Will use the default environment variables.
	 ************************************************************************/
	
	public EnvProxySearchStrategy() {
		this("http_proxy", "https_proxy", "ftp_proxy", "no_proxy");
	}
	
	/*************************************************************************
	 * Constructor
	 * @param httpEnv name of environment variable
	 * @param httpsEnv name of environment variable
	 * @param ftpEnv name of environment variable
	 * @param noProxyEnv name of environment variable
	 ************************************************************************/
	
	public EnvProxySearchStrategy(String httpEnv, String httpsEnv, String ftpEnv, String noProxyEnv) {
		super();
		this.httpEnv = httpEnv;
		this.httpsEnv = httpsEnv;
		this.ftpEnv = ftpEnv;
		this.noProxyEnv = noProxyEnv;

		loadProxySettings();
	}

	/*************************************************************************
	 * Loads the proxy settings from the system environment variables. 
	 ************************************************************************/
	
	private void loadProxySettings() {
		this.httpProxy = System.getenv(this.httpEnv);
		this.httpsProxy = System.getenv(this.httpsEnv);
		this.ftpProxy = System.getenv(this.ftpEnv);
		this.noProxy = System.getenv(this.noProxyEnv);
	}
	
	/*************************************************************************
	 * Loads the settings and stores them in a properties map.
	 * @return the settings.
	 ************************************************************************/
	
	public Properties readSettings() {
		Properties result = new Properties();
		result.setProperty(this.httpEnv, this.httpProxy);
		result.setProperty(this.httpsEnv, this.httpsProxy);
		result.setProperty(this.ftpEnv, this.ftpProxy);
		result.setProperty(this.noProxyEnv, this.noProxy);
		return result;
	}
	
	
	/*************************************************************************
	 * Loads the proxy settings from environment variables.
	 * @return a configured ProxySelector, null if none is found.
	 ************************************************************************/

	public ProxySelector getProxySelector() {
		
		Logger.log(getClass(), LogLevel.TRACE, "Inspecting environment variables.");
		ProtocolDispatchSelector ps = new ProtocolDispatchSelector();

		FixedProxySelector httpPS = ProxyUtil.parseProxySettings(this.httpProxy);
		if (httpPS != null) {
			Logger.log(getClass(), LogLevel.TRACE, "Http Proxy is {0}", this.httpProxy);
			ps.setSelector("http", httpPS);
		}

		FixedProxySelector httpsPS = ProxyUtil.parseProxySettings(this.httpsProxy);
		if (httpsPS != null) {
			Logger.log(getClass(), LogLevel.TRACE, "Https Proxy is {0}", this.httpsProxy);
			ps.setSelector("https", httpsPS);
		}

		FixedProxySelector ftpPS = ProxyUtil.parseProxySettings(this.ftpProxy);
		if (ftpPS != null) {
			Logger.log(getClass(), LogLevel.TRACE, "Ftp Proxy is {0}", this.ftpProxy);
			ps.setSelector("ftp", ftpPS);			
		}
		
		ps.setFallbackSocksSelector(httpPS, httpsPS, ftpPS);
		
		if (ps.isEmpty())
			return null;

		// Wrap with white list support
		ProxySelector result = ps;
		if (this.noProxy != null && this.noProxy.trim().length() > 0) {
			Logger.log(getClass(), LogLevel.TRACE, "Using proxy bypass list: {0}", this.noProxy);
			result = new ProxyBypassListSelector(this.noProxy, ps);
		}
		
		return result;
	}

}