summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/search/desktop/kde/KdeProxySearchStrategy.java
blob: de81c4cb5ca2c22def3396999815c94b7a8c2bf2 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.btr.proxy.search.desktop.kde;

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

import com.btr.proxy.search.ProxySearchStrategy;
import com.btr.proxy.search.env.EnvProxySearchStrategy;
import com.btr.proxy.search.wpad.WpadProxySearchStrategy;
import com.btr.proxy.selector.direct.NoProxySelector;
import com.btr.proxy.selector.fixed.FixedProxySelector;
import com.btr.proxy.selector.misc.ProtocolDispatchSelector;
import com.btr.proxy.selector.pac.PacProxySelector;
import com.btr.proxy.selector.pac.UrlPacScriptSource;
import com.btr.proxy.selector.whitelist.ProxyBypassListSelector;
import com.btr.proxy.selector.whitelist.UseProxyWhiteListSelector;
import com.btr.proxy.util.Logger;
import com.btr.proxy.util.Logger.LogLevel;
import com.btr.proxy.util.ProxyException;
import com.btr.proxy.util.ProxyUtil;

/*****************************************************************************
 * Loads the KDE4 proxy settings from the KDE <i>kioslaverc</i> file.
 * This will load properties from the file
 * <p>
 * <i>.kde/share/config/kioslaverc</i>
 * </P>
 * starting from the current users home directory.
 * <p>
 * The following settings are extracted from the section "[Proxy Settings]":
 * </p>
 * <ul>
 * <li><i>AuthMode</i> 	-> 0 = no auth., 1 = use login.</li>
 * <li><i>ProxyType</i> -> 0 = direct 1 = use fixed settings, 2 = use PAC, 3 = automatic (WPAD) , 4 = Use environment variables?</li>
 * <li><i>Proxy Config</i> Script -> URL to PAC file</li>
 * <li><i>ftpProxy</i> -> Fixed ftp proxy address e.g. http://www.ftp-proxy.com:8080</li>
 * <li><i>httpProxy</i> -> Fixed http proxy e.g http://www.http-proxy.com:8080</li>
 * <li><i>httpsProxy</i> -> Fixed https proxy e.g http://www.https-proxy.com:8080</li>
 * <li><i>NoProxyFor</i> -> Proxy white list</li>
 * <li><i>ReversedException</i> -> false = use NoProxyFor, true = revert meaning of the NoProxyFor list</li>
 * </ul>
 *
 *
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class KdeProxySearchStrategy implements ProxySearchStrategy {

	private KdeSettingsParser settingsParser;

	/*************************************************************************
	 * ProxySelector using the given parser.
	 * @see java.net.ProxySelector#ProxySelector()
	 ************************************************************************/

	public KdeProxySearchStrategy() {
		this(new KdeSettingsParser());
	}

	/*************************************************************************
	 * ProxySelector
	 * @see java.net.ProxySelector#ProxySelector()
	 ************************************************************************/

	public KdeProxySearchStrategy(KdeSettingsParser settingsParser) {
		super();
		this.settingsParser = settingsParser;
	}

	/*************************************************************************
	 * Loads the proxy settings and initializes a proxy selector for the firefox
	 * proxy settings.
	 * @return a configured ProxySelector, null if none is found.
	 * @throws ProxyException on file reading error.
	 ************************************************************************/

	public ProxySelector getProxySelector() throws ProxyException {

		Logger.log(getClass(), LogLevel.TRACE, "Detecting Kde proxy settings");

		Properties settings = readSettings();
		if (settings == null) {
			return null;
		}

		ProxySelector result = null;
		int type = Integer.parseInt(settings.getProperty("ProxyType", "-1"));
		switch (type) {
			case 0: // Use no proxy
				Logger.log(getClass(), LogLevel.TRACE, "Kde uses no proxy");
				result = NoProxySelector.getInstance();
				break;
			case 1: // Fixed settings
				Logger.log(getClass(), LogLevel.TRACE, "Kde uses manual proxy settings");
				result = setupFixedProxySelector(settings);
				break;
			case 2: // PAC Script
				String pacScriptUrl = settings.getProperty("Proxy Config Script", "");
				Logger.log(getClass(), LogLevel.TRACE, "Kde uses autodetect script {0}", pacScriptUrl);
				result = ProxyUtil.buildPacSelectorForUrl(pacScriptUrl);
				break;
			case 3: // WPAD
				Logger.log(getClass(), LogLevel.TRACE, "Kde uses WPAD to detect the proxy");
				result = new WpadProxySearchStrategy().getProxySelector();
				break;
			case 4: // Use environment variables
				Logger.log(getClass(), LogLevel.TRACE, "Kde reads proxy from environment");
				result = setupEnvVarSelector(settings);
				break;
			default:
				break;
		}

		return result;
	}

	/*************************************************************************
	 * Reads the settings and stores them in a properties map.
	 * @return the parsed settings.
	 * @throws ProxyException
	 ************************************************************************/

	private Properties readSettings() throws ProxyException {
		try {
			return  this.settingsParser.parseSettings();
		} catch (IOException e) {
			Logger.log(getClass(), LogLevel.ERROR, "Can't parse settings.", e);
			throw new ProxyException(e);
		}
	}

	/*************************************************************************
	 * Builds an environment variable selector.
	 * @param settings the settings to read from.
	 * @return the ProxySelector using environment variables.
	 ************************************************************************/

	private ProxySelector setupEnvVarSelector(Properties settings) {
		ProxySelector result;
		result = new EnvProxySearchStrategy(
				settings.getProperty("httpProxy"),
				settings.getProperty("httpsProxy"),
				settings.getProperty("ftpProxy"),
				settings.getProperty("NoProxyFor")
				).getProxySelector();
		return result;
	}

	/*************************************************************************
	 * Parse the fixed proxy settings and build an ProxySelector for this a
	 * chained configuration.
	 * @param settings the proxy settings to evaluate.
	 ************************************************************************/

	private ProxySelector setupFixedProxySelector(Properties settings) {
		String proxyVar = settings.getProperty("httpProxy", null);
		ProxySelector httpPS = ProxyUtil.parseProxySettings(proxyVar);
		if (httpPS == null) {
			Logger.log(getClass(), LogLevel.TRACE, "Kde http proxy is {0}", proxyVar);
			return null;
		}

		ProtocolDispatchSelector ps = new ProtocolDispatchSelector();
		ps.setSelector("http", httpPS);

		proxyVar = settings.getProperty("httpsProxy", null);
		ProxySelector httpsPS = ProxyUtil.parseProxySettings(proxyVar);
		if (httpsPS != null) {
			Logger.log(getClass(), LogLevel.TRACE, "Kde https proxy is {0}", proxyVar);
			ps.setSelector("https", httpsPS);
		}

		proxyVar = settings.getProperty("ftpProxy", null);
		ProxySelector ftpPS = ProxyUtil.parseProxySettings(proxyVar);
		if (ftpPS != null) {
			Logger.log(getClass(), LogLevel.TRACE, "Kde ftp proxy is {0}", proxyVar);
			ps.setSelector("ftp", ftpPS);
		}

		// Wrap in white list filter.
		String noProxyList = settings.getProperty("NoProxyFor", null);
		if (noProxyList != null && noProxyList.trim().length() > 0) {
			boolean reverse = "true".equals(settings.getProperty("ReversedException", "false"));
			if (reverse) {
				Logger.log(getClass(), LogLevel.TRACE, "Kde proxy blacklist is {0}", noProxyList);
				return new UseProxyWhiteListSelector(noProxyList, ps);
			} else {
				Logger.log(getClass(), LogLevel.TRACE, "Kde proxy whitelist is {0}", noProxyList);
				return new ProxyBypassListSelector(noProxyList, ps);
			}
		}

		return ps;
	}



}