Debian Codegraph @

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

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.search.desktop.gnome;
2
3 import java.io.IOException;
4 import java.net.ProxySelector;
5 import java.util.Properties;
6
7 import com.github.markusbernhardt.proxy.ProxySearchStrategy;
8 import com.github.markusbernhardt.proxy.selector.direct.NoProxySelector;
9 import com.github.markusbernhardt.proxy.selector.fixed.FixedProxySelector;
10 import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector;
11 import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector;
12 import com.github.markusbernhardt.proxy.util.Logger;
13 import com.github.markusbernhardt.proxy.util.ProxyException;
14 import com.github.markusbernhardt.proxy.util.ProxyUtil;
15 import com.github.markusbernhardt.proxy.util.Logger.LogLevel;
16
17 import java.io.BufferedReader;
18 import java.io.InputStreamReader;
19
20 /*****************************************************************************
21 * Loads the Gnome proxy settings from the Gnome GConf settings.
22 * <p>
23 * The following settings are extracted from the configuration that is stored in
24 * <i>.gconf</i> folder found in the user's home directory:
25 * </p>
26 * <ul>
27 * <li><i>org.gnome.system.proxy.http enabled</i> -&gt; bool used only by
28 * gnome-vfs</li>
29 * <li><i>org.gnome.system.proxy.http host</i> -&gt; string
30 * "my-proxy.example.com" without "http://"</li>
31 * <li><i>org.gnome.system.proxy.http port</i> -&gt; int</li>
32 * <li><i>org.gnome.system.proxy.http use-authentication</i> -&gt; bool</li>
33 * <li><i>org.gnome.system.proxy.http authentication-user</i> -&gt; string</li>
34 * <li><i>org.gnome.system.proxy.http authentication-password</i> -&gt; string
35 * </li>
36 * <li><i>org.gnome.system.proxy ignore-hosts</i> -&gt; list-of-string</li>
37 * <li><i>org.gnome.system.proxy mode</i> -&gt; string THIS IS THE CANONICAL
38 * KEY; SEE BELOW</li>
39 * <li><i>org.gnome.system.proxy use-same-proxy</i> -&gt; bool</li>
40 * <li><i>org.gnome.system.proxy.https host</i> -&gt; string
41 * "proxy-for-https.example.com"</li>
42 * <li><i>org.gnome.system.proxy.https port</i> -&gt; int</li>
43 * <li><i>org.gnome.system.proxy.ftp host</i> -&gt; string
44 * "proxy-for-ftp.example.com"</li>
45 * <li><i>org.gnome.system.proxy.ftp port</i> -&gt; int</li>
46 * <li><i>org.gnome.system.proxy.socks host</i> -&gt; string
47 * "proxy-for-socks.example.com"</li>
48 * <li><i>org.gnome.system.proxy.socks port</i> -&gt; int</li>
49 * <li><i>org.gnome.system.proxy autoconfig-url</i> -&gt; string
50 * "http://proxy-autoconfig.example.com"</li>
51 * </ul>
52 * <i>org.gnome.system.proxy mode</i> can be either:<br>
53 * "none" -&gt; No proxy is used<br>
54 * "manual" -&gt; The user's configuration values are used
55 * (org.gnome.system.http_proxy/{host,port,etc.})<br>
56 * "auto" -&gt; The "org.gnome.system.proxy/autoconfig_url" key is used <br>
57 * <p>
58 * GNOME Proxy_configuration settings are explained
59 * <a href="https://developer.gnome.org/ProxyConfiguration/">here</a> in detail
60 * </p>
61 *
62 * @author Markus Bernhardt, Copyright 2016
63 * @author Bernd Rosstauscher, Copyright 2009
64 ****************************************************************************/
65
66 // TODO Work in progress: Needs unit tests and testing and code review
67
68 public class GnomeDConfProxySearchStrategy implements ProxySearchStrategy {
69
70 /*************************************************************************
71 * ProxySelector
72 *
73 * @see java.net.ProxySelector#ProxySelector()
74 ************************************************************************/
75
76 public GnomeDConfProxySearchStrategy() {
77 super();
78 }
79
80 /*************************************************************************
81 * Loads the proxy settings and initializes a proxy selector for the Gnome
82 * proxy settings.
83 *
84 * @return a configured ProxySelector, null if none is found.
85 * @throws ProxyException
86 * on file reading error.
87 ************************************************************************/
88
89 @Override
90 public ProxySelector getProxySelector() throws ProxyException {
91
92 Logger.log(getClass(), LogLevel.TRACE, "Detecting Gnome proxy settings");
93
94 Properties settings = readSettings();
95
96 String type = settings.getProperty("org.gnome.system.proxy mode");
97 Logger.log(getClass(), LogLevel.TRACE, "Mode is :{0}", type);
98 ProxySelector result = null;
99 if (type == null) {
100 String useProxy = settings.getProperty("org.gnome.system.proxy.http enabled");
101 if (useProxy == null) {
102 return null;
103 }
104 type = Boolean.parseBoolean(useProxy) ? "manual" : "none";
105 }
106
107 if ("none".equals(type)) {
108 Logger.log(getClass(), LogLevel.TRACE, "Gnome uses no proxy");
109 result = NoProxySelector.getInstance();
110 }
111 if ("manual".equals(type)) {
112 Logger.log(getClass(), LogLevel.TRACE, "Gnome uses manual proxy settings");
113 result = setupFixedProxySelector(settings);
114 }
115 if ("auto".equals(type)) {
116 String pacScriptUrl = settings.getProperty("org.gnome.system.proxy autoconfig-url", "");
117 Logger.log(getClass(), LogLevel.TRACE, "Gnome uses autodetect script {0}", pacScriptUrl);
118 result = ProxyUtil.buildPacSelectorForUrl(pacScriptUrl);
119 }
120
121 // Wrap into white-list filter?
122 String noProxyList = settings.getProperty("org.gnome.system.proxy ignore-hosts", null);
123 if (result != null && noProxyList != null && noProxyList.trim().length() > 0) {
124 Logger.log(getClass(), LogLevel.TRACE, "Gnome uses proxy bypass list: {0}", noProxyList);
125 result = new ProxyBypassListSelector(noProxyList, result);
126 }
127
128 return result;
129 }
130
131 /*************************************************************************
132 * Gets the printable name of the search strategy.
133 *
134 * @return the printable name of the search strategy
135 ************************************************************************/
136
137 @Override
138 public String getName() {
139 return "gnome";
140 }
141
142 /*************************************************************************
143 * Load the proxy settings from the gconf settings XML file.
144 *
145 * @return the loaded settings stored in a properties object.
146 * @throws ProxyException
147 * on processing error.
148 ************************************************************************/
149
150 public Properties readSettings() throws ProxyException {
151 Properties settings = new Properties();
152 try {
153 parseSettings(settings);
154 } catch (IOException e) {
155 Logger.log(getClass(), LogLevel.ERROR, "Gnome settings read error.", e);
156 throw new ProxyException(e);
157 }
158 return settings;
159 }
160
161 /*************************************************************************
162 * Parse the fixed proxy settings and build an ProxySelector for this a
163 * chained configuration.
164 *
165 * @param settings
166 * the proxy settings to evaluate.
167 ************************************************************************/
168
169 private ProxySelector setupFixedProxySelector(Properties settings) {
170 if (!hasProxySettings(settings)) {
171 return null;
172 }
173 ProtocolDispatchSelector ps = new ProtocolDispatchSelector();
174 installHttpSelector(settings, ps);
175
176 if (useForAllProtocols(settings)) {
177 ps.setFallbackSelector(ps.getSelector("http"));
178 } else {
179 installSecureSelector(settings, ps);
180 installFtpSelector(settings, ps);
181 installSocksSelector(settings, ps);
182 }
183 return ps;
184 }
185
186 /*************************************************************************
187 * Check if the http proxy should also be used for all other protocols.
188 *
189 * @param settings
190 * to inspect.
191 * @return true if only one proxy is configured else false.
192 ************************************************************************/
193
194 private boolean useForAllProtocols(Properties settings) {
195 return Boolean.parseBoolean(settings.getProperty("org.gnome.system.proxy use-same-proxy", "false"));
196 }
197
198 /*************************************************************************
199 * Checks if we have Proxy configuration settings in the properties.
200 *
201 * @param settings
202 * to inspect.
203 * @return true if we have found Proxy settings.
204 ************************************************************************/
205
206 private boolean hasProxySettings(Properties settings) {
207 String proxyHost = settings.getProperty("org.gnome.system.proxy.http host", null);
208 return proxyHost != null && proxyHost.length() > 0;
209 }
210
211 /*************************************************************************
212 * Install a http proxy from the given settings.
213 *
214 * @param settings
215 * to inspect
216 * @param ps
217 * the dispatch selector to configure.
218 * @throws NumberFormatException
219 ************************************************************************/
220
221 private void installHttpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException {
222 String proxyHost = settings.getProperty("org.gnome.system.proxy.http host", null);
223 int proxyPort = Integer.parseInt(settings.getProperty("org.gnome.system.proxy.http port", "0").trim());
224 if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
225 Logger.log(getClass(), LogLevel.TRACE, "Gnome http proxy is {0}:{1}", proxyHost, proxyPort);
226 ps.setSelector("http", new FixedProxySelector(proxyHost.trim(), proxyPort));
227 }
228 }
229
230 /*************************************************************************
231 * Install a socks proxy from the given settings.
232 *
233 * @param settings
234 * to inspect
235 * @param ps
236 * the dispatch selector to configure.
237 * @throws NumberFormatException
238 ************************************************************************/
239
240 private void installSocksSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException {
241 String proxyHost = settings.getProperty("org.gnome.system.proxy.socks host", null);
242 int proxyPort = Integer.parseInt(settings.getProperty("org.gnome.system.proxy.socks port", "0").trim());
243 if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
244 Logger.log(getClass(), LogLevel.TRACE, "Gnome socks proxy is {0}:{1}", proxyHost, proxyPort);
245 ps.setSelector("socks", new FixedProxySelector(proxyHost.trim(), proxyPort));
246 }
247 }
248
249 /*************************************************************************
250 * @param settings
251 * @param ps
252 * @throws NumberFormatException
253 ************************************************************************/
254
255 private void installFtpSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException {
256 String proxyHost = settings.getProperty("org.gnome.system.proxy.ftp host", null);
257 int proxyPort = Integer.parseInt(settings.getProperty("org.gnome.system.proxy.ftp port", "0").trim());
258 if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
259 Logger.log(getClass(), LogLevel.TRACE, "Gnome ftp proxy is {0}:{1}", proxyHost, proxyPort);
260 ps.setSelector("ftp", new FixedProxySelector(proxyHost.trim(), proxyPort));
261 }
262 }
263
264 /*************************************************************************
265 * @param settings
266 * @param ps
267 * @throws NumberFormatException
268 ************************************************************************/
269
270 private void installSecureSelector(Properties settings, ProtocolDispatchSelector ps) throws NumberFormatException {
271 String proxyHost = settings.getProperty("org.gnome.system.proxy.https host", null);
272 int proxyPort = Integer.parseInt(settings.getProperty("org.gnome.system.proxy.https port", "0").trim());
273 if (proxyHost != null && proxyHost.length() > 0 && proxyPort > 0) {
274 Logger.log(getClass(), LogLevel.TRACE, "Gnome secure proxy is {0}:{1}", proxyHost, proxyPort);
275 ps.setSelector("https", new FixedProxySelector(proxyHost.trim(), proxyPort));
276 ps.setSelector("sftp", new FixedProxySelector(proxyHost.trim(), proxyPort));
277 }
278 }
279
280 /*************************************************************************
281 * Parse the settings file and extract all network.proxy.* settings from it.
282 *
283 * @param context
284 * the gconf context to parse.
285 * @param settings
286 * the settings object to fill.
287 * @return the parsed properties.
288 * @throws IOException
289 * on read error.
290 ************************************************************************/
291
292 private Properties parseSettings(Properties settings) throws IOException {
293 String line;
294 Logger.log(getClass(), LogLevel.TRACE, "exec gsettings list-recursively org.gnome.system.proxy");
295 Process p = Runtime.getRuntime().exec("gsettings list-recursively org.gnome.system.proxy");
296 BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
297
298 while ((line = bri.readLine()) != null) {
299 Logger.log(getClass(), LogLevel.TRACE, line);
300 int schemaSep = line.indexOf(" ");
301 int keySep = line.indexOf(" ", schemaSep + 1);
302 String entry = line.substring(0, keySep);
303 String value = line.substring(keySep + 1).replaceAll("'", "");
304
305 // TODO 30.03.2015 bros Test for IP6 compatibility
306 if (value.matches("\\[.*\\]")) {
307 value = value.replaceAll("\\[|\\]| ", "");
308 }
309
310 Logger.log(getClass(), LogLevel.TRACE, "prop is: {0}|{1}", entry, value);
311 settings.setProperty(entry, value);
312 }
313 try {
314 p.waitFor();
315 } catch (InterruptedException ex) {
316 Logger.log(getClass(), LogLevel.ERROR, ex.getMessage());
317 }
318 return settings;
319 }
320
321 }