Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/desktop/gnome/GnomeProxySearchStrategy.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.search.desktop.gnome;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.ProxySelector;
6 import java.util.Properties;
7
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10 import javax.xml.parsers.ParserConfigurationException;
11
12 import org.w3c.dom.Document;
13 import org.w3c.dom.Element;
14 import org.w3c.dom.Node;
15 import org.w3c.dom.NodeList;
16 import org.xml.sax.SAXException;
17
18 import com.github.markusbernhardt.proxy.ProxySearchStrategy;
19 import com.github.markusbernhardt.proxy.selector.direct.NoProxySelector;
20 import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector;
21 import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector;
22 import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector;
23 import com.github.markusbernhardt.proxy.util.EmptyXMLResolver;
24 import com.github.markusbernhardt.proxy.util.Logger;
25 import com.github.markusbernhardt.proxy.util.PlatformUtil;
26 import com.github.markusbernhardt.proxy.util.ProxyException;
27 import com.github.markusbernhardt.proxy.util.ProxyUtil;
28 import com.github.markusbernhardt.proxy.util.Logger.LogLevel;
29
30 /*****************************************************************************
31 * Loads the Gnome proxy settings from the Gnome GConf settings.
32 * <p>
33 * The following settings are extracted from the configuration that is stored in
34 * <i>.gconf</i> folder found in the user's home directory:
35 * </p>
36 * <ul>
37 * <li><i>/system/http_proxy/use_http_proxy</i> -&gt; bool used only by
38 * gnome-vfs</li>
39 * <li><i>/system/http_proxy/host</i> -&gt; string "my-proxy.example.com"
40 * without "http://"</li>
41 * <li><i>/system/http_proxy/port</i> -&gt; int</li>
42 * <li><i>/system/http_proxy/use_authentication</i> -&gt; bool</li>
43 * <li><i>/system/http_proxy/authentication_user</i> -&gt; string</li>
44 * <li><i>/system/http_proxy/authentication_password</i> -&gt; string</li>
45 * <li><i>/system/http_proxy/ignore_hosts</i> -&gt; list-of-string</li>
46 * <li><i>/system/proxy/mode</i> -&gt; string THIS IS THE CANONICAL KEY; SEE
47 * BELOW</li>
48 * <li><i>/system/proxy/secure_host</i> -&gt; string
49 * "proxy-for-https.example.com"</li>
50 * <li><i>/system/proxy/secure_port</i> -&gt; int</li>
51 * <li><i>/system/proxy/ftp_host</i> -&gt; string "proxy-for-ftp.example.com"
52 * </li>
53 * <li><i>/system/proxy/ftp_port</i> -&gt; int</li>
54 * <li><i>/system/proxy/socks_host</i> -&gt; string
55 * "proxy-for-socks.example.com"</li>
56 * <li><i>/system/proxy/socks_port</i> -&gt; int</li>
57 * <li><i>/system/proxy/autoconfig_url</i> -&gt; string
58 * "http://proxy-autoconfig.example.com"</li>
59 * </ul>
60 * <i>/system/proxy/mode</i> can be either:<br>
61 * "none" -&gt; No proxy is used<br>
62 * "manual" -&gt; The user's configuration values are used
63 * (/system/http_proxy/{host,port,etc.})<br>
64 * "auto" -&gt; The "/system/proxy/autoconfig_url" key is used <br>
65 * <p>
66 * GNOME Proxy_configuration settings are explained
67 * <a href="http://en.opensuse.org/GNOME/Proxy_configuration">here</a> in detail
68 * </p>
69 *
70 * @author Markus Bernhardt, Copyright 2016
71 * @author Bernd Rosstauscher, Copyright 2009
72 ****************************************************************************/
73
74 public class GnomeProxySearchStrategy implements ProxySearchStrategy {
75
76 /*************************************************************************
77 * ProxySelector
78 *
79 * @see java.net.ProxySelector#ProxySelector()
80 ************************************************************************/
81
82 public GnomeProxySearchStrategy() {
83 super();
84 }
85
86 /*************************************************************************
87 * Loads the proxy settings and initializes a proxy selector for the Gnome
88 * proxy settings.
89 *
90 * @return a configured ProxySelector, null if none is found.
91 * @throws ProxyException
92 * on file reading error.
93 ************************************************************************/
94
95 @Override
96 public ProxySelector getProxySelector() throws ProxyException {
97
98 Logger.log(getClass(), LogLevel.TRACE, "Detecting Gnome proxy settings");
99
100 Properties settings = readSettings();
101
102 String type = settings.getProperty("/system/proxy/mode");
103 ProxySelector result = null;
104 if (type == null) {
105 String useProxy = settings.getProperty("/system/http_proxy/use_http_proxy");
106 if (useProxy == null) {
107 return null;
108 }
109 type = Boolean.parseBoolean(useProxy) ? "manual" : "none";
110 }
111
112 if ("none".equals(type)) {
113 Logger.log(getClass(), LogLevel.TRACE, "Gnome uses no proxy");
114 result = NoProxySelector.getInstance();
115 }
116 if ("manual".equals(type)) {
117 Logger.log(getClass(), LogLevel.TRACE, "Gnome uses manual proxy settings");
118 result = setupFixedProxySelector(settings);
119 }
120 if ("auto".equals(type)) {
121 String pacScriptUrl = settings.getProperty("/system/proxy/autoconfig_url", "");
122 Logger.log(getClass(), LogLevel.TRACE, "Gnome uses autodetect script {0}", pacScriptUrl);
123 result = ProxyUtil.buildPacSelectorForUrl(pacScriptUrl);
124 }
125
126 // Wrap into white-list filter?
127 String noProxyList = settings.getProperty("/system/http_proxy/ignore_hosts", null);
128 if (result != null && noProxyList != null && noProxyList.trim().length() > 0) {
129 Logger.log(getClass(), LogLevel.TRACE, "Gnome uses proxy bypass list: {0}", noProxyList);
130 result = new ProxyBypassListSelector(noProxyList, result);
131 }
132
133 return result;
134 }
135
136 /*************************************************************************
137 * Gets the printable name of the search strategy.
138 *
139 * @return the printable name of the search strategy
140 ************************************************************************/
141
142 @Override
143 public String getName() {
144 return "gnome";
145 }
146
147 /*************************************************************************
148 * Load the proxy settings from the gconf settings XML file.
149 *
150 * @return the loaded settings stored in a properties object.
151 * @throws ProxyException
152 * on processing error.
153 ************************************************************************/
154
155 public Properties readSettings() throws ProxyException {
156 Properties settings = new Properties();
157 try {
158 parseSettings("/system/proxy/", settings);
159 parseSettings("/system/http_proxy/", settings);
160 } catch (IOException e) {
161 Logger.log(getClass(), LogLevel.ERROR, "Gnome settings file error.", e);
162 throw new ProxyException(e);
163 }
164 return settings;
165 }
166
167 /*************************************************************************
168 * Finds the Gnome GConf settings file.
169 *
170 * @param context
171 * the gconf context to parse.
172 * @return a file or null if does not exist.
173 ************************************************************************/
174
175 private File findSettingsFile(String context) {
176 // Normally we should inspect /etc/gconf/<version>/path to find out
177 // where the actual file is.
178 // But for normal systems this is always stored in .gconf folder in the
179 // user's home directory.
180 File userDir = new File(PlatformUtil.getUserHomeDir());
181
182 // Build directory path for context
183 StringBuilder path = new StringBuilder();
184 String[] parts = context.split("/");
185 for (String part : parts) {
186 path.append(part);
187 path.append(File.separator);
188 }
189
190 File settingsFile = new File(userDir, ".gconf" + File.separator + path.toString() + "%gconf.xml");
191 if (!settingsFile.exists()) {
192 Logger.log(getClass(), LogLevel.WARNING, "Gnome settings: {0} not found.", settingsFile);
193 return null;
194 }
195 return settingsFile;
196 }
197
198 /*************************************************************************
199 * Parse the fixed proxy settings and build an ProxySelector for this a
200 * chained configuration.
201 *
202 * @param settings
203 * the proxy settings to evaluate.
204 ************************************************************************/
205
206 private ProxySelector setupFixedProxySelector(Properties settings) {
207 if (!hasProxySettings(settings)) {
208 return null;
209 }
210 ProtocolDispatchSelector ps = new ProtocolDispatchSelector();
211 installHttpSelector(settings, ps);
212
213 if (useForAllProtocols(settings)) {
214 ps.setFallbackSelector(ps.getSelector("http"));
215 } else {
216 installSecureSelector(settings, ps);
217 installFtpSelector(settings, ps);
218 installSocksSelector(settings, ps);
219 }
220 return ps;
221 }
222
223 /*************************************************************************
224 * Check if the http proxy should also be used for all other protocols.
225 *
226 * @param settings
227 * to inspect.
228 * @return true if only one proxy is configured else false.
229 ************************************************************************/
230
231 private boolean useForAllProtocols(Properties settings) {
232 return Boolean.parseBoolean(settings.getProperty("/system/http_proxy/use_same_proxy", "false"));
233 }
234
235 /*************************************************************************
236 * Checks if we have Proxy configuration settings in the properties.
237 *
238 * @param settings
239 * to inspect.
240 * @return true if we have found Proxy settings.
241 ************************************************************************/
242
243 private boolean hasProxySettings(Properties settings) {
244 String proxyHost = settings.getProperty("/system/http_proxy/host", null);
245 return proxyHost != null && proxyHost.length() > 0;
246 }
247
248 /*************************************************************************
249 * Install a http proxy from the given settings.
250 *
251 * @param settings
252 * to inspect
253 * @param ps
254 * the dispatch selector to configure.
255 * @throws NumberFormatException
256 ************************************************************************/
257
258 private void installHttpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException {
259 String proxyHost = settings.getProperty("/system/http_proxy/host", null);
260 int proxyPort = Integer.parseInt(settings.getProperty("/system/http_proxy/port", "0").trim());
261 if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
262 Logger.log(getClass(), LogLevel.TRACE, "Gnome http proxy is {0}:{1}", proxyHost, proxyPort);
263 ps.setSelector("http", new FixedProxySelector(proxyHost.trim(), proxyPort));
264 }
265 }
266
267 /*************************************************************************
268 * Install a socks proxy from the given settings.
269 *
270 * @param settings
271 * to inspect
272 * @param ps
273 * the dispatch selector to configure.
274 * @throws NumberFormatException
275 ************************************************************************/
276
277 private void installSocksSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException {
278 String proxyHost = settings.getProperty("/system/proxy/socks_host", null);
279 int proxyPort = Integer.parseInt(settings.getProperty("/system/proxy/socks_port", "0").trim());
280 if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
281 Logger.log(getClass(), LogLevel.TRACE, "Gnome socks proxy is {0}:{1}", proxyHost, proxyPort);
282 ps.setSelector("socks", new FixedProxySelector(proxyHost.trim(), proxyPort));
283 }
284 }
285
286 /*************************************************************************
287 * @param settings
288 * @param ps
289 * @throws NumberFormatException
290 ************************************************************************/
291
292 private void installFtpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException {
293 String proxyHost = settings.getProperty("/system/proxy/ftp_host", null);
294 int proxyPort = Integer.parseInt(settings.getProperty("/system/proxy/ftp_port", "0").trim());
295 if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
296 Logger.log(getClass(), LogLevel.TRACE, "Gnome ftp proxy is {0}:{1}", proxyHost, proxyPort);
297 ps.setSelector("ftp", new FixedProxySelector(proxyHost.trim(), proxyPort));
298 }
299 }
300
301 /*************************************************************************
302 * @param settings
303 * @param ps
304 * @throws NumberFormatException
305 ************************************************************************/
306
307 private void installSecureSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException {
308 String proxyHost = settings.getProperty("/system/proxy/secure_host", null);
309 int proxyPort = Integer.parseInt(settings.getProperty("/system/proxy/secure_port", "0").trim());
310 if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
311 Logger.log(getClass(), LogLevel.TRACE, "Gnome secure proxy is {0}:{1}", proxyHost, proxyPort);
312 ps.setSelector("https", new FixedProxySelector(proxyHost.trim(), proxyPort));
313 ps.setSelector("sftp", new FixedProxySelector(proxyHost.trim(), proxyPort));
314 }
315 }
316
317 /*************************************************************************
318 * Parse the settings file and extract all network.proxy.* settings from it.
319 *
320 * @param context
321 * the gconf context to parse.
322 * @param settings
323 * the settings object to fill.
324 * @return the parsed properties.
325 * @throws IOException
326 * on read error.
327 ************************************************************************/
328
329 private Properties parseSettings(String context, Properties settings) throws IOException {
330
331 // Read settings from file
332 File settingsFile = findSettingsFile(context);
333 if (settingsFile == null) {
334 return settings;
335 }
336
337 try {
338 DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
339 documentBuilder.setEntityResolver(new EmptyXMLResolver());
340 Document doc = documentBuilder.parse(settingsFile);
341 Element root = doc.getDocumentElement();
342 Node entry = root.getFirstChild();
343 while (entry != null) {
344 if ("entry".equals(entry.getNodeName()) && entry instanceof Element) {
345 String entryName = ((Element) entry).getAttribute("name");
346 settings.setProperty(context + entryName, getEntryValue((Element) entry));
347 }
348 entry = entry.getNextSibling();
349 }
350 } catch (SAXException e) {
351 Logger.log(getClass(), LogLevel.ERROR, "Gnome settings parse error", e);
352 throw new IOException(e.getMessage());
353 } catch (ParserConfigurationException e) {
354 Logger.log(getClass(), LogLevel.ERROR, "Gnome settings parse error", e);
355 throw new IOException(e.getMessage());
356 }
357
358 return settings;
359 }
360
361 /*************************************************************************
362 * Parse an entry value from a given entry node.
363 *
364 * @param entry
365 * the XML node to inspect.
366 * @return the value, null if it has no value.
367 ************************************************************************/
368
369 private String getEntryValue(Element entry) {
370 String type = entry.getAttribute("type");
371
372 if ("int".equals(type) || "bool".equals(type)) {
373 return entry.getAttribute("value");
374 }
375 if ("string".equals(type)) {
376 NodeList list = entry.getElementsByTagName("stringvalue");
377 if (list.getLength() > 0) {
378 return list.item(0).getTextContent();
379 }
380 }
381 if ("list".equals(type)) {
382 StringBuilder result = new StringBuilder();
383 NodeList list = entry.getElementsByTagName("li");
384
385 // Build comma separated list of items
386 for (int i = 0; i < list.getLength(); i++) {
387 if (result.length() > 0) {
388 result.append(",");
389 }
390 result.append(getEntryValue((Element) list.item(i)));
391 }
392 return result.toString();
393 }
394 return null;
395 }
396
397 }