summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/search/env/EnvProxySearchStrategy.java
blob: f7b6483e6601884520bf45e0d8f9d9625641c9fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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;
	}

}